blob: 47943d073d6ddd9e9aa13bc35282a1ee83f0ee2e [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"
Sean Christopherson8373d252018-12-03 13:53:08 -080065#include "vmx.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030066
Avi Kivity6aa8b732006-12-10 02:21:36 -080067MODULE_AUTHOR("Qumranet");
68MODULE_LICENSE("GPL");
69
Josh Triplette9bda3b2012-03-20 23:33:51 -070070static const struct x86_cpu_id vmx_cpu_id[] = {
71 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
75
Sean Christopherson2c4fd912018-12-03 13:53:03 -080076bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080078
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010079static bool __read_mostly enable_vnmi = 1;
80module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
81
Sean Christopherson2c4fd912018-12-03 13:53:03 -080082bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020083module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020084
Sean Christopherson2c4fd912018-12-03 13:53:03 -080085bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020086module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080087
Sean Christopherson2c4fd912018-12-03 13:53:03 -080088bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070089module_param_named(unrestricted_guest,
90 enable_unrestricted_guest, bool, S_IRUGO);
91
Sean Christopherson2c4fd912018-12-03 13:53:03 -080092bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080093module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
94
Avi Kivitya27685c2012-06-12 20:30:18 +030095static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020096module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030097
Rusty Russell476bc002012-01-13 09:32:18 +103098static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030099module_param(fasteoi, bool, S_IRUGO);
100
Yang Zhang5a717852013-04-11 19:25:16 +0800101static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800102module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800103
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200109static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800114bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_BITMAP_MODE_X2APIC 1
118#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100119
Haozhong Zhang64903d62015-10-20 15:39:09 +0800120#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
121
Yunhong Jiang64672c92016-06-13 14:19:59 -0700122/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
123static int __read_mostly cpu_preemption_timer_multi;
124static bool __read_mostly enable_preemption_timer = 1;
125#ifdef CONFIG_X86_64
126module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
127#endif
128
Sean Christopherson3de63472018-07-13 08:42:30 -0700129#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800130#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
131#define KVM_VM_CR0_ALWAYS_ON \
132 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
133 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200134#define KVM_CR4_GUEST_OWNED_BITS \
135 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800136 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200137
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800138#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200139#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
140#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
141
Avi Kivity78ac8b42010-04-08 18:19:35 +0300142#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
143
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800144/*
145 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
146 * ple_gap: upper bound on the amount of time between two successive
147 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500148 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800149 * ple_window: upper bound on the amount of time a guest is allowed to execute
150 * in a PAUSE loop. Tests indicate that most spinlocks are held for
151 * less than 2^12 cycles
152 * Time is measured based on a counter that runs at the same rate as the TSC,
153 * refer SDM volume 3b section 21.6.13 & 22.1.3.
154 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400155static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500156module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200157
Babu Moger7fbc85a2018-03-16 16:37:22 -0400158static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
159module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800160
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200161/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400162static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400163module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200164
165/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400166static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400167module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200168
169/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400170static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
171module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200172
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200173static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200174static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200175static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200176
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200177/* Storage for pre module init parameter parsing */
178static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200179
180static const struct {
181 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200182 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200183} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200184 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
185 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
186 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
187 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
188 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
189 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200190};
191
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200192#define L1D_CACHE_ORDER 4
193static void *vmx_l1d_flush_pages;
194
195static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
196{
197 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200198 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200199
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200200 if (!enable_ept) {
201 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
202 return 0;
203 }
204
Yi Wangd806afa2018-08-16 13:42:39 +0800205 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
206 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200207
Yi Wangd806afa2018-08-16 13:42:39 +0800208 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
209 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
210 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
211 return 0;
212 }
213 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200214
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200215 /* If set to auto use the default l1tf mitigation method */
216 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
217 switch (l1tf_mitigation) {
218 case L1TF_MITIGATION_OFF:
219 l1tf = VMENTER_L1D_FLUSH_NEVER;
220 break;
221 case L1TF_MITIGATION_FLUSH_NOWARN:
222 case L1TF_MITIGATION_FLUSH:
223 case L1TF_MITIGATION_FLUSH_NOSMT:
224 l1tf = VMENTER_L1D_FLUSH_COND;
225 break;
226 case L1TF_MITIGATION_FULL:
227 case L1TF_MITIGATION_FULL_FORCE:
228 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
229 break;
230 }
231 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
232 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
233 }
234
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200235 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
236 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
237 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
238 if (!page)
239 return -ENOMEM;
240 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200241
242 /*
243 * Initialize each page with a different pattern in
244 * order to protect against KSM in the nested
245 * virtualization case.
246 */
247 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
248 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
249 PAGE_SIZE);
250 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200251 }
252
253 l1tf_vmx_mitigation = l1tf;
254
Thomas Gleixner895ae472018-07-13 16:23:22 +0200255 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
256 static_branch_enable(&vmx_l1d_should_flush);
257 else
258 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200259
Nicolai Stange427362a2018-07-21 22:25:00 +0200260 if (l1tf == VMENTER_L1D_FLUSH_COND)
261 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200262 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200263 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200264 return 0;
265}
266
267static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200268{
269 unsigned int i;
270
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200271 if (s) {
272 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200273 if (vmentry_l1d_param[i].for_parse &&
274 sysfs_streq(s, vmentry_l1d_param[i].option))
275 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200276 }
277 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200278 return -EINVAL;
279}
280
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200281static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
282{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200283 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200284
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200285 l1tf = vmentry_l1d_flush_parse(s);
286 if (l1tf < 0)
287 return l1tf;
288
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200289 if (!boot_cpu_has(X86_BUG_L1TF))
290 return 0;
291
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200292 /*
293 * Has vmx_init() run already? If not then this is the pre init
294 * parameter parsing. In that case just store the value and let
295 * vmx_init() do the proper setup after enable_ept has been
296 * established.
297 */
298 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
299 vmentry_l1d_flush_param = l1tf;
300 return 0;
301 }
302
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200303 mutex_lock(&vmx_l1d_flush_mutex);
304 ret = vmx_setup_l1d_flush(l1tf);
305 mutex_unlock(&vmx_l1d_flush_mutex);
306 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200307}
308
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200309static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
310{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200311 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
312 return sprintf(s, "???\n");
313
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200314 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200315}
316
317static const struct kernel_param_ops vmentry_l1d_flush_ops = {
318 .set = vmentry_l1d_flush_set,
319 .get = vmentry_l1d_flush_get,
320};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200321module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200322
Gleb Natapovd99e4152012-12-20 16:57:45 +0200323static bool guest_state_valid(struct kvm_vcpu *vcpu);
324static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yi Wang1e4329ee2018-11-08 11:22:21 +0800325static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +0100326 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300327
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800329DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300330/*
331 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
332 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
333 */
334static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335
Feng Wubf9f6ac2015-09-18 22:29:55 +0800336/*
337 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
338 * can find which vCPU should be waken up.
339 */
340static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
341static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
342
Sheng Yang2384d2b2008-01-17 15:14:33 +0800343static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
344static DEFINE_SPINLOCK(vmx_vpid_lock);
345
Sean Christopherson3077c192018-12-03 13:53:02 -0800346struct vmcs_config vmcs_config;
347struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800348
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349#define VMX_SEGMENT_FIELD(seg) \
350 [VCPU_SREG_##seg] = { \
351 .selector = GUEST_##seg##_SELECTOR, \
352 .base = GUEST_##seg##_BASE, \
353 .limit = GUEST_##seg##_LIMIT, \
354 .ar_bytes = GUEST_##seg##_AR_BYTES, \
355 }
356
Mathias Krause772e0312012-08-30 01:30:19 +0200357static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358 unsigned selector;
359 unsigned base;
360 unsigned limit;
361 unsigned ar_bytes;
362} kvm_vmx_segment_fields[] = {
363 VMX_SEGMENT_FIELD(CS),
364 VMX_SEGMENT_FIELD(DS),
365 VMX_SEGMENT_FIELD(ES),
366 VMX_SEGMENT_FIELD(FS),
367 VMX_SEGMENT_FIELD(GS),
368 VMX_SEGMENT_FIELD(SS),
369 VMX_SEGMENT_FIELD(TR),
370 VMX_SEGMENT_FIELD(LDTR),
371};
372
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800373u64 host_efer;
Avi Kivity26bb0982009-09-07 11:14:12 +0300374
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300375/*
Brian Gerst8c065852010-07-17 09:03:26 -0400376 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300377 * away by decrementing the array size.
378 */
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800379const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800380#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300381 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400383 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100386#if IS_ENABLED(CONFIG_HYPERV)
387static bool __read_mostly enlightened_vmcs = true;
388module_param(enlightened_vmcs, bool, 0444);
389
Tianyu Lan877ad952018-07-19 08:40:23 +0000390/* check_ept_pointer() should be under protection of ept_pointer_lock. */
391static void check_ept_pointer_match(struct kvm *kvm)
392{
393 struct kvm_vcpu *vcpu;
394 u64 tmp_eptp = INVALID_PAGE;
395 int i;
396
397 kvm_for_each_vcpu(i, vcpu, kvm) {
398 if (!VALID_PAGE(tmp_eptp)) {
399 tmp_eptp = to_vmx(vcpu)->ept_pointer;
400 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
401 to_kvm_vmx(kvm)->ept_pointers_match
402 = EPT_POINTERS_MISMATCH;
403 return;
404 }
405 }
406
407 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
408}
409
410static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
411{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800412 struct kvm_vcpu *vcpu;
413 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000414
415 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
416
417 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
418 check_ept_pointer_match(kvm);
419
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200420 /*
421 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
422 * base of EPT PML4 table, strip off EPT configuration information.
423 */
Tianyu Lan877ad952018-07-19 08:40:23 +0000424 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +0800425 kvm_for_each_vcpu(i, vcpu, kvm)
426 ret |= hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -0700427 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
Lan Tianyua5c214d2018-10-13 22:54:05 +0800428 } else {
429 ret = hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -0700430 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
Tianyu Lan877ad952018-07-19 08:40:23 +0000431 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000432
Tianyu Lan877ad952018-07-19 08:40:23 +0000433 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
434 return ret;
435}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100436#endif /* IS_ENABLED(CONFIG_HYPERV) */
437
Yunhong Jiang64672c92016-06-13 14:19:59 -0700438/*
439 * Comment's format: document - errata name - stepping - processor name.
440 * Refer from
441 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
442 */
443static u32 vmx_preemption_cpu_tfms[] = {
444/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
4450x000206E6,
446/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
447/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
448/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
4490x00020652,
450/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
4510x00020655,
452/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
453/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
454/*
455 * 320767.pdf - AAP86 - B1 -
456 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
457 */
4580x000106E5,
459/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
4600x000106A0,
461/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
4620x000106A1,
463/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
4640x000106A4,
465 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
466 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
467 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
4680x000106A5,
469};
470
471static inline bool cpu_has_broken_vmx_preemption_timer(void)
472{
473 u32 eax = cpuid_eax(0x00000001), i;
474
475 /* Clear the reserved bits */
476 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000477 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700478 if (eax == vmx_preemption_cpu_tfms[i])
479 return true;
480
481 return false;
482}
483
Paolo Bonzini35754c92015-07-29 12:05:37 +0200484static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800485{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200486 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800487}
488
Sheng Yang04547152009-04-01 15:52:31 +0800489static inline bool report_flexpriority(void)
490{
491 return flexpriority_enabled;
492}
493
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800494static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800495{
496 int i;
497
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400498 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300499 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300500 return i;
501 return -1;
502}
503
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800504struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300505{
506 int i;
507
Rusty Russell8b9cf982007-07-30 16:31:43 +1000508 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300509 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400510 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000511 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800512}
513
Sean Christopherson7c97fcb2018-12-03 13:53:17 -0800514void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
515{
516 vmcs_clear(loaded_vmcs->vmcs);
517 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
518 vmcs_clear(loaded_vmcs->shadow_vmcs);
519 loaded_vmcs->cpu = -1;
520 loaded_vmcs->launched = 0;
521}
522
Dave Young2965faa2015-09-09 15:38:55 -0700523#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800524/*
525 * This bitmap is used to indicate whether the vmclear
526 * operation is enabled on all cpus. All disabled by
527 * default.
528 */
529static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
530
531static inline void crash_enable_local_vmclear(int cpu)
532{
533 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
534}
535
536static inline void crash_disable_local_vmclear(int cpu)
537{
538 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
539}
540
541static inline int crash_local_vmclear_enabled(int cpu)
542{
543 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
544}
545
546static void crash_vmclear_local_loaded_vmcss(void)
547{
548 int cpu = raw_smp_processor_id();
549 struct loaded_vmcs *v;
550
551 if (!crash_local_vmclear_enabled(cpu))
552 return;
553
554 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
555 loaded_vmcss_on_cpu_link)
556 vmcs_clear(v->vmcs);
557}
558#else
559static inline void crash_enable_local_vmclear(int cpu) { }
560static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -0700561#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800562
Nadav Har'Eld462b812011-05-24 15:26:10 +0300563static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300565 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800566 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567
Nadav Har'Eld462b812011-05-24 15:26:10 +0300568 if (loaded_vmcs->cpu != cpu)
569 return; /* vcpu migration can race with cpu offline */
570 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800572 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300573 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800574
575 /*
576 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
577 * is before setting loaded_vmcs->vcpu to -1 which is done in
578 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
579 * then adds the vmcs into percpu list before it is deleted.
580 */
581 smp_wmb();
582
Nadav Har'Eld462b812011-05-24 15:26:10 +0300583 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800584 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585}
586
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800587void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800588{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800589 int cpu = loaded_vmcs->cpu;
590
591 if (cpu != -1)
592 smp_call_function_single(cpu,
593 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800594}
595
Avi Kivity2fb92db2011-04-27 19:42:18 +0300596static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
597 unsigned field)
598{
599 bool ret;
600 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
601
602 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
603 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
604 vmx->segment_cache.bitmask = 0;
605 }
606 ret = vmx->segment_cache.bitmask & mask;
607 vmx->segment_cache.bitmask |= mask;
608 return ret;
609}
610
611static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
612{
613 u16 *p = &vmx->segment_cache.seg[seg].selector;
614
615 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
616 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
617 return *p;
618}
619
620static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
621{
622 ulong *p = &vmx->segment_cache.seg[seg].base;
623
624 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
625 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
626 return *p;
627}
628
629static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
630{
631 u32 *p = &vmx->segment_cache.seg[seg].limit;
632
633 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
634 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
635 return *p;
636}
637
638static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
639{
640 u32 *p = &vmx->segment_cache.seg[seg].ar;
641
642 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
643 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
644 return *p;
645}
646
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800647void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300648{
649 u32 eb;
650
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100651 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800652 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200653 /*
654 * Guest access to VMware backdoor ports could legitimately
655 * trigger #GP because of TSS I/O permission bitmap.
656 * We intercept those #GP and allow access to them anyway
657 * as VMware does.
658 */
659 if (enable_vmware_backdoor)
660 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100661 if ((vcpu->guest_debug &
662 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
663 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
664 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300665 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300666 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200667 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800668 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300669
670 /* When we are running a nested L2 guest and L1 specified for it a
671 * certain exception bitmap, we must trap the same exceptions and pass
672 * them to L1. When running L2, we will only handle the exceptions
673 * specified above if L1 did not want them.
674 */
675 if (is_guest_mode(vcpu))
676 eb |= get_vmcs12(vcpu)->exception_bitmap;
677
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300678 vmcs_write32(EXCEPTION_BITMAP, eb);
679}
680
Ashok Raj15d45072018-02-01 22:59:43 +0100681/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100682 * Check if MSR is intercepted for currently loaded MSR bitmap.
683 */
684static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
685{
686 unsigned long *msr_bitmap;
687 int f = sizeof(unsigned long);
688
689 if (!cpu_has_vmx_msr_bitmap())
690 return true;
691
692 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
693
694 if (msr <= 0x1fff) {
695 return !!test_bit(msr, msr_bitmap + 0x800 / f);
696 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
697 msr &= 0x1fff;
698 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
699 }
700
701 return true;
702}
703
Gleb Natapov2961e8762013-11-25 15:37:13 +0200704static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
705 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200706{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200707 vm_entry_controls_clearbit(vmx, entry);
708 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200709}
710
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400711static int find_msr(struct vmx_msrs *m, unsigned int msr)
712{
713 unsigned int i;
714
715 for (i = 0; i < m->nr; ++i) {
716 if (m->val[i].index == msr)
717 return i;
718 }
719 return -ENOENT;
720}
721
Avi Kivity61d2ef22010-04-28 16:40:38 +0300722static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
723{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400724 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300725 struct msr_autoload *m = &vmx->msr_autoload;
726
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200727 switch (msr) {
728 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800729 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200730 clear_atomic_switch_msr_special(vmx,
731 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200732 VM_EXIT_LOAD_IA32_EFER);
733 return;
734 }
735 break;
736 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800737 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200738 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200739 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
740 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
741 return;
742 }
743 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200744 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400745 i = find_msr(&m->guest, msr);
746 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400747 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400748 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400749 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400750 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200751
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400752skip_guest:
753 i = find_msr(&m->host, msr);
754 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300755 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400756
757 --m->host.nr;
758 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400759 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300760}
761
Gleb Natapov2961e8762013-11-25 15:37:13 +0200762static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
763 unsigned long entry, unsigned long exit,
764 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
765 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200766{
767 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700768 if (host_val_vmcs != HOST_IA32_EFER)
769 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200770 vm_entry_controls_setbit(vmx, entry);
771 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200772}
773
Avi Kivity61d2ef22010-04-28 16:40:38 +0300774static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400775 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300776{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400777 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300778 struct msr_autoload *m = &vmx->msr_autoload;
779
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200780 switch (msr) {
781 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800782 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200783 add_atomic_switch_msr_special(vmx,
784 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200785 VM_EXIT_LOAD_IA32_EFER,
786 GUEST_IA32_EFER,
787 HOST_IA32_EFER,
788 guest_val, host_val);
789 return;
790 }
791 break;
792 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800793 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200794 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200795 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
796 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
797 GUEST_IA32_PERF_GLOBAL_CTRL,
798 HOST_IA32_PERF_GLOBAL_CTRL,
799 guest_val, host_val);
800 return;
801 }
802 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100803 case MSR_IA32_PEBS_ENABLE:
804 /* PEBS needs a quiescent period after being disabled (to write
805 * a record). Disabling PEBS through VMX MSR swapping doesn't
806 * provide that period, so a CPU could write host's record into
807 * guest's memory.
808 */
809 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200810 }
811
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400812 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400813 if (!entry_only)
814 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300815
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400816 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200817 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200818 "Can't add msr %x\n", msr);
819 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300820 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400821 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400822 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400823 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400824 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400825 m->guest.val[i].index = msr;
826 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300827
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400828 if (entry_only)
829 return;
830
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400831 if (j < 0) {
832 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400833 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300834 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400835 m->host.val[j].index = msr;
836 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300837}
838
Avi Kivity92c0d902009-10-29 11:00:16 +0200839static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300840{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100841 u64 guest_efer = vmx->vcpu.arch.efer;
842 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +0300843
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100844 if (!enable_ept) {
845 /*
846 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
847 * host CPUID is more efficient than testing guest CPUID
848 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
849 */
850 if (boot_cpu_has(X86_FEATURE_SMEP))
851 guest_efer |= EFER_NX;
852 else if (!(guest_efer & EFER_NX))
853 ignore_bits |= EFER_NX;
854 }
Roel Kluin3a34a882009-08-04 02:08:45 -0700855
Avi Kivity51c6cf62007-08-29 03:48:05 +0300856 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100857 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +0300858 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100859 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300860#ifdef CONFIG_X86_64
861 ignore_bits |= EFER_LMA | EFER_LME;
862 /* SCE is meaningful only in long mode on Intel */
863 if (guest_efer & EFER_LMA)
864 ignore_bits &= ~(u64)EFER_SCE;
865#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +0300866
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800867 /*
868 * On EPT, we can't emulate NX, so we must switch EFER atomically.
869 * On CPUs that support "load IA32_EFER", always switch EFER
870 * atomically, since it's faster than switching it manually.
871 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800872 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800873 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +0300874 if (!(guest_efer & EFER_LMA))
875 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -0800876 if (guest_efer != host_efer)
877 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400878 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -0700879 else
880 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +0300881 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100882 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -0700883 clear_atomic_switch_msr(vmx, MSR_EFER);
884
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100885 guest_efer &= ~ignore_bits;
886 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300887
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100888 vmx->guest_msrs[efer_offset].data = guest_efer;
889 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
890
891 return true;
892 }
Avi Kivity51c6cf62007-08-29 03:48:05 +0300893}
894
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800895#ifdef CONFIG_X86_32
896/*
897 * On 32-bit kernels, VM exits still load the FS and GS bases from the
898 * VMCS rather than the segment table. KVM uses this helper to figure
899 * out the current bases to poke them into the VMCS before entry.
900 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200901static unsigned long segment_base(u16 selector)
902{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800903 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200904 unsigned long v;
905
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800906 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200907 return 0;
908
Thomas Garnier45fc8752017-03-14 10:05:08 -0700909 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200910
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800911 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200912 u16 ldt_selector = kvm_read_ldt();
913
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800914 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200915 return 0;
916
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800917 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200918 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800919 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200920 return v;
921}
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800922#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200923
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800924void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300925{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300926 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -0700927 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +0200928#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +0100929 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +0200930#endif
Sean Christophersone368b872018-07-23 12:32:41 -0700931 unsigned long fs_base, gs_base;
932 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +0300933 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300934
Sean Christophersond264ee02018-08-27 15:21:12 -0700935 vmx->req_immediate_exit = false;
936
Liran Alonf48b4712018-11-20 18:03:25 +0200937 /*
938 * Note that guest MSRs to be saved/restored can also be changed
939 * when guest state is loaded. This happens when guest transitions
940 * to/from long-mode by setting MSR_EFER.LMA.
941 */
942 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
943 vmx->guest_msrs_dirty = false;
944 for (i = 0; i < vmx->save_nmsrs; ++i)
945 kvm_set_shared_msr(vmx->guest_msrs[i].index,
946 vmx->guest_msrs[i].data,
947 vmx->guest_msrs[i].mask);
948
949 }
950
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700951 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +0300952 return;
953
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700954 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700955 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700956
Avi Kivity33ed6322007-05-02 16:54:03 +0300957 /*
958 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
959 * allow segment selectors with cpl > 0 or ti == 1.
960 */
Sean Christophersond7ee0392018-07-23 12:32:47 -0700961 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +0100962
963#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -0700964 savesegment(ds, host_state->ds_sel);
965 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -0700966
967 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200968 if (likely(is_64bit_mm(current->mm))) {
969 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -0700970 fs_sel = current->thread.fsindex;
971 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200972 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -0700973 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200974 } else {
Sean Christophersone368b872018-07-23 12:32:41 -0700975 savesegment(fs, fs_sel);
976 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200977 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -0700978 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +0300979 }
980
Paolo Bonzini4679b612018-09-24 17:23:01 +0200981 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +0300982#else
Sean Christophersone368b872018-07-23 12:32:41 -0700983 savesegment(fs, fs_sel);
984 savesegment(gs, gs_sel);
985 fs_base = segment_base(fs_sel);
986 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300987#endif
Sean Christophersone368b872018-07-23 12:32:41 -0700988
Sean Christopherson8f21a0b2018-07-23 12:32:49 -0700989 if (unlikely(fs_sel != host_state->fs_sel)) {
990 if (!(fs_sel & 7))
991 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
992 else
993 vmcs_write16(HOST_FS_SELECTOR, 0);
994 host_state->fs_sel = fs_sel;
995 }
996 if (unlikely(gs_sel != host_state->gs_sel)) {
997 if (!(gs_sel & 7))
998 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
999 else
1000 vmcs_write16(HOST_GS_SELECTOR, 0);
1001 host_state->gs_sel = gs_sel;
1002 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07001003 if (unlikely(fs_base != host_state->fs_base)) {
1004 vmcs_writel(HOST_FS_BASE, fs_base);
1005 host_state->fs_base = fs_base;
1006 }
1007 if (unlikely(gs_base != host_state->gs_base)) {
1008 vmcs_writel(HOST_GS_BASE, gs_base);
1009 host_state->gs_base = gs_base;
1010 }
Avi Kivity33ed6322007-05-02 16:54:03 +03001011}
1012
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001013static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001014{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001015 struct vmcs_host_state *host_state;
1016
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001017 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03001018 return;
1019
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001020 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001021 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001022
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001023 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001024 vmx->loaded_cpu_state = NULL;
1025
Avi Kivityc8770e72010-11-11 12:37:26 +02001026#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001027 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001028#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001029 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1030 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001031#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001032 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001033#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001034 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001035#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001036 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001037 if (host_state->fs_sel & 7)
1038 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001039#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001040 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1041 loadsegment(ds, host_state->ds_sel);
1042 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001043 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001044#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001045 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001046#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001047 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001048#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001049 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03001050}
1051
Sean Christopherson678e3152018-07-23 12:32:43 -07001052#ifdef CONFIG_X86_64
1053static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001054{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001055 preempt_disable();
1056 if (vmx->loaded_cpu_state)
1057 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1058 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001059 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001060}
1061
Sean Christopherson678e3152018-07-23 12:32:43 -07001062static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1063{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001064 preempt_disable();
1065 if (vmx->loaded_cpu_state)
1066 wrmsrl(MSR_KERNEL_GS_BASE, data);
1067 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001068 vmx->msr_guest_kernel_gs_base = data;
1069}
1070#endif
1071
Feng Wu28b835d2015-09-18 22:29:54 +08001072static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1073{
1074 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1075 struct pi_desc old, new;
1076 unsigned int dest;
1077
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001078 /*
1079 * In case of hot-plug or hot-unplug, we may have to undo
1080 * vmx_vcpu_pi_put even if there is no assigned device. And we
1081 * always keep PI.NDST up to date for simplicity: it makes the
1082 * code easier, and CPU migration is not a fast path.
1083 */
1084 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08001085 return;
1086
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001087 /*
1088 * First handle the simple case where no cmpxchg is necessary; just
1089 * allow posting non-urgent interrupts.
1090 *
1091 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
1092 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
1093 * expects the VCPU to be on the blocked_vcpu_list that matches
1094 * PI.NDST.
1095 */
1096 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
1097 vcpu->cpu == cpu) {
1098 pi_clear_sn(pi_desc);
1099 return;
1100 }
1101
1102 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08001103 do {
1104 old.control = new.control = pi_desc->control;
1105
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001106 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08001107
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001108 if (x2apic_enabled())
1109 new.ndst = dest;
1110 else
1111 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08001112
Feng Wu28b835d2015-09-18 22:29:54 +08001113 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02001114 } while (cmpxchg64(&pi_desc->control, old.control,
1115 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08001116}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001117
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118/*
1119 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1120 * vcpu mutex is already taken.
1121 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001122void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001124 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001125 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001127 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001128 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001129 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001130 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001131
1132 /*
1133 * Read loaded_vmcs->cpu should be before fetching
1134 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1135 * See the comments in __loaded_vmcs_clear().
1136 */
1137 smp_rmb();
1138
Nadav Har'Eld462b812011-05-24 15:26:10 +03001139 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1140 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001141 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001142 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001143 }
1144
1145 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1146 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1147 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01001148 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001149 }
1150
1151 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001152 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001153 unsigned long sysenter_esp;
1154
1155 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001156
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 /*
1158 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001159 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001161 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001162 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001163 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001165 /*
1166 * VM exits change the host TR limit to 0x67 after a VM
1167 * exit. This is okay, since 0x67 covers everything except
1168 * the IO bitmap and have have code to handle the IO bitmap
1169 * being lost after a VM exit.
1170 */
1171 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
1172
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1174 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001175
Nadav Har'Eld462b812011-05-24 15:26:10 +03001176 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177 }
Feng Wu28b835d2015-09-18 22:29:54 +08001178
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001179 /* Setup TSC multiplier */
1180 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001181 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1182 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001183
Feng Wu28b835d2015-09-18 22:29:54 +08001184 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001185 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08001186 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001187}
1188
1189static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
1190{
1191 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1192
1193 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08001194 !irq_remapping_cap(IRQ_POSTING_CAP) ||
1195 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08001196 return;
1197
1198 /* Set SN when the vCPU is preempted */
1199 if (vcpu->preempted)
1200 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201}
1202
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001203void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204{
Feng Wu28b835d2015-09-18 22:29:54 +08001205 vmx_vcpu_pi_put(vcpu);
1206
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001207 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208}
1209
Wanpeng Lif244dee2017-07-20 01:11:54 -07001210static bool emulation_required(struct kvm_vcpu *vcpu)
1211{
1212 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
1213}
1214
Avi Kivityedcafe32009-12-30 18:07:40 +02001215static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1216
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001217unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001219 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001220
Avi Kivity6de12732011-03-07 12:51:22 +02001221 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1222 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1223 rflags = vmcs_readl(GUEST_RFLAGS);
1224 if (to_vmx(vcpu)->rmode.vm86_active) {
1225 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1226 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1227 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1228 }
1229 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001230 }
Avi Kivity6de12732011-03-07 12:51:22 +02001231 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232}
1233
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001234void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235{
Wanpeng Lif244dee2017-07-20 01:11:54 -07001236 unsigned long old_rflags = vmx_get_rflags(vcpu);
1237
Avi Kivity6de12732011-03-07 12:51:22 +02001238 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1239 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001240 if (to_vmx(vcpu)->rmode.vm86_active) {
1241 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001242 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001243 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001245
1246 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
1247 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248}
1249
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001250u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001251{
1252 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1253 int ret = 0;
1254
1255 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001256 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001257 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001258 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001259
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001260 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001261}
1262
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001263void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001264{
1265 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1266 u32 interruptibility = interruptibility_old;
1267
1268 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1269
Jan Kiszka48005f62010-02-19 19:38:07 +01001270 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001271 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001272 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001273 interruptibility |= GUEST_INTR_STATE_STI;
1274
1275 if ((interruptibility != interruptibility_old))
1276 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1277}
1278
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1280{
1281 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001283 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001285 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286
Glauber Costa2809f5d2009-05-12 16:21:05 -04001287 /* skipping an emulated instruction also counts */
1288 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289}
1290
Wanpeng Licaa057a2018-03-12 04:53:03 -07001291static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1292{
1293 /*
1294 * Ensure that we clear the HLT state in the VMCS. We don't need to
1295 * explicitly skip the instruction because if the HLT state is set,
1296 * then the instruction is already executing and RIP has already been
1297 * advanced.
1298 */
1299 if (kvm_hlt_in_guest(vcpu->kvm) &&
1300 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1301 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1302}
1303
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001304static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001305{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001306 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001307 unsigned nr = vcpu->arch.exception.nr;
1308 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001309 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001310 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001311
Jim Mattsonda998b42018-10-16 14:29:22 -07001312 kvm_deliver_exception_payload(vcpu);
1313
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001314 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001315 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001316 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1317 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001318
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001319 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001320 int inc_eip = 0;
1321 if (kvm_exception_is_soft(nr))
1322 inc_eip = vcpu->arch.event_exit_inst_len;
1323 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001324 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001325 return;
1326 }
1327
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001328 WARN_ON_ONCE(vmx->emulation_required);
1329
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001330 if (kvm_exception_is_soft(nr)) {
1331 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1332 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001333 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1334 } else
1335 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1336
1337 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001338
1339 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001340}
1341
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001342static bool vmx_rdtscp_supported(void)
1343{
1344 return cpu_has_vmx_rdtscp();
1345}
1346
Mao, Junjiead756a12012-07-02 01:18:48 +00001347static bool vmx_invpcid_supported(void)
1348{
Junaid Shahideb4b2482018-06-27 14:59:14 -07001349 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00001350}
1351
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352/*
Eddie Donga75beee2007-05-17 18:55:15 +03001353 * Swap MSR entry in host/guest MSR entry array.
1354 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001355static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001356{
Avi Kivity26bb0982009-09-07 11:14:12 +03001357 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001358
1359 tmp = vmx->guest_msrs[to];
1360 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1361 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001362}
1363
1364/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001365 * Set up the vmcs to automatically save and restore system
1366 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1367 * mode, as fiddling with msrs is very expensive.
1368 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001369static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001370{
Avi Kivity26bb0982009-09-07 11:14:12 +03001371 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001372
Eddie Donga75beee2007-05-17 18:55:15 +03001373 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001374#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001375 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001376 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001377 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001378 move_msr_up(vmx, index, save_nmsrs++);
1379 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001380 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001381 move_msr_up(vmx, index, save_nmsrs++);
1382 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001383 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001384 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001385 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02001386 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001387 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001388 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001389 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001390 * if efer.sce is enabled.
1391 */
Brian Gerst8c065852010-07-17 09:03:26 -04001392 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001393 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001394 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001395 }
Eddie Donga75beee2007-05-17 18:55:15 +03001396#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001397 index = __find_msr_index(vmx, MSR_EFER);
1398 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001399 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001400
Avi Kivity26bb0982009-09-07 11:14:12 +03001401 vmx->save_nmsrs = save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +02001402 vmx->guest_msrs_dirty = true;
Avi Kivity58972972009-02-24 22:26:47 +02001403
Yang Zhang8d146952013-01-25 10:18:50 +08001404 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001405 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001406}
1407
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001408static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001410 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001412 if (is_guest_mode(vcpu) &&
1413 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1414 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
1415
1416 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417}
1418
Leonid Shatz326e7422018-11-06 12:14:25 +02001419static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001421 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1422 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001423
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001424 /*
1425 * We're here if L1 chose not to trap WRMSR to TSC. According
1426 * to the spec, this should set L1's TSC; The offset that L1
1427 * set for L2 remains unchanged, and still needs to be added
1428 * to the newly set TSC to get L2's TSC.
1429 */
1430 if (is_guest_mode(vcpu) &&
1431 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1432 g_tsc_offset = vmcs12->tsc_offset;
1433
1434 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1435 vcpu->arch.tsc_offset - g_tsc_offset,
1436 offset);
1437 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1438 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439}
1440
Nadav Har'El801d3422011-05-25 23:02:23 +03001441/*
1442 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1443 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1444 * all guests if the "nested" module option is off, and can also be disabled
1445 * for a single guest by disabling its VMX cpuid bit.
1446 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001447bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001448{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001449 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001450}
1451
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001452static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1453 uint64_t val)
1454{
1455 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1456
1457 return !(val & ~valid_bits);
1458}
1459
Tom Lendacky801e4592018-02-21 13:39:51 -06001460static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1461{
Paolo Bonzini13893092018-02-26 13:40:09 +01001462 switch (msr->index) {
1463 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1464 if (!nested)
1465 return 1;
1466 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
1467 default:
1468 return 1;
1469 }
1470
1471 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06001472}
1473
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001474/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 * Reads an msr value (of 'msr_index') into 'pdata'.
1476 * Returns 0 on success, non-0 otherwise.
1477 * Assumes vcpu_load() was already called.
1478 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001479static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001481 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001482 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001484 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001485#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001487 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 break;
1489 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001490 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001492 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001493 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001494 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001495#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001497 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001498 case MSR_IA32_SPEC_CTRL:
1499 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001500 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1501 return 1;
1502
1503 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1504 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01001505 case MSR_IA32_ARCH_CAPABILITIES:
1506 if (!msr_info->host_initiated &&
1507 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
1508 return 1;
1509 msr_info->data = to_vmx(vcpu)->arch_capabilities;
1510 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001512 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513 break;
1514 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001515 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 break;
1517 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001518 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001520 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001521 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001522 (!msr_info->host_initiated &&
1523 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001524 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001525 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001526 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001527 case MSR_IA32_MCG_EXT_CTL:
1528 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001529 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08001530 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01001531 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001532 msr_info->data = vcpu->arch.mcg_ext_ctl;
1533 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001534 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001535 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001536 break;
1537 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1538 if (!nested_vmx_allowed(vcpu))
1539 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001540 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1541 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08001542 case MSR_IA32_XSS:
1543 if (!vmx_xsaves_supported())
1544 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001545 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08001546 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001547 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001548 if (!msr_info->host_initiated &&
1549 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001550 return 1;
1551 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001553 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001554 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001555 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001556 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001558 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559 }
1560
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561 return 0;
1562}
1563
1564/*
1565 * Writes msr value into into the appropriate "register".
1566 * Returns 0 on success, non-0 otherwise.
1567 * Assumes vcpu_load() was already called.
1568 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001569static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001571 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001572 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001573 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08001574 u32 msr_index = msr_info->index;
1575 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03001576
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001578 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08001579 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03001580 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001581#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001583 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584 vmcs_writel(GUEST_FS_BASE, data);
1585 break;
1586 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001587 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 vmcs_writel(GUEST_GS_BASE, data);
1589 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001590 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001591 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001592 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593#endif
1594 case MSR_IA32_SYSENTER_CS:
1595 vmcs_write32(GUEST_SYSENTER_CS, data);
1596 break;
1597 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001598 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 break;
1600 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001601 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001603 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001604 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001605 (!msr_info->host_initiated &&
1606 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001607 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001608 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07001609 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001611 vmcs_write64(GUEST_BNDCFGS, data);
1612 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001613 case MSR_IA32_SPEC_CTRL:
1614 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001615 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1616 return 1;
1617
1618 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02001619 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001620 return 1;
1621
1622 vmx->spec_ctrl = data;
1623
1624 if (!data)
1625 break;
1626
1627 /*
1628 * For non-nested:
1629 * When it's written (to non-zero) for the first time, pass
1630 * it through.
1631 *
1632 * For nested:
1633 * The handling of the MSR bitmap for L2 guests is done in
1634 * nested_vmx_merge_msr_bitmap. We should not touch the
1635 * vmcs02.msr_bitmap here since it gets completely overwritten
1636 * in the merging. We update the vmcs01 here for L1 as well
1637 * since it will end up touching the MSR anyway now.
1638 */
1639 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
1640 MSR_IA32_SPEC_CTRL,
1641 MSR_TYPE_RW);
1642 break;
Ashok Raj15d45072018-02-01 22:59:43 +01001643 case MSR_IA32_PRED_CMD:
1644 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01001645 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1646 return 1;
1647
1648 if (data & ~PRED_CMD_IBPB)
1649 return 1;
1650
1651 if (!data)
1652 break;
1653
1654 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
1655
1656 /*
1657 * For non-nested:
1658 * When it's written (to non-zero) for the first time, pass
1659 * it through.
1660 *
1661 * For nested:
1662 * The handling of the MSR bitmap for L2 guests is done in
1663 * nested_vmx_merge_msr_bitmap. We should not touch the
1664 * vmcs02.msr_bitmap here since it gets completely overwritten
1665 * in the merging.
1666 */
1667 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
1668 MSR_TYPE_W);
1669 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01001670 case MSR_IA32_ARCH_CAPABILITIES:
1671 if (!msr_info->host_initiated)
1672 return 1;
1673 vmx->arch_capabilities = data;
1674 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001675 case MSR_IA32_CR_PAT:
1676 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03001677 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
1678 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001679 vmcs_write64(GUEST_IA32_PAT, data);
1680 vcpu->arch.pat = data;
1681 break;
1682 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001683 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001684 break;
Will Auldba904632012-11-29 12:42:50 -08001685 case MSR_IA32_TSC_ADJUST:
1686 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001687 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001688 case MSR_IA32_MCG_EXT_CTL:
1689 if ((!msr_info->host_initiated &&
1690 !(to_vmx(vcpu)->msr_ia32_feature_control &
1691 FEATURE_CONTROL_LMCE)) ||
1692 (data & ~MCG_EXT_CTL_LMCE_EN))
1693 return 1;
1694 vcpu->arch.mcg_ext_ctl = data;
1695 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001696 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001697 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08001698 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01001699 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
1700 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001701 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01001702 if (msr_info->host_initiated && data == 0)
1703 vmx_leave_nested(vcpu);
1704 break;
1705 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08001706 if (!msr_info->host_initiated)
1707 return 1; /* they are read-only */
1708 if (!nested_vmx_allowed(vcpu))
1709 return 1;
1710 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08001711 case MSR_IA32_XSS:
1712 if (!vmx_xsaves_supported())
1713 return 1;
1714 /*
1715 * The only supported bit as of Skylake is bit 8, but
1716 * it is not supported on KVM.
1717 */
1718 if (data != 0)
1719 return 1;
1720 vcpu->arch.ia32_xss = data;
1721 if (vcpu->arch.ia32_xss != host_xss)
1722 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001723 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08001724 else
1725 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
1726 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001727 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001728 if (!msr_info->host_initiated &&
1729 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001730 return 1;
1731 /* Check reserved bit, higher 32 bits should be zero */
1732 if ((data >> 32) != 0)
1733 return 1;
1734 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001736 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001737 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07001738 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001739 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03001740 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
1741 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07001742 ret = kvm_set_shared_msr(msr->index, msr->data,
1743 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03001744 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07001745 if (ret)
1746 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03001747 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001748 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001750 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751 }
1752
Eddie Dong2cc51562007-05-21 07:28:09 +03001753 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001754}
1755
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001756static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001758 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1759 switch (reg) {
1760 case VCPU_REGS_RSP:
1761 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1762 break;
1763 case VCPU_REGS_RIP:
1764 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1765 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001766 case VCPU_EXREG_PDPTR:
1767 if (enable_ept)
1768 ept_save_pdptrs(vcpu);
1769 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001770 default:
1771 break;
1772 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773}
1774
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775static __init int cpu_has_kvm_support(void)
1776{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001777 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778}
1779
1780static __init int vmx_disabled_by_bios(void)
1781{
1782 u64 msr;
1783
1784 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001785 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001786 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001787 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1788 && tboot_enabled())
1789 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001790 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001791 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001792 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001793 && !tboot_enabled()) {
1794 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001795 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001796 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001797 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001798 /* launched w/o TXT and VMX disabled */
1799 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1800 && !tboot_enabled())
1801 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001802 }
1803
1804 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805}
1806
Dongxiao Xu7725b892010-05-11 18:29:38 +08001807static void kvm_cpu_vmxon(u64 addr)
1808{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001809 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001810 intel_pt_handle_vmx(1);
1811
Uros Bizjak4b1e5472018-10-11 19:40:44 +02001812 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08001813}
1814
Radim Krčmář13a34e02014-08-28 15:13:03 +02001815static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816{
1817 int cpu = raw_smp_processor_id();
1818 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001819 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001821 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02001822 return -EBUSY;
1823
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001824 /*
1825 * This can happen if we hot-added a CPU but failed to allocate
1826 * VP assist page for it.
1827 */
1828 if (static_branch_unlikely(&enable_evmcs) &&
1829 !hv_get_vp_assist_page(cpu))
1830 return -EFAULT;
1831
Nadav Har'Eld462b812011-05-24 15:26:10 +03001832 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08001833 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
1834 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001835
1836 /*
1837 * Now we can enable the vmclear operation in kdump
1838 * since the loaded_vmcss_on_cpu list on this cpu
1839 * has been initialized.
1840 *
1841 * Though the cpu is not in VMX operation now, there
1842 * is no problem to enable the vmclear operation
1843 * for the loaded_vmcss_on_cpu list is empty!
1844 */
1845 crash_enable_local_vmclear(cpu);
1846
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001848
1849 test_bits = FEATURE_CONTROL_LOCKED;
1850 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1851 if (tboot_enabled())
1852 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1853
1854 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001856 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1857 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001858 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02001859 if (enable_ept)
1860 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02001861
1862 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863}
1864
Nadav Har'Eld462b812011-05-24 15:26:10 +03001865static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03001866{
1867 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001868 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03001869
Nadav Har'Eld462b812011-05-24 15:26:10 +03001870 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
1871 loaded_vmcss_on_cpu_link)
1872 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03001873}
1874
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001875
1876/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1877 * tricks.
1878 */
1879static void kvm_cpu_vmxoff(void)
1880{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02001881 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001882
1883 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001884 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001885}
1886
Radim Krčmář13a34e02014-08-28 15:13:03 +02001887static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001888{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001889 vmclear_local_loaded_vmcss();
1890 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891}
1892
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001893static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001894 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895{
1896 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001897 u32 ctl = ctl_min | ctl_opt;
1898
1899 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1900
1901 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1902 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1903
1904 /* Ensure minimum (required) set of control bits are supported. */
1905 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001906 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001907
1908 *result = ctl;
1909 return 0;
1910}
1911
Sean Christopherson7caaa712018-12-03 13:53:01 -08001912static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
1913 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001914{
1915 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001916 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001917 u32 _pin_based_exec_control = 0;
1918 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001919 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001920 u32 _vmexit_control = 0;
1921 u32 _vmentry_control = 0;
1922
Paolo Bonzini13893092018-02-26 13:40:09 +01001923 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05301924 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001925#ifdef CONFIG_X86_64
1926 CPU_BASED_CR8_LOAD_EXITING |
1927 CPU_BASED_CR8_STORE_EXITING |
1928#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001929 CPU_BASED_CR3_LOAD_EXITING |
1930 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08001931 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001932 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001933 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001934 CPU_BASED_MWAIT_EXITING |
1935 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02001936 CPU_BASED_INVLPG_EXITING |
1937 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001938
Sheng Yangf78e0e22007-10-29 09:40:42 +08001939 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001940 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001941 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001942 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1943 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001944 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001945#ifdef CONFIG_X86_64
1946 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1947 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1948 ~CPU_BASED_CR8_STORE_EXITING;
1949#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001950 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001951 min2 = 0;
1952 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08001953 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001954 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001955 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001956 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001957 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001958 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02001959 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00001960 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08001961 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08001962 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03001963 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08001964 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08001965 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02001966 SECONDARY_EXEC_RDSEED_EXITING |
1967 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08001968 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04001969 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07001970 SECONDARY_EXEC_ENABLE_VMFUNC |
1971 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001972 if (adjust_vmx_controls(min2, opt2,
1973 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001974 &_cpu_based_2nd_exec_control) < 0)
1975 return -EIO;
1976 }
1977#ifndef CONFIG_X86_64
1978 if (!(_cpu_based_2nd_exec_control &
1979 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1980 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1981#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08001982
1983 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1984 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08001985 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08001986 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
1987 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08001988
Wanpeng Li61f1dd92017-10-18 16:02:19 -07001989 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08001990 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07001991
Sheng Yangd56f5462008-04-25 10:13:16 +08001992 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001993 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1994 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001995 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1996 CPU_BASED_CR3_STORE_EXITING |
1997 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08001998 } else if (vmx_cap->ept) {
1999 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002000 pr_warn_once("EPT CAP should not exist if not support "
2001 "1-setting enable EPT VM-execution control\n");
2002 }
2003 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002004 vmx_cap->vpid) {
2005 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002006 pr_warn_once("VPID CAP should not exist if not support "
2007 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002008 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002009
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002010 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002011#ifdef CONFIG_X86_64
2012 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2013#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002014 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
2015 VM_EXIT_SAVE_IA32_PAT |
2016 VM_EXIT_LOAD_IA32_PAT |
2017 VM_EXIT_LOAD_IA32_EFER |
2018 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002019 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2020 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002021 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002022
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002023 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2024 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2025 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002026 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2027 &_pin_based_exec_control) < 0)
2028 return -EIO;
2029
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002030 if (cpu_has_broken_vmx_preemption_timer())
2031 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002032 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002033 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002034 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2035
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002036 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002037 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2038 VM_ENTRY_LOAD_IA32_PAT |
2039 VM_ENTRY_LOAD_IA32_EFER |
2040 VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002041 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2042 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002043 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002045 /*
2046 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2047 * can't be used due to an errata where VM Exit may incorrectly clear
2048 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2049 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2050 */
2051 if (boot_cpu_data.x86 == 0x6) {
2052 switch (boot_cpu_data.x86_model) {
2053 case 26: /* AAK155 */
2054 case 30: /* AAP115 */
2055 case 37: /* AAT100 */
2056 case 44: /* BC86,AAY89,BD102 */
2057 case 46: /* BA97 */
2058 _vmexit_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
2059 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2060 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2061 "does not work properly. Using workaround\n");
2062 break;
2063 default:
2064 break;
2065 }
2066 }
2067
2068
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002069 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002070
2071 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2072 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002073 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002074
2075#ifdef CONFIG_X86_64
2076 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2077 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002078 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002079#endif
2080
2081 /* Require Write-Back (WB) memory type for VMCS accesses. */
2082 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002083 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002084
Yang, Sheng002c7f72007-07-31 14:23:01 +03002085 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002086 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002087 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002088
Liran Alon2307af12018-06-29 22:59:04 +03002089 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002090
Yang, Sheng002c7f72007-07-31 14:23:01 +03002091 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2092 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002093 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002094 vmcs_conf->vmexit_ctrl = _vmexit_control;
2095 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002096
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002097 if (static_branch_unlikely(&enable_evmcs))
2098 evmcs_sanitize_exec_ctrls(vmcs_conf);
2099
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002100 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002101}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002103struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104{
2105 int node = cpu_to_node(cpu);
2106 struct page *pages;
2107 struct vmcs *vmcs;
2108
Vlastimil Babka96db8002015-09-08 15:03:50 -07002109 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 if (!pages)
2111 return NULL;
2112 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002113 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002114
2115 /* KVM supports Enlightened VMCS v1 only */
2116 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002117 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002118 else
Liran Alon392b2f22018-06-23 02:35:01 +03002119 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002120
Liran Alon491a6032018-06-23 02:35:12 +03002121 if (shadow)
2122 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 return vmcs;
2124}
2125
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002126void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002128 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129}
2130
Nadav Har'Eld462b812011-05-24 15:26:10 +03002131/*
2132 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2133 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002134void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002135{
2136 if (!loaded_vmcs->vmcs)
2137 return;
2138 loaded_vmcs_clear(loaded_vmcs);
2139 free_vmcs(loaded_vmcs->vmcs);
2140 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002141 if (loaded_vmcs->msr_bitmap)
2142 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002143 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002144}
2145
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002146int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002147{
Liran Alon491a6032018-06-23 02:35:12 +03002148 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002149 if (!loaded_vmcs->vmcs)
2150 return -ENOMEM;
2151
2152 loaded_vmcs->shadow_vmcs = NULL;
2153 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002154
2155 if (cpu_has_vmx_msr_bitmap()) {
2156 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
2157 if (!loaded_vmcs->msr_bitmap)
2158 goto out_vmcs;
2159 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002160
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002161 if (IS_ENABLED(CONFIG_HYPERV) &&
2162 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002163 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2164 struct hv_enlightened_vmcs *evmcs =
2165 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2166
2167 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2168 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002169 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002170
2171 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
2172
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002173 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002174
2175out_vmcs:
2176 free_loaded_vmcs(loaded_vmcs);
2177 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002178}
2179
Sam Ravnborg39959582007-06-01 00:47:13 -07002180static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181{
2182 int cpu;
2183
Zachary Amsden3230bb42009-09-29 11:38:37 -10002184 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002186 per_cpu(vmxarea, cpu) = NULL;
2187 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188}
2189
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190static __init int alloc_kvm_area(void)
2191{
2192 int cpu;
2193
Zachary Amsden3230bb42009-09-29 11:38:37 -10002194 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 struct vmcs *vmcs;
2196
Liran Alon491a6032018-06-23 02:35:12 +03002197 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 if (!vmcs) {
2199 free_kvm_area();
2200 return -ENOMEM;
2201 }
2202
Liran Alon2307af12018-06-29 22:59:04 +03002203 /*
2204 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2205 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2206 * revision_id reported by MSR_IA32_VMX_BASIC.
2207 *
2208 * However, even though not explictly documented by
2209 * TLFS, VMXArea passed as VMXON argument should
2210 * still be marked with revision_id reported by
2211 * physical CPU.
2212 */
2213 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002214 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002215
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216 per_cpu(vmxarea, cpu) = vmcs;
2217 }
2218 return 0;
2219}
2220
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002221static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002222 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002224 if (!emulate_invalid_guest_state) {
2225 /*
2226 * CS and SS RPL should be equal during guest entry according
2227 * to VMX spec, but in reality it is not always so. Since vcpu
2228 * is in the middle of the transition from real mode to
2229 * protected mode it is safe to assume that RPL 0 is a good
2230 * default value.
2231 */
2232 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002233 save->selector &= ~SEGMENT_RPL_MASK;
2234 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002235 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002237 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238}
2239
2240static void enter_pmode(struct kvm_vcpu *vcpu)
2241{
2242 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002243 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244
Gleb Natapovd99e4152012-12-20 16:57:45 +02002245 /*
2246 * Update real mode segment cache. It may be not up-to-date if sement
2247 * register was written while vcpu was in a guest mode.
2248 */
2249 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2250 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2251 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2252 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2253 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2254 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2255
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002256 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257
Avi Kivity2fb92db2011-04-27 19:42:18 +03002258 vmx_segment_cache_clear(vmx);
2259
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002260 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261
2262 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002263 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2264 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 vmcs_writel(GUEST_RFLAGS, flags);
2266
Rusty Russell66aee912007-07-17 23:34:16 +10002267 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2268 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269
2270 update_exception_bitmap(vcpu);
2271
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002272 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2273 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2274 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2275 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2276 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2277 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278}
2279
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002280static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281{
Mathias Krause772e0312012-08-30 01:30:19 +02002282 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002283 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284
Gleb Natapovd99e4152012-12-20 16:57:45 +02002285 var.dpl = 0x3;
2286 if (seg == VCPU_SREG_CS)
2287 var.type = 0x3;
2288
2289 if (!emulate_invalid_guest_state) {
2290 var.selector = var.base >> 4;
2291 var.base = var.base & 0xffff0;
2292 var.limit = 0xffff;
2293 var.g = 0;
2294 var.db = 0;
2295 var.present = 1;
2296 var.s = 1;
2297 var.l = 0;
2298 var.unusable = 0;
2299 var.type = 0x3;
2300 var.avl = 0;
2301 if (save->base & 0xf)
2302 printk_once(KERN_WARNING "kvm: segment base is not "
2303 "paragraph aligned when entering "
2304 "protected mode (seg=%d)", seg);
2305 }
2306
2307 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002308 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002309 vmcs_write32(sf->limit, var.limit);
2310 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311}
2312
2313static void enter_rmode(struct kvm_vcpu *vcpu)
2314{
2315 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002316 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002317 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002319 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2320 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2321 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2322 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2323 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002324 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2325 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002326
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002327 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328
Gleb Natapov776e58e2011-03-13 12:34:27 +02002329 /*
2330 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002331 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002332 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002333 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002334 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2335 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002336
Avi Kivity2fb92db2011-04-27 19:42:18 +03002337 vmx_segment_cache_clear(vmx);
2338
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002339 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2342
2343 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002344 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002346 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347
2348 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002349 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 update_exception_bitmap(vcpu);
2351
Gleb Natapovd99e4152012-12-20 16:57:45 +02002352 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2353 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2354 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2355 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2356 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2357 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002358
Eddie Dong8668a3c2007-10-10 14:26:45 +08002359 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360}
2361
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002362void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302363{
2364 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002365 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2366
2367 if (!msr)
2368 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302369
Avi Kivityf6801df2010-01-21 15:31:50 +02002370 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302371 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002372 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302373 msr->data = efer;
2374 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002375 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302376
2377 msr->data = efer & ~EFER_LME;
2378 }
2379 setup_msrs(vmx);
2380}
2381
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002382#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383
2384static void enter_lmode(struct kvm_vcpu *vcpu)
2385{
2386 u32 guest_tr_ar;
2387
Avi Kivity2fb92db2011-04-27 19:42:18 +03002388 vmx_segment_cache_clear(to_vmx(vcpu));
2389
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002391 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002392 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2393 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002395 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2396 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 }
Avi Kivityda38f432010-07-06 11:30:49 +03002398 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399}
2400
2401static void exit_lmode(struct kvm_vcpu *vcpu)
2402{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002403 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002404 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405}
2406
2407#endif
2408
Junaid Shahidfaff8752018-06-29 13:10:05 -07002409static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2410{
2411 int vpid = to_vmx(vcpu)->vpid;
2412
2413 if (!vpid_sync_vcpu_addr(vpid, addr))
2414 vpid_sync_context(vpid);
2415
2416 /*
2417 * If VPIDs are not supported or enabled, then the above is a no-op.
2418 * But we don't really need a TLB flush in that case anyway, because
2419 * each VM entry/exit includes an implicit flush when VPID is 0.
2420 */
2421}
2422
Avi Kivitye8467fd2009-12-29 18:43:06 +02002423static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2424{
2425 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2426
2427 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2428 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2429}
2430
Avi Kivityaff48ba2010-12-05 18:56:11 +02002431static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2432{
Sean Christophersonb4d18512018-03-05 12:04:40 -08002433 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02002434 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2435 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2436}
2437
Anthony Liguori25c4c272007-04-27 09:29:21 +03002438static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002439{
Avi Kivityfc78f512009-12-07 12:16:48 +02002440 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2441
2442 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2443 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002444}
2445
Sheng Yang14394422008-04-28 12:24:45 +08002446static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2447{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002448 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2449
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002450 if (!test_bit(VCPU_EXREG_PDPTR,
2451 (unsigned long *)&vcpu->arch.regs_dirty))
2452 return;
2453
Sheng Yang14394422008-04-28 12:24:45 +08002454 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002455 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2456 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2457 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2458 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002459 }
2460}
2461
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002462void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002463{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002464 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2465
Avi Kivity8f5d5492009-05-31 18:41:29 +03002466 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002467 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2468 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2469 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2470 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002471 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002472
2473 __set_bit(VCPU_EXREG_PDPTR,
2474 (unsigned long *)&vcpu->arch.regs_avail);
2475 __set_bit(VCPU_EXREG_PDPTR,
2476 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002477}
2478
Sheng Yang14394422008-04-28 12:24:45 +08002479static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2480 unsigned long cr0,
2481 struct kvm_vcpu *vcpu)
2482{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002483 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2484 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002485 if (!(cr0 & X86_CR0_PG)) {
2486 /* From paging/starting to nonpaging */
2487 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002488 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002489 (CPU_BASED_CR3_LOAD_EXITING |
2490 CPU_BASED_CR3_STORE_EXITING));
2491 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002492 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002493 } else if (!is_paging(vcpu)) {
2494 /* From nonpaging to paging */
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 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002502
2503 if (!(cr0 & X86_CR0_WP))
2504 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002505}
2506
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002507void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002509 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002510 unsigned long hw_cr0;
2511
Sean Christopherson3de63472018-07-13 08:42:30 -07002512 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002513 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02002514 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02002515 else {
Gleb Natapov50378782013-02-04 16:00:28 +02002516 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002517
Gleb Natapov218e7632013-01-21 15:36:45 +02002518 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
2519 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520
Gleb Natapov218e7632013-01-21 15:36:45 +02002521 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
2522 enter_rmode(vcpu);
2523 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002525#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002526 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002527 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10002529 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 exit_lmode(vcpu);
2531 }
2532#endif
2533
Sean Christophersonb4d18512018-03-05 12:04:40 -08002534 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08002535 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2536
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002538 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002539 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02002540
2541 /* depends on vcpu->arch.cr0 to be set to a new value */
2542 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543}
2544
Yu Zhang855feb62017-08-24 20:27:55 +08002545static int get_ept_level(struct kvm_vcpu *vcpu)
2546{
2547 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
2548 return 5;
2549 return 4;
2550}
2551
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002552u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08002553{
Yu Zhang855feb62017-08-24 20:27:55 +08002554 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08002555
Yu Zhang855feb62017-08-24 20:27:55 +08002556 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08002557
Peter Feiner995f00a2017-06-30 17:26:32 -07002558 if (enable_ept_ad_bits &&
2559 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02002560 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08002561 eptp |= (root_hpa & PAGE_MASK);
2562
2563 return eptp;
2564}
2565
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002566void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567{
Tianyu Lan877ad952018-07-19 08:40:23 +00002568 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08002569 unsigned long guest_cr3;
2570 u64 eptp;
2571
2572 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002573 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07002574 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08002575 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00002576
2577 if (kvm_x86_ops->tlb_remote_flush) {
2578 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
2579 to_vmx(vcpu)->ept_pointer = eptp;
2580 to_kvm_vmx(kvm)->ept_pointers_match
2581 = EPT_POINTERS_CHECK;
2582 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
2583 }
2584
Sean Christophersone90008d2018-03-05 12:04:37 -08002585 if (enable_unrestricted_guest || is_paging(vcpu) ||
2586 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02002587 guest_cr3 = kvm_read_cr3(vcpu);
2588 else
Tianyu Lan877ad952018-07-19 08:40:23 +00002589 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02002590 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002591 }
2592
Sheng Yang14394422008-04-28 12:24:45 +08002593 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594}
2595
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002596int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597{
Ben Serebrin085e68e2015-04-16 11:58:05 -07002598 /*
2599 * Pass through host's Machine Check Enable value to hw_cr4, which
2600 * is in force while we are in guest mode. Do not let guests control
2601 * this bit, even if host CR4.MCE == 0.
2602 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002603 unsigned long hw_cr4;
2604
2605 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
2606 if (enable_unrestricted_guest)
2607 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
2608 else if (to_vmx(vcpu)->rmode.vm86_active)
2609 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
2610 else
2611 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002612
Sean Christopherson64f7a112018-04-30 10:01:06 -07002613 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
2614 if (cr4 & X86_CR4_UMIP) {
2615 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02002616 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07002617 hw_cr4 &= ~X86_CR4_UMIP;
2618 } else if (!is_guest_mode(vcpu) ||
2619 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
2620 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
2621 SECONDARY_EXEC_DESC);
2622 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02002623
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002624 if (cr4 & X86_CR4_VMXE) {
2625 /*
2626 * To use VMXON (and later other VMX instructions), a guest
2627 * must first be able to turn on cr4.VMXE (see handle_vmon()).
2628 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02002629 * is here. We operate under the default treatment of SMM,
2630 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002631 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02002632 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002633 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01002634 }
David Matlack38991522016-11-29 18:14:08 -08002635
2636 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002637 return 1;
2638
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002639 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08002640
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002641 if (!enable_unrestricted_guest) {
2642 if (enable_ept) {
2643 if (!is_paging(vcpu)) {
2644 hw_cr4 &= ~X86_CR4_PAE;
2645 hw_cr4 |= X86_CR4_PSE;
2646 } else if (!(cr4 & X86_CR4_PAE)) {
2647 hw_cr4 &= ~X86_CR4_PAE;
2648 }
2649 }
2650
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002651 /*
Huaitong Handdba2622016-03-22 16:51:15 +08002652 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
2653 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
2654 * to be manually disabled when guest switches to non-paging
2655 * mode.
2656 *
2657 * If !enable_unrestricted_guest, the CPU is always running
2658 * with CR0.PG=1 and CR4 needs to be modified.
2659 * If enable_unrestricted_guest, the CPU automatically
2660 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002661 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002662 if (!is_paging(vcpu))
2663 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
2664 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002665
Sheng Yang14394422008-04-28 12:24:45 +08002666 vmcs_writel(CR4_READ_SHADOW, cr4);
2667 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002668 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669}
2670
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002671void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672{
Avi Kivitya9179492011-01-03 14:28:52 +02002673 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 u32 ar;
2675
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002676 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002677 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02002678 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002679 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002680 return;
Avi Kivity1390a282012-08-21 17:07:08 +03002681 var->base = vmx_read_guest_seg_base(vmx, seg);
2682 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2683 return;
Avi Kivitya9179492011-01-03 14:28:52 +02002684 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002685 var->base = vmx_read_guest_seg_base(vmx, seg);
2686 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2687 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2688 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03002689 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 var->type = ar & 15;
2691 var->s = (ar >> 4) & 1;
2692 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03002693 /*
2694 * Some userspaces do not preserve unusable property. Since usable
2695 * segment has to be present according to VMX spec we can use present
2696 * property to amend userspace bug by making unusable segment always
2697 * nonpresent. vmx_segment_access_rights() already marks nonpresent
2698 * segment as unusable.
2699 */
2700 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 var->avl = (ar >> 12) & 1;
2702 var->l = (ar >> 13) & 1;
2703 var->db = (ar >> 14) & 1;
2704 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002705}
2706
Avi Kivitya9179492011-01-03 14:28:52 +02002707static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2708{
Avi Kivitya9179492011-01-03 14:28:52 +02002709 struct kvm_segment s;
2710
2711 if (to_vmx(vcpu)->rmode.vm86_active) {
2712 vmx_get_segment(vcpu, &s, seg);
2713 return s.base;
2714 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002715 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002716}
2717
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002718int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002719{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02002720 struct vcpu_vmx *vmx = to_vmx(vcpu);
2721
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002722 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002723 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002724 else {
2725 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002726 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002727 }
Avi Kivity69c73022011-03-07 15:26:44 +02002728}
2729
Avi Kivity653e3102007-05-07 10:55:37 +03002730static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 u32 ar;
2733
Avi Kivityf0495f92012-06-07 17:06:10 +03002734 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 ar = 1 << 16;
2736 else {
2737 ar = var->type & 15;
2738 ar |= (var->s & 1) << 4;
2739 ar |= (var->dpl & 3) << 5;
2740 ar |= (var->present & 1) << 7;
2741 ar |= (var->avl & 1) << 12;
2742 ar |= (var->l & 1) << 13;
2743 ar |= (var->db & 1) << 14;
2744 ar |= (var->g & 1) << 15;
2745 }
Avi Kivity653e3102007-05-07 10:55:37 +03002746
2747 return ar;
2748}
2749
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002750void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03002751{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002752 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02002753 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03002754
Avi Kivity2fb92db2011-04-27 19:42:18 +03002755 vmx_segment_cache_clear(vmx);
2756
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02002757 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
2758 vmx->rmode.segs[seg] = *var;
2759 if (seg == VCPU_SREG_TR)
2760 vmcs_write16(sf->selector, var->selector);
2761 else if (var->s)
2762 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002763 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03002764 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02002765
Avi Kivity653e3102007-05-07 10:55:37 +03002766 vmcs_writel(sf->base, var->base);
2767 vmcs_write32(sf->limit, var->limit);
2768 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002769
2770 /*
2771 * Fix the "Accessed" bit in AR field of segment registers for older
2772 * qemu binaries.
2773 * IA32 arch specifies that at the time of processor reset the
2774 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08002775 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002776 * state vmexit when "unrestricted guest" mode is turned on.
2777 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2778 * tree. Newer qemu binaries with that qemu fix would not need this
2779 * kvm hack.
2780 */
2781 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02002782 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002783
Gleb Natapovf924d662012-12-12 19:10:55 +02002784 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02002785
2786out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01002787 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788}
2789
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2791{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002792 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793
2794 *db = (ar >> 14) & 1;
2795 *l = (ar >> 13) & 1;
2796}
2797
Gleb Natapov89a27f42010-02-16 10:51:48 +02002798static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002800 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2801 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802}
2803
Gleb Natapov89a27f42010-02-16 10:51:48 +02002804static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002806 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2807 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808}
2809
Gleb Natapov89a27f42010-02-16 10:51:48 +02002810static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002812 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2813 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814}
2815
Gleb Natapov89a27f42010-02-16 10:51:48 +02002816static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002818 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2819 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820}
2821
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002822static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2823{
2824 struct kvm_segment var;
2825 u32 ar;
2826
2827 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02002828 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02002829 if (seg == VCPU_SREG_CS)
2830 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002831 ar = vmx_segment_access_rights(&var);
2832
2833 if (var.base != (var.selector << 4))
2834 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02002835 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002836 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02002837 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002838 return false;
2839
2840 return true;
2841}
2842
2843static bool code_segment_valid(struct kvm_vcpu *vcpu)
2844{
2845 struct kvm_segment cs;
2846 unsigned int cs_rpl;
2847
2848 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03002849 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002850
Avi Kivity1872a3f2009-01-04 23:26:52 +02002851 if (cs.unusable)
2852 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002853 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002854 return false;
2855 if (!cs.s)
2856 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002857 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002858 if (cs.dpl > cs_rpl)
2859 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002860 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002861 if (cs.dpl != cs_rpl)
2862 return false;
2863 }
2864 if (!cs.present)
2865 return false;
2866
2867 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2868 return true;
2869}
2870
2871static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2872{
2873 struct kvm_segment ss;
2874 unsigned int ss_rpl;
2875
2876 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03002877 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002878
Avi Kivity1872a3f2009-01-04 23:26:52 +02002879 if (ss.unusable)
2880 return true;
2881 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002882 return false;
2883 if (!ss.s)
2884 return false;
2885 if (ss.dpl != ss_rpl) /* DPL != RPL */
2886 return false;
2887 if (!ss.present)
2888 return false;
2889
2890 return true;
2891}
2892
2893static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2894{
2895 struct kvm_segment var;
2896 unsigned int rpl;
2897
2898 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03002899 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002900
Avi Kivity1872a3f2009-01-04 23:26:52 +02002901 if (var.unusable)
2902 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002903 if (!var.s)
2904 return false;
2905 if (!var.present)
2906 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002907 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002908 if (var.dpl < rpl) /* DPL < RPL */
2909 return false;
2910 }
2911
2912 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2913 * rights flags
2914 */
2915 return true;
2916}
2917
2918static bool tr_valid(struct kvm_vcpu *vcpu)
2919{
2920 struct kvm_segment tr;
2921
2922 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2923
Avi Kivity1872a3f2009-01-04 23:26:52 +02002924 if (tr.unusable)
2925 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03002926 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002927 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002928 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002929 return false;
2930 if (!tr.present)
2931 return false;
2932
2933 return true;
2934}
2935
2936static bool ldtr_valid(struct kvm_vcpu *vcpu)
2937{
2938 struct kvm_segment ldtr;
2939
2940 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2941
Avi Kivity1872a3f2009-01-04 23:26:52 +02002942 if (ldtr.unusable)
2943 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03002944 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002945 return false;
2946 if (ldtr.type != 2)
2947 return false;
2948 if (!ldtr.present)
2949 return false;
2950
2951 return true;
2952}
2953
2954static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2955{
2956 struct kvm_segment cs, ss;
2957
2958 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2959 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2960
Nadav Amitb32a9912015-03-29 16:33:04 +03002961 return ((cs.selector & SEGMENT_RPL_MASK) ==
2962 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002963}
2964
2965/*
2966 * Check if guest state is valid. Returns true if valid, false if
2967 * not.
2968 * We assume that registers are always usable
2969 */
2970static bool guest_state_valid(struct kvm_vcpu *vcpu)
2971{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02002972 if (enable_unrestricted_guest)
2973 return true;
2974
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002975 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03002976 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002977 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2978 return false;
2979 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2980 return false;
2981 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2982 return false;
2983 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2984 return false;
2985 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2986 return false;
2987 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2988 return false;
2989 } else {
2990 /* protected mode guest state checks */
2991 if (!cs_ss_rpl_check(vcpu))
2992 return false;
2993 if (!code_segment_valid(vcpu))
2994 return false;
2995 if (!stack_segment_valid(vcpu))
2996 return false;
2997 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2998 return false;
2999 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3000 return false;
3001 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3002 return false;
3003 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3004 return false;
3005 if (!tr_valid(vcpu))
3006 return false;
3007 if (!ldtr_valid(vcpu))
3008 return false;
3009 }
3010 /* TODO:
3011 * - Add checks on RIP
3012 * - Add checks on RFLAGS
3013 */
3014
3015 return true;
3016}
3017
Mike Dayd77c26f2007-10-08 09:02:08 -04003018static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003020 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003021 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003022 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003024 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003025 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003026 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3027 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003028 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003029 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003030 r = kvm_write_guest_page(kvm, fn++, &data,
3031 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003032 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003033 goto out;
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 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3038 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003039 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003040 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003041 r = kvm_write_guest_page(kvm, fn, &data,
3042 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3043 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003044out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003045 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003046 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047}
3048
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003049static int init_rmode_identity_map(struct kvm *kvm)
3050{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003051 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08003052 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003053 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003054 u32 tmp;
3055
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003056 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003057 mutex_lock(&kvm->slots_lock);
3058
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003059 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08003060 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08003061
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003062 if (!kvm_vmx->ept_identity_map_addr)
3063 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3064 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003065
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003066 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003067 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003068 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08003069 goto out2;
3070
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003071 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003072 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3073 if (r < 0)
3074 goto out;
3075 /* Set up identity-mapping pagetable for EPT in real mode */
3076 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3077 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3078 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3079 r = kvm_write_guest_page(kvm, identity_map_pfn,
3080 &tmp, i * sizeof(tmp), sizeof(tmp));
3081 if (r < 0)
3082 goto out;
3083 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003084 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003085
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003086out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003087 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08003088
3089out2:
3090 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003091 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003092}
3093
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094static void seg_setup(int seg)
3095{
Mathias Krause772e0312012-08-30 01:30:19 +02003096 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003097 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098
3099 vmcs_write16(sf->selector, 0);
3100 vmcs_writel(sf->base, 0);
3101 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003102 ar = 0x93;
3103 if (seg == VCPU_SREG_CS)
3104 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003105
3106 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107}
3108
Sheng Yangf78e0e22007-10-29 09:40:42 +08003109static int alloc_apic_access_page(struct kvm *kvm)
3110{
Xiao Guangrong44841412012-09-07 14:14:20 +08003111 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003112 int r = 0;
3113
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003114 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003115 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003116 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003117 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3118 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003119 if (r)
3120 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003121
Tang Chen73a6d942014-09-11 13:38:00 +08003122 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003123 if (is_error_page(page)) {
3124 r = -EFAULT;
3125 goto out;
3126 }
3127
Tang Chenc24ae0d2014-09-24 15:57:58 +08003128 /*
3129 * Do not pin the page in memory, so that memory hot-unplug
3130 * is able to migrate it.
3131 */
3132 put_page(page);
3133 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003134out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003135 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003136 return r;
3137}
3138
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003139int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003140{
3141 int vpid;
3142
Avi Kivity919818a2009-03-23 18:01:29 +02003143 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003144 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003145 spin_lock(&vmx_vpid_lock);
3146 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003147 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003148 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003149 else
3150 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003151 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003152 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003153}
3154
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003155void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003156{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003157 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003158 return;
3159 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003160 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003161 spin_unlock(&vmx_vpid_lock);
3162}
3163
Yi Wang1e4329ee2018-11-08 11:22:21 +08003164static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003165 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003166{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003167 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003168
3169 if (!cpu_has_vmx_msr_bitmap())
3170 return;
3171
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003172 if (static_branch_unlikely(&enable_evmcs))
3173 evmcs_touch_msr_bitmap();
3174
Sheng Yang25c5f222008-03-28 13:18:56 +08003175 /*
3176 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3177 * have the write-low and read-high bitmap offsets the wrong way round.
3178 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3179 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003180 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003181 if (type & MSR_TYPE_R)
3182 /* read-low */
3183 __clear_bit(msr, msr_bitmap + 0x000 / f);
3184
3185 if (type & MSR_TYPE_W)
3186 /* write-low */
3187 __clear_bit(msr, msr_bitmap + 0x800 / f);
3188
Sheng Yang25c5f222008-03-28 13:18:56 +08003189 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3190 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003191 if (type & MSR_TYPE_R)
3192 /* read-high */
3193 __clear_bit(msr, msr_bitmap + 0x400 / f);
3194
3195 if (type & MSR_TYPE_W)
3196 /* write-high */
3197 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3198
3199 }
3200}
3201
Yi Wang1e4329ee2018-11-08 11:22:21 +08003202static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003203 u32 msr, int type)
3204{
3205 int f = sizeof(unsigned long);
3206
3207 if (!cpu_has_vmx_msr_bitmap())
3208 return;
3209
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003210 if (static_branch_unlikely(&enable_evmcs))
3211 evmcs_touch_msr_bitmap();
3212
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003213 /*
3214 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3215 * have the write-low and read-high bitmap offsets the wrong way round.
3216 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3217 */
3218 if (msr <= 0x1fff) {
3219 if (type & MSR_TYPE_R)
3220 /* read-low */
3221 __set_bit(msr, msr_bitmap + 0x000 / f);
3222
3223 if (type & MSR_TYPE_W)
3224 /* write-low */
3225 __set_bit(msr, msr_bitmap + 0x800 / f);
3226
3227 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3228 msr &= 0x1fff;
3229 if (type & MSR_TYPE_R)
3230 /* read-high */
3231 __set_bit(msr, msr_bitmap + 0x400 / f);
3232
3233 if (type & MSR_TYPE_W)
3234 /* write-high */
3235 __set_bit(msr, msr_bitmap + 0xc00 / f);
3236
3237 }
3238}
3239
Yi Wang1e4329ee2018-11-08 11:22:21 +08003240static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003241 u32 msr, int type, bool value)
3242{
3243 if (value)
3244 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
3245 else
3246 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
3247}
3248
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003249static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003250{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003251 u8 mode = 0;
3252
3253 if (cpu_has_secondary_exec_ctrls() &&
3254 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
3255 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3256 mode |= MSR_BITMAP_MODE_X2APIC;
3257 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3258 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3259 }
3260
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003261 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003262}
3263
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003264static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
3265 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003266{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003267 int msr;
3268
3269 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3270 unsigned word = msr / BITS_PER_LONG;
3271 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3272 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003273 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003274
3275 if (mode & MSR_BITMAP_MODE_X2APIC) {
3276 /*
3277 * TPR reads and writes can be virtualized even if virtual interrupt
3278 * delivery is not in use.
3279 */
3280 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
3281 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3282 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
3283 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3284 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
3285 }
3286 }
3287}
3288
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003289void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003290{
3291 struct vcpu_vmx *vmx = to_vmx(vcpu);
3292 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3293 u8 mode = vmx_msr_bitmap_mode(vcpu);
3294 u8 changed = mode ^ vmx->msr_bitmap_mode;
3295
3296 if (!changed)
3297 return;
3298
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003299 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
3300 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
3301
3302 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003303}
3304
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05003305static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003306{
Andrey Smetanind62caab2015-11-10 15:36:33 +03003307 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003308}
3309
Liran Alone6c67d82018-09-04 10:56:52 +03003310static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3311{
3312 struct vcpu_vmx *vmx = to_vmx(vcpu);
3313 void *vapic_page;
3314 u32 vppr;
3315 int rvi;
3316
3317 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3318 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
3319 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
3320 return false;
3321
Paolo Bonzini7e712682018-10-03 13:44:26 +02003322 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003323
3324 vapic_page = kmap(vmx->nested.virtual_apic_page);
3325 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
3326 kunmap(vmx->nested.virtual_apic_page);
3327
3328 return ((rvi & 0xf0) > (vppr & 0xf0));
3329}
3330
Wincy Van06a55242017-04-28 13:13:59 +08003331static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3332 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003333{
3334#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003335 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3336
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003337 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003338 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003339 * The vector of interrupt to be delivered to vcpu had
3340 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003341 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003342 * Following cases will be reached in this block, and
3343 * we always send a notification event in all cases as
3344 * explained below.
3345 *
3346 * Case 1: vcpu keeps in non-root mode. Sending a
3347 * notification event posts the interrupt to vcpu.
3348 *
3349 * Case 2: vcpu exits to root mode and is still
3350 * runnable. PIR will be synced to vIRR before the
3351 * next vcpu entry. Sending a notification event in
3352 * this case has no effect, as vcpu is not in root
3353 * mode.
3354 *
3355 * Case 3: vcpu exits to root mode and is blocked.
3356 * vcpu_block() has already synced PIR to vIRR and
3357 * never blocks vcpu if vIRR is not cleared. Therefore,
3358 * a blocked vcpu here does not wait for any requested
3359 * interrupts in PIR, and sending a notification event
3360 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003361 */
Feng Wu28b835d2015-09-18 22:29:54 +08003362
Wincy Van06a55242017-04-28 13:13:59 +08003363 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003364 return true;
3365 }
3366#endif
3367 return false;
3368}
3369
Wincy Van705699a2015-02-03 23:58:17 +08003370static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3371 int vector)
3372{
3373 struct vcpu_vmx *vmx = to_vmx(vcpu);
3374
3375 if (is_guest_mode(vcpu) &&
3376 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003377 /*
3378 * If a posted intr is not recognized by hardware,
3379 * we will accomplish it in the next vmentry.
3380 */
3381 vmx->nested.pi_pending = true;
3382 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003383 /* the PIR and ON have been set by L1. */
3384 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3385 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003386 return 0;
3387 }
3388 return -1;
3389}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003391 * Send interrupt to vcpu via posted interrupt way.
3392 * 1. If target vcpu is running(non-root mode), send posted interrupt
3393 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3394 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3395 * interrupt from PIR in next vmentry.
3396 */
3397static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
3398{
3399 struct vcpu_vmx *vmx = to_vmx(vcpu);
3400 int r;
3401
Wincy Van705699a2015-02-03 23:58:17 +08003402 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3403 if (!r)
3404 return;
3405
Yang Zhanga20ed542013-04-11 19:25:15 +08003406 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
3407 return;
3408
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003409 /* If a previous notification has sent the IPI, nothing to do. */
3410 if (pi_test_and_set_on(&vmx->pi_desc))
3411 return;
3412
Wincy Van06a55242017-04-28 13:13:59 +08003413 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003414 kvm_vcpu_kick(vcpu);
3415}
3416
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003418 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3419 * will not change in the lifetime of the guest.
3420 * Note that host-state that does change is set elsewhere. E.g., host-state
3421 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3422 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003423void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003424{
3425 u32 low32, high32;
3426 unsigned long tmpl;
3427 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003428 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003429
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003430 cr0 = read_cr0();
3431 WARN_ON(cr0 & X86_CR0_TS);
3432 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003433
3434 /*
3435 * Save the most likely value for this task's CR3 in the VMCS.
3436 * We can't use __get_current_cr3_fast() because we're not atomic.
3437 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07003438 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003439 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003440 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003441
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003442 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003443 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003444 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003445 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003446
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003447 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003448#ifdef CONFIG_X86_64
3449 /*
3450 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003451 * vmx_prepare_switch_to_host(), in case userspace uses
3452 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03003453 */
3454 vmcs_write16(HOST_DS_SELECTOR, 0);
3455 vmcs_write16(HOST_ES_SELECTOR, 0);
3456#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003457 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3458 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003459#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003460 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3461 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3462
Juergen Gross87930012017-09-04 12:25:27 +02003463 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003464 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08003465 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003466
Avi Kivity83287ea422012-09-16 15:10:57 +03003467 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003468
3469 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3470 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3471 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3472 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3473
3474 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3475 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3476 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3477 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003478
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003479 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003480 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003481}
3482
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003483void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003484{
3485 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
3486 if (enable_ept)
3487 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003488 if (is_guest_mode(&vmx->vcpu))
3489 vmx->vcpu.arch.cr4_guest_owned_bits &=
3490 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003491 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3492}
3493
Yang Zhang01e439b2013-04-11 19:25:12 +08003494static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
3495{
3496 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
3497
Andrey Smetanind62caab2015-11-10 15:36:33 +03003498 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08003499 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003500
3501 if (!enable_vnmi)
3502 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
3503
Yunhong Jiang64672c92016-06-13 14:19:59 -07003504 /* Enable the preemption timer dynamically */
3505 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003506 return pin_based_exec_ctrl;
3507}
3508
Andrey Smetanind62caab2015-11-10 15:36:33 +03003509static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
3510{
3511 struct vcpu_vmx *vmx = to_vmx(vcpu);
3512
3513 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03003514 if (cpu_has_secondary_exec_ctrls()) {
3515 if (kvm_vcpu_apicv_active(vcpu))
3516 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
3517 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3518 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3519 else
3520 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
3521 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3522 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3523 }
3524
3525 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003526 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003527}
3528
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003529u32 vmx_exec_control(struct vcpu_vmx *vmx)
3530{
3531 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3532
3533 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
3534 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
3535
3536 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
3537 exec_control &= ~CPU_BASED_TPR_SHADOW;
3538#ifdef CONFIG_X86_64
3539 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3540 CPU_BASED_CR8_LOAD_EXITING;
3541#endif
3542 }
3543 if (!enable_ept)
3544 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3545 CPU_BASED_CR3_LOAD_EXITING |
3546 CPU_BASED_INVLPG_EXITING;
3547 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
3548 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
3549 CPU_BASED_MONITOR_EXITING);
3550 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
3551 exec_control &= ~CPU_BASED_HLT_EXITING;
3552 return exec_control;
3553}
3554
3555
Paolo Bonzini80154d72017-08-24 13:55:35 +02003556static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003557{
Paolo Bonzini80154d72017-08-24 13:55:35 +02003558 struct kvm_vcpu *vcpu = &vmx->vcpu;
3559
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003560 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02003561
Paolo Bonzini80154d72017-08-24 13:55:35 +02003562 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003563 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3564 if (vmx->vpid == 0)
3565 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
3566 if (!enable_ept) {
3567 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
3568 enable_unrestricted_guest = 0;
3569 }
3570 if (!enable_unrestricted_guest)
3571 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07003572 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003573 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02003574 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08003575 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
3576 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08003577 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02003578
3579 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
3580 * in vmx_set_cr4. */
3581 exec_control &= ~SECONDARY_EXEC_DESC;
3582
Abel Gordonabc4fc52013-04-18 14:35:25 +03003583 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
3584 (handle_vmptrld).
3585 We can NOT enable shadow_vmcs here because we don't have yet
3586 a current VMCS12
3587 */
3588 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08003589
3590 if (!enable_pml)
3591 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08003592
Paolo Bonzini3db13482017-08-24 14:48:03 +02003593 if (vmx_xsaves_supported()) {
3594 /* Exposing XSAVES only when XSAVE is exposed */
3595 bool xsaves_enabled =
3596 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
3597 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
3598
3599 if (!xsaves_enabled)
3600 exec_control &= ~SECONDARY_EXEC_XSAVES;
3601
3602 if (nested) {
3603 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003604 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02003605 SECONDARY_EXEC_XSAVES;
3606 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02003608 ~SECONDARY_EXEC_XSAVES;
3609 }
3610 }
3611
Paolo Bonzini80154d72017-08-24 13:55:35 +02003612 if (vmx_rdtscp_supported()) {
3613 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
3614 if (!rdtscp_enabled)
3615 exec_control &= ~SECONDARY_EXEC_RDTSCP;
3616
3617 if (nested) {
3618 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003620 SECONDARY_EXEC_RDTSCP;
3621 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003622 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003623 ~SECONDARY_EXEC_RDTSCP;
3624 }
3625 }
3626
3627 if (vmx_invpcid_supported()) {
3628 /* Exposing INVPCID only when PCID is exposed */
3629 bool invpcid_enabled =
3630 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
3631 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
3632
3633 if (!invpcid_enabled) {
3634 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
3635 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
3636 }
3637
3638 if (nested) {
3639 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003640 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003641 SECONDARY_EXEC_ENABLE_INVPCID;
3642 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003643 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003644 ~SECONDARY_EXEC_ENABLE_INVPCID;
3645 }
3646 }
3647
Jim Mattson45ec3682017-08-23 16:32:04 -07003648 if (vmx_rdrand_supported()) {
3649 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
3650 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02003651 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003652
3653 if (nested) {
3654 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003655 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003656 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003657 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003659 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003660 }
3661 }
3662
Jim Mattson75f4fc82017-08-23 16:32:03 -07003663 if (vmx_rdseed_supported()) {
3664 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
3665 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02003666 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003667
3668 if (nested) {
3669 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003670 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003671 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003672 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003674 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003675 }
3676 }
3677
Paolo Bonzini80154d72017-08-24 13:55:35 +02003678 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003679}
3680
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003681static void ept_set_mmio_spte_mask(void)
3682{
3683 /*
3684 * EPT Misconfigurations can be generated if the value of bits 2:0
3685 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003686 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07003687 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
3688 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003689}
3690
Wanpeng Lif53cd632014-12-02 19:14:58 +08003691#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692
Sean Christopherson944c3462018-12-03 13:53:09 -08003693/*
3694 * Sets up the vmcs for emulated real mode.
3695 */
3696static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
3697{
3698 int i;
3699
3700 if (nested)
3701 nested_vmx_vcpu_setup();
3702
Sheng Yang25c5f222008-03-28 13:18:56 +08003703 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003704 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08003705
Avi Kivity6aa8b732006-12-10 02:21:36 -08003706 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
3707
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08003709 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07003710 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003711
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003712 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003713
Dan Williamsdfa169b2016-06-02 11:17:24 -07003714 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02003715 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003716 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02003717 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07003718 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08003719
Andrey Smetanind62caab2015-11-10 15:36:33 +03003720 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08003721 vmcs_write64(EOI_EXIT_BITMAP0, 0);
3722 vmcs_write64(EOI_EXIT_BITMAP1, 0);
3723 vmcs_write64(EOI_EXIT_BITMAP2, 0);
3724 vmcs_write64(EOI_EXIT_BITMAP3, 0);
3725
3726 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08003727
Li RongQing0bcf2612015-12-03 13:29:34 +08003728 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08003729 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08003730 }
3731
Wanpeng Lib31c1142018-03-12 04:53:04 -07003732 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003733 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02003734 vmx->ple_window = ple_window;
3735 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003736 }
3737
Xiao Guangrongc3707952011-07-12 03:28:04 +08003738 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
3739 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003740 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
3741
Avi Kivity9581d442010-10-19 16:46:55 +02003742 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
3743 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08003744 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
3746 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003747
Bandan Das2a499e42017-08-03 15:54:41 -04003748 if (cpu_has_vmx_vmfunc())
3749 vmcs_write64(VM_FUNCTION_CONTROL, 0);
3750
Eddie Dong2cc51562007-05-21 07:28:09 +03003751 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
3752 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04003753 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03003754 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04003755 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756
Radim Krčmář74545702015-04-27 15:11:25 +02003757 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
3758 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08003759
Paolo Bonzini03916db2014-07-24 14:21:57 +02003760 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761 u32 index = vmx_msr_index[i];
3762 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003763 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764
3765 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3766 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08003767 if (wrmsr_safe(index, data_low, data_high) < 0)
3768 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03003769 vmx->guest_msrs[j].index = i;
3770 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02003771 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003772 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02003775 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02003776
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003777 vm_exit_controls_init(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778
3779 /* 22.2.1, 20.8.1 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003780 vm_entry_controls_init(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003781
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08003782 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
3783 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
3784
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003785 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003786
Wanpeng Lif53cd632014-12-02 19:14:58 +08003787 if (vmx_xsaves_supported())
3788 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
3789
Peter Feiner4e595162016-07-07 14:49:58 -07003790 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07003791 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
3792 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
3793 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07003794
3795 if (cpu_has_vmx_encls_vmexit())
3796 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003797}
3798
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003799static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003800{
3801 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01003802 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003803 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003804
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003805 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003806 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003807
Wanpeng Li518e7b92018-02-28 14:03:31 +08003808 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003809 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003810 kvm_set_cr8(vcpu, 0);
3811
3812 if (!init_event) {
3813 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
3814 MSR_IA32_APICBASE_ENABLE;
3815 if (kvm_vcpu_is_reset_bsp(vcpu))
3816 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
3817 apic_base_msr.host_initiated = true;
3818 kvm_set_apic_base(vcpu, &apic_base_msr);
3819 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003820
Avi Kivity2fb92db2011-04-27 19:42:18 +03003821 vmx_segment_cache_clear(vmx);
3822
Avi Kivity5706be02008-08-20 15:07:31 +03003823 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01003824 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01003825 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003826
3827 seg_setup(VCPU_SREG_DS);
3828 seg_setup(VCPU_SREG_ES);
3829 seg_setup(VCPU_SREG_FS);
3830 seg_setup(VCPU_SREG_GS);
3831 seg_setup(VCPU_SREG_SS);
3832
3833 vmcs_write16(GUEST_TR_SELECTOR, 0);
3834 vmcs_writel(GUEST_TR_BASE, 0);
3835 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3836 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3837
3838 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3839 vmcs_writel(GUEST_LDTR_BASE, 0);
3840 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3841 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3842
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003843 if (!init_event) {
3844 vmcs_write32(GUEST_SYSENTER_CS, 0);
3845 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3846 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3847 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3848 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003849
Wanpeng Lic37c2872017-11-20 14:52:21 -08003850 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01003851 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003852
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003853 vmcs_writel(GUEST_GDTR_BASE, 0);
3854 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
3855
3856 vmcs_writel(GUEST_IDTR_BASE, 0);
3857 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
3858
Anthony Liguori443381a2010-12-06 10:53:38 -06003859 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003860 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01003861 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07003862 if (kvm_mpx_supported())
3863 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003864
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003865 setup_msrs(vmx);
3866
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3868
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003869 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08003870 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003871 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08003872 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003873 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08003874 vmcs_write32(TPR_THRESHOLD, 0);
3875 }
3876
Paolo Bonzinia73896c2014-11-02 07:54:30 +01003877 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878
Sheng Yang2384d2b2008-01-17 15:14:33 +08003879 if (vmx->vpid != 0)
3880 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3881
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003882 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003883 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06003884 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003885 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02003886 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08003887
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003888 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003890 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003891 if (init_event)
3892 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893}
3894
Jan Kiszkac9a79532014-03-07 20:03:15 +01003895static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003896{
Paolo Bonzini47c01522016-12-19 11:44:07 +01003897 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
3898 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003899}
3900
Jan Kiszkac9a79532014-03-07 20:03:15 +01003901static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003902{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003903 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003904 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01003905 enable_irq_window(vcpu);
3906 return;
3907 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02003908
Paolo Bonzini47c01522016-12-19 11:44:07 +01003909 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
3910 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003911}
3912
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003913static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003914{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003915 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003916 uint32_t intr;
3917 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003918
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003919 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003920
Avi Kivityfa89a812008-09-01 15:57:51 +03003921 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003922 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003923 int inc_eip = 0;
3924 if (vcpu->arch.interrupt.soft)
3925 inc_eip = vcpu->arch.event_exit_inst_len;
3926 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003927 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003928 return;
3929 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003930 intr = irq | INTR_INFO_VALID_MASK;
3931 if (vcpu->arch.interrupt.soft) {
3932 intr |= INTR_TYPE_SOFT_INTR;
3933 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3934 vmx->vcpu.arch.event_exit_inst_len);
3935 } else
3936 intr |= INTR_TYPE_EXT_INTR;
3937 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003938
3939 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003940}
3941
Sheng Yangf08864b2008-05-15 18:23:25 +08003942static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3943{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003944 struct vcpu_vmx *vmx = to_vmx(vcpu);
3945
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003946 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003947 /*
3948 * Tracking the NMI-blocked state in software is built upon
3949 * finding the next open IRQ window. This, in turn, depends on
3950 * well-behaving guests: They have to keep IRQs disabled at
3951 * least as long as the NMI handler runs. Otherwise we may
3952 * cause NMI nesting, maybe breaking the guest. But as this is
3953 * highly unlikely, we can live with the residual risk.
3954 */
3955 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
3956 vmx->loaded_vmcs->vnmi_blocked_time = 0;
3957 }
3958
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003959 ++vcpu->stat.nmi_injections;
3960 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003961
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003962 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003963 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003964 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003965 return;
3966 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08003967
Sheng Yangf08864b2008-05-15 18:23:25 +08003968 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3969 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003970
3971 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003972}
3973
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003974bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003975{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003976 struct vcpu_vmx *vmx = to_vmx(vcpu);
3977 bool masked;
3978
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003979 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003980 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003981 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02003982 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003983 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
3984 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
3985 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003986}
3987
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003988void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003989{
3990 struct vcpu_vmx *vmx = to_vmx(vcpu);
3991
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003992 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003993 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
3994 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
3995 vmx->loaded_vmcs->vnmi_blocked_time = 0;
3996 }
3997 } else {
3998 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
3999 if (masked)
4000 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4001 GUEST_INTR_STATE_NMI);
4002 else
4003 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4004 GUEST_INTR_STATE_NMI);
4005 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004006}
4007
Jan Kiszka2505dc92013-04-14 12:12:47 +02004008static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4009{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004010 if (to_vmx(vcpu)->nested.nested_run_pending)
4011 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004012
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004013 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004014 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4015 return 0;
4016
Jan Kiszka2505dc92013-04-14 12:12:47 +02004017 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4018 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4019 | GUEST_INTR_STATE_NMI));
4020}
4021
Gleb Natapov78646122009-03-23 12:12:11 +02004022static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4023{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004024 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4025 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004026 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4027 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004028}
4029
Izik Eiduscbc94022007-10-25 00:29:55 +02004030static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4031{
4032 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004033
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004034 if (enable_unrestricted_guest)
4035 return 0;
4036
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004037 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4038 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02004039 if (ret)
4040 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004041 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004042 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004043}
4044
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004045static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4046{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004047 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004048 return 0;
4049}
4050
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004051static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004053 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004054 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004055 /*
4056 * Update instruction length as we may reinject the exception
4057 * from user space while in guest debugging mode.
4058 */
4059 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4060 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004061 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004062 return false;
4063 /* fall through */
4064 case DB_VECTOR:
4065 if (vcpu->guest_debug &
4066 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4067 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004068 /* fall through */
4069 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004070 case OF_VECTOR:
4071 case BR_VECTOR:
4072 case UD_VECTOR:
4073 case DF_VECTOR:
4074 case SS_VECTOR:
4075 case GP_VECTOR:
4076 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004077 return true;
4078 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004079 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004080 return false;
4081}
4082
4083static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4084 int vec, u32 err_code)
4085{
4086 /*
4087 * Instruction with address size override prefix opcode 0x67
4088 * Cause the #SS fault with 0 error code in VM86 mode.
4089 */
4090 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004091 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004092 if (vcpu->arch.halt_request) {
4093 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004094 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004095 }
4096 return 1;
4097 }
4098 return 0;
4099 }
4100
4101 /*
4102 * Forward all other exceptions that are valid in real mode.
4103 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4104 * the required debugging infrastructure rework.
4105 */
4106 kvm_queue_exception(vcpu, vec);
4107 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108}
4109
Andi Kleena0861c02009-06-08 17:37:09 +08004110/*
4111 * Trigger machine check on the host. We assume all the MSRs are already set up
4112 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4113 * We pass a fake environment to the machine check handler because we want
4114 * the guest to be always treated like user space, no matter what context
4115 * it used internally.
4116 */
4117static void kvm_machine_check(void)
4118{
4119#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4120 struct pt_regs regs = {
4121 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4122 .flags = X86_EFLAGS_IF,
4123 };
4124
4125 do_machine_check(&regs, 0);
4126#endif
4127}
4128
Avi Kivity851ba692009-08-24 11:10:17 +03004129static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004130{
4131 /* already handled by vcpu_run */
4132 return 1;
4133}
4134
Avi Kivity851ba692009-08-24 11:10:17 +03004135static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136{
Avi Kivity1155f762007-11-22 11:30:47 +02004137 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004138 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004139 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004140 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 u32 vect_info;
4142 enum emulation_result er;
4143
Avi Kivity1155f762007-11-22 11:30:47 +02004144 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004145 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146
Andi Kleena0861c02009-06-08 17:37:09 +08004147 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004148 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004149
Jim Mattsonef85b672016-12-12 11:01:37 -08004150 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02004151 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004152
Wanpeng Li082d06e2018-04-03 16:28:48 -07004153 if (is_invalid_opcode(intr_info))
4154 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004155
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004157 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004159
Liran Alon9e869482018-03-12 13:12:51 +02004160 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4161 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004162 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02004163 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
4164 if (er == EMULATE_USER_EXIT)
4165 return 0;
4166 else if (er != EMULATE_DONE)
4167 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4168 return 1;
4169 }
4170
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004171 /*
4172 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4173 * MMIO, it is better to report an internal error.
4174 * See the comments in vmx_handle_exit.
4175 */
4176 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4177 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4178 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4179 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004180 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004181 vcpu->run->internal.data[0] = vect_info;
4182 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004183 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004184 return 0;
4185 }
4186
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187 if (is_page_fault(intr_info)) {
4188 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004189 /* EPT won't cause page fault directly */
4190 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02004191 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004192 }
4193
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004194 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004195
4196 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4197 return handle_rmode_exception(vcpu, ex_no, error_code);
4198
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004199 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01004200 case AC_VECTOR:
4201 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4202 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004203 case DB_VECTOR:
4204 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4205 if (!(vcpu->guest_debug &
4206 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004207 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004208 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004209 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01004210 skip_emulated_instruction(vcpu);
4211
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004212 kvm_queue_exception(vcpu, DB_VECTOR);
4213 return 1;
4214 }
4215 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4216 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4217 /* fall through */
4218 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004219 /*
4220 * Update instruction length as we may reinject #BP from
4221 * user space while in guest debugging mode. Reading it for
4222 * #DB as well causes no harm, it is not used in that case.
4223 */
4224 vmx->vcpu.arch.event_exit_inst_len =
4225 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004226 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004227 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004228 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4229 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004230 break;
4231 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004232 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4233 kvm_run->ex.exception = ex_no;
4234 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004235 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004236 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 return 0;
4238}
4239
Avi Kivity851ba692009-08-24 11:10:17 +03004240static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004241{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004242 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243 return 1;
4244}
4245
Avi Kivity851ba692009-08-24 11:10:17 +03004246static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004247{
Avi Kivity851ba692009-08-24 11:10:17 +03004248 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004249 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004250 return 0;
4251}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252
Avi Kivity851ba692009-08-24 11:10:17 +03004253static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254{
He, Qingbfdaab02007-09-12 14:18:28 +08004255 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004256 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004257 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258
He, Qingbfdaab02007-09-12 14:18:28 +08004259 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004260 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004261
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004262 ++vcpu->stat.io_exits;
4263
Sean Christopherson432baf62018-03-08 08:57:26 -08004264 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004265 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004266
4267 port = exit_qualification >> 16;
4268 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004269 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004270
Sean Christophersondca7f122018-03-08 08:57:27 -08004271 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272}
4273
Ingo Molnar102d8322007-02-19 14:37:47 +02004274static void
4275vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4276{
4277 /*
4278 * Patch in the VMCALL instruction:
4279 */
4280 hypercall[0] = 0x0f;
4281 hypercall[1] = 0x01;
4282 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004283}
4284
Guo Chao0fa06072012-06-28 15:16:19 +08004285/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004286static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4287{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004288 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004289 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4290 unsigned long orig_val = val;
4291
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004292 /*
4293 * We get here when L2 changed cr0 in a way that did not change
4294 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004295 * but did change L0 shadowed bits. So we first calculate the
4296 * effective cr0 value that L1 would like to write into the
4297 * hardware. It consists of the L2-owned bits from the new
4298 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004299 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004300 val = (val & ~vmcs12->cr0_guest_host_mask) |
4301 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4302
David Matlack38991522016-11-29 18:14:08 -08004303 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004304 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004305
4306 if (kvm_set_cr0(vcpu, val))
4307 return 1;
4308 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004309 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004310 } else {
4311 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004312 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004313 return 1;
David Matlack38991522016-11-29 18:14:08 -08004314
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004315 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004316 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004317}
4318
4319static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4320{
4321 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004322 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4323 unsigned long orig_val = val;
4324
4325 /* analogously to handle_set_cr0 */
4326 val = (val & ~vmcs12->cr4_guest_host_mask) |
4327 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4328 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004329 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004330 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004331 return 0;
4332 } else
4333 return kvm_set_cr4(vcpu, val);
4334}
4335
Paolo Bonzini0367f202016-07-12 10:44:55 +02004336static int handle_desc(struct kvm_vcpu *vcpu)
4337{
4338 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004339 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004340}
4341
Avi Kivity851ba692009-08-24 11:10:17 +03004342static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004343{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004344 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345 int cr;
4346 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004347 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004348 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004349
He, Qingbfdaab02007-09-12 14:18:28 +08004350 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351 cr = exit_qualification & 15;
4352 reg = (exit_qualification >> 8) & 15;
4353 switch ((exit_qualification >> 4) & 3) {
4354 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004355 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004356 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357 switch (cr) {
4358 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004359 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004360 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004362 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004363 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004364 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004366 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004367 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004368 case 8: {
4369 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03004370 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004371 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004372 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004373 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08004374 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004375 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004376 return ret;
4377 /*
4378 * TODO: we might be squashing a
4379 * KVM_GUESTDBG_SINGLESTEP-triggered
4380 * KVM_EXIT_DEBUG here.
4381 */
Avi Kivity851ba692009-08-24 11:10:17 +03004382 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004383 return 0;
4384 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004385 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004387 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004388 WARN_ONCE(1, "Guest should always own CR0.TS");
4389 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02004390 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08004391 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392 case 1: /*mov from cr*/
4393 switch (cr) {
4394 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004395 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02004396 val = kvm_read_cr3(vcpu);
4397 kvm_register_write(vcpu, reg, val);
4398 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004399 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004401 val = kvm_get_cr8(vcpu);
4402 kvm_register_write(vcpu, reg, val);
4403 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004404 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405 }
4406 break;
4407 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004408 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004409 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004410 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004411
Kyle Huey6affcbe2016-11-29 12:40:40 -08004412 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413 default:
4414 break;
4415 }
Avi Kivity851ba692009-08-24 11:10:17 +03004416 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004417 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418 (int)(exit_qualification >> 4) & 3, cr);
4419 return 0;
4420}
4421
Avi Kivity851ba692009-08-24 11:10:17 +03004422static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004423{
He, Qingbfdaab02007-09-12 14:18:28 +08004424 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004425 int dr, dr7, reg;
4426
4427 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4428 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4429
4430 /* First, if DR does not exist, trigger UD */
4431 if (!kvm_require_dr(vcpu, dr))
4432 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433
Jan Kiszkaf2483412010-01-20 18:20:20 +01004434 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004435 if (!kvm_require_cpl(vcpu, 0))
4436 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004437 dr7 = vmcs_readl(GUEST_DR7);
4438 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004439 /*
4440 * As the vm-exit takes precedence over the debug trap, we
4441 * need to emulate the latter, either for the host or the
4442 * guest debugging itself.
4443 */
4444 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03004445 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004446 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02004447 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004448 vcpu->run->debug.arch.exception = DB_VECTOR;
4449 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004450 return 0;
4451 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02004452 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004453 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004454 kvm_queue_exception(vcpu, DB_VECTOR);
4455 return 1;
4456 }
4457 }
4458
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004459 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01004460 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4461 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004462
4463 /*
4464 * No more DR vmexits; force a reload of the debug registers
4465 * and reenter on this instruction. The next vmexit will
4466 * retrieve the full state of the debug registers.
4467 */
4468 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4469 return 1;
4470 }
4471
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004472 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4473 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004474 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004475
4476 if (kvm_get_dr(vcpu, dr, &val))
4477 return 1;
4478 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03004479 } else
Nadav Amit57773922014-06-18 17:19:23 +03004480 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004481 return 1;
4482
Kyle Huey6affcbe2016-11-29 12:40:40 -08004483 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484}
4485
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004486static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
4487{
4488 return vcpu->arch.dr6;
4489}
4490
4491static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
4492{
4493}
4494
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004495static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
4496{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004497 get_debugreg(vcpu->arch.db[0], 0);
4498 get_debugreg(vcpu->arch.db[1], 1);
4499 get_debugreg(vcpu->arch.db[2], 2);
4500 get_debugreg(vcpu->arch.db[3], 3);
4501 get_debugreg(vcpu->arch.dr6, 6);
4502 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
4503
4504 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01004505 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004506}
4507
Gleb Natapov020df072010-04-13 10:05:23 +03004508static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4509{
4510 vmcs_writel(GUEST_DR7, val);
4511}
4512
Avi Kivity851ba692009-08-24 11:10:17 +03004513static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004514{
Kyle Huey6a908b62016-11-29 12:40:37 -08004515 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516}
4517
Avi Kivity851ba692009-08-24 11:10:17 +03004518static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004519{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004520 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004521 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004522
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004523 msr_info.index = ecx;
4524 msr_info.host_initiated = false;
4525 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004526 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004527 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004528 return 1;
4529 }
4530
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004531 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004532
Avi Kivity6aa8b732006-12-10 02:21:36 -08004533 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004534 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
4535 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004536 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004537}
4538
Avi Kivity851ba692009-08-24 11:10:17 +03004539static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004540{
Will Auld8fe8ab42012-11-29 12:42:12 -08004541 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004542 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4543 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4544 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545
Will Auld8fe8ab42012-11-29 12:42:12 -08004546 msr.data = data;
4547 msr.index = ecx;
4548 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004549 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02004550 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004551 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004552 return 1;
4553 }
4554
Avi Kivity59200272010-01-25 19:47:02 +02004555 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004556 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004557}
4558
Avi Kivity851ba692009-08-24 11:10:17 +03004559static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004560{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01004561 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004562 return 1;
4563}
4564
Avi Kivity851ba692009-08-24 11:10:17 +03004565static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566{
Paolo Bonzini47c01522016-12-19 11:44:07 +01004567 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4568 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004569
Avi Kivity3842d132010-07-27 12:30:24 +03004570 kvm_make_request(KVM_REQ_EVENT, vcpu);
4571
Jan Kiszkaa26bf122008-09-26 09:30:45 +02004572 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573 return 1;
4574}
4575
Avi Kivity851ba692009-08-24 11:10:17 +03004576static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577{
Avi Kivityd3bef152007-06-05 15:53:05 +03004578 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004579}
4580
Avi Kivity851ba692009-08-24 11:10:17 +03004581static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02004582{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03004583 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02004584}
4585
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004586static int handle_invd(struct kvm_vcpu *vcpu)
4587{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004588 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004589}
4590
Avi Kivity851ba692009-08-24 11:10:17 +03004591static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03004592{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004593 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004594
4595 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004596 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004597}
4598
Avi Kivityfee84b02011-11-10 14:57:25 +02004599static int handle_rdpmc(struct kvm_vcpu *vcpu)
4600{
4601 int err;
4602
4603 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004604 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02004605}
4606
Avi Kivity851ba692009-08-24 11:10:17 +03004607static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02004608{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004609 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02004610}
4611
Dexuan Cui2acf9232010-06-10 11:27:12 +08004612static int handle_xsetbv(struct kvm_vcpu *vcpu)
4613{
4614 u64 new_bv = kvm_read_edx_eax(vcpu);
4615 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4616
4617 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004618 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08004619 return 1;
4620}
4621
Wanpeng Lif53cd632014-12-02 19:14:58 +08004622static int handle_xsaves(struct kvm_vcpu *vcpu)
4623{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004624 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08004625 WARN(1, "this should never happen\n");
4626 return 1;
4627}
4628
4629static int handle_xrstors(struct kvm_vcpu *vcpu)
4630{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004631 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08004632 WARN(1, "this should never happen\n");
4633 return 1;
4634}
4635
Avi Kivity851ba692009-08-24 11:10:17 +03004636static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004637{
Kevin Tian58fbbf22011-08-30 13:56:17 +03004638 if (likely(fasteoi)) {
4639 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4640 int access_type, offset;
4641
4642 access_type = exit_qualification & APIC_ACCESS_TYPE;
4643 offset = exit_qualification & APIC_ACCESS_OFFSET;
4644 /*
4645 * Sane guest uses MOV to write EOI, with written value
4646 * not cared. So make a short-circuit here by avoiding
4647 * heavy instruction emulation.
4648 */
4649 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4650 (offset == APIC_EOI)) {
4651 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004652 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03004653 }
4654 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004655 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004656}
4657
Yang Zhangc7c9c562013-01-25 10:18:51 +08004658static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
4659{
4660 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4661 int vector = exit_qualification & 0xff;
4662
4663 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
4664 kvm_apic_set_eoi_accelerated(vcpu, vector);
4665 return 1;
4666}
4667
Yang Zhang83d4c282013-01-25 10:18:49 +08004668static int handle_apic_write(struct kvm_vcpu *vcpu)
4669{
4670 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4671 u32 offset = exit_qualification & 0xfff;
4672
4673 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
4674 kvm_apic_write_nodecode(vcpu, offset);
4675 return 1;
4676}
4677
Avi Kivity851ba692009-08-24 11:10:17 +03004678static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02004679{
Jan Kiszka60637aa2008-09-26 09:30:47 +02004680 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004681 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02004682 bool has_error_code = false;
4683 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004684 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004685 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004686
4687 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004688 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004689 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02004690
4691 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4692
4693 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004694 if (reason == TASK_SWITCH_GATE && idt_v) {
4695 switch (type) {
4696 case INTR_TYPE_NMI_INTR:
4697 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02004698 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004699 break;
4700 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004701 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004702 kvm_clear_interrupt_queue(vcpu);
4703 break;
4704 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02004705 if (vmx->idt_vectoring_info &
4706 VECTORING_INFO_DELIVER_CODE_MASK) {
4707 has_error_code = true;
4708 error_code =
4709 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4710 }
4711 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004712 case INTR_TYPE_SOFT_EXCEPTION:
4713 kvm_clear_exception_queue(vcpu);
4714 break;
4715 default:
4716 break;
4717 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02004718 }
Izik Eidus37817f22008-03-24 23:14:53 +02004719 tss_selector = exit_qualification;
4720
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004721 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
4722 type != INTR_TYPE_EXT_INTR &&
4723 type != INTR_TYPE_NMI_INTR))
4724 skip_emulated_instruction(vcpu);
4725
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004726 if (kvm_task_switch(vcpu, tss_selector,
4727 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
4728 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03004729 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4730 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4731 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004732 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03004733 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004734
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004735 /*
4736 * TODO: What about debug traps on tss switch?
4737 * Are we supposed to inject them and update dr6?
4738 */
4739
4740 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02004741}
4742
Avi Kivity851ba692009-08-24 11:10:17 +03004743static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08004744{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004745 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08004746 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01004747 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08004748
Sheng Yangf9c617f2009-03-25 10:08:52 +08004749 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08004750
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03004751 /*
4752 * EPT violation happened while executing iret from NMI,
4753 * "blocked by NMI" bit has to be set before next VM entry.
4754 * There are errata that may cause this bit to not be set:
4755 * AAK134, BY25.
4756 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03004757 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004758 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03004759 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03004760 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
4761
Sheng Yang14394422008-04-28 12:24:45 +08004762 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004763 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004764
Junaid Shahid27959a42016-12-06 16:46:10 -08004765 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004766 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08004767 ? PFERR_USER_MASK : 0;
4768 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004769 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08004770 ? PFERR_WRITE_MASK : 0;
4771 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004772 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08004773 ? PFERR_FETCH_MASK : 0;
4774 /* ept page table entry is present? */
4775 error_code |= (exit_qualification &
4776 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
4777 EPT_VIOLATION_EXECUTABLE))
4778 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004779
Paolo Bonzinieebed242016-11-28 14:39:58 +01004780 error_code |= (exit_qualification & 0x100) != 0 ?
4781 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03004782
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004783 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004784 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08004785}
4786
Avi Kivity851ba692009-08-24 11:10:17 +03004787static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004788{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004789 gpa_t gpa;
4790
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02004791 /*
4792 * A nested guest cannot optimize MMIO vmexits, because we have an
4793 * nGPA here instead of the required GPA.
4794 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004795 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02004796 if (!is_guest_mode(vcpu) &&
4797 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08004798 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01004799 /*
4800 * Doing kvm_skip_emulated_instruction() depends on undefined
4801 * behavior: Intel's manual doesn't mandate
4802 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
4803 * occurs and while on real hardware it was observed to be set,
4804 * other hypervisors (namely Hyper-V) don't set it, we end up
4805 * advancing IP with some random value. Disable fast mmio when
4806 * running nested and keep it for real hardware in hope that
4807 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
4808 */
4809 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
4810 return kvm_skip_emulated_instruction(vcpu);
4811 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004812 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07004813 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03004814 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004815
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07004816 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004817}
4818
Avi Kivity851ba692009-08-24 11:10:17 +03004819static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08004820{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004821 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01004822 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4823 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08004824 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03004825 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004826
4827 return 1;
4828}
4829
Mohammed Gamal80ced182009-09-01 12:48:18 +02004830static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004831{
Avi Kivity8b3079a2009-01-05 12:10:54 +02004832 struct vcpu_vmx *vmx = to_vmx(vcpu);
4833 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004834 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02004835 u32 cpu_exec_ctrl;
4836 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03004837 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02004838
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07004839 /*
4840 * We should never reach the point where we are emulating L2
4841 * due to invalid guest state as that means we incorrectly
4842 * allowed a nested VMEntry with an invalid vmcs12.
4843 */
4844 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
4845
Avi Kivity49e9d552010-09-19 14:34:08 +02004846 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4847 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004848
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004849 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03004850 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02004851 return handle_interrupt_window(&vmx->vcpu);
4852
Radim Krčmář72875d82017-04-26 22:32:19 +02004853 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03004854 return 1;
4855
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004856 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004857
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02004858 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02004859 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004860 ret = 0;
4861 goto out;
4862 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01004863
Sean Christophersonadd5ff72018-03-23 09:34:00 -07004864 if (err != EMULATE_DONE)
4865 goto emulation_error;
4866
4867 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
4868 vcpu->arch.exception.pending)
4869 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004870
Gleb Natapov8d76c492013-05-08 18:38:44 +03004871 if (vcpu->arch.halt_request) {
4872 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004873 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03004874 goto out;
4875 }
4876
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004877 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02004878 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004879 if (need_resched())
4880 schedule();
4881 }
4882
Mohammed Gamal80ced182009-09-01 12:48:18 +02004883out:
4884 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004885
Sean Christophersonadd5ff72018-03-23 09:34:00 -07004886emulation_error:
4887 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4888 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4889 vcpu->run->internal.ndata = 0;
4890 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004891}
4892
4893static void grow_ple_window(struct kvm_vcpu *vcpu)
4894{
4895 struct vcpu_vmx *vmx = to_vmx(vcpu);
4896 int old = vmx->ple_window;
4897
Babu Mogerc8e88712018-03-16 16:37:24 -04004898 vmx->ple_window = __grow_ple_window(old, ple_window,
4899 ple_window_grow,
4900 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004901
4902 if (vmx->ple_window != old)
4903 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02004904
4905 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004906}
4907
4908static void shrink_ple_window(struct kvm_vcpu *vcpu)
4909{
4910 struct vcpu_vmx *vmx = to_vmx(vcpu);
4911 int old = vmx->ple_window;
4912
Babu Mogerc8e88712018-03-16 16:37:24 -04004913 vmx->ple_window = __shrink_ple_window(old, ple_window,
4914 ple_window_shrink,
4915 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004916
4917 if (vmx->ple_window != old)
4918 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02004919
4920 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004921}
4922
4923/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08004924 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
4925 */
4926static void wakeup_handler(void)
4927{
4928 struct kvm_vcpu *vcpu;
4929 int cpu = smp_processor_id();
4930
4931 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4932 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
4933 blocked_vcpu_list) {
4934 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
4935
4936 if (pi_test_on(pi_desc) == 1)
4937 kvm_vcpu_kick(vcpu);
4938 }
4939 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4940}
4941
Peng Haoe01bca22018-04-07 05:47:32 +08004942static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08004943{
4944 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
4945 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
4946 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
4947 0ull, VMX_EPT_EXECUTABLE_MASK,
4948 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004949 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08004950
4951 ept_set_mmio_spte_mask();
4952 kvm_enable_tdp();
4953}
4954
Avi Kivity6aa8b732006-12-10 02:21:36 -08004955/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004956 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
4957 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
4958 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03004959static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004960{
Wanpeng Lib31c1142018-03-12 04:53:04 -07004961 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004962 grow_ple_window(vcpu);
4963
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004964 /*
4965 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
4966 * VM-execution control is ignored if CPL > 0. OTOH, KVM
4967 * never set PAUSE_EXITING and just set PLE if supported,
4968 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
4969 */
4970 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004971 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004972}
4973
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004974static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08004975{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004976 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08004977}
4978
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004979static int handle_mwait(struct kvm_vcpu *vcpu)
4980{
4981 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4982 return handle_nop(vcpu);
4983}
4984
Jim Mattson45ec3682017-08-23 16:32:04 -07004985static int handle_invalid_op(struct kvm_vcpu *vcpu)
4986{
4987 kvm_queue_exception(vcpu, UD_VECTOR);
4988 return 1;
4989}
4990
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03004991static int handle_monitor_trap(struct kvm_vcpu *vcpu)
4992{
4993 return 1;
4994}
4995
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004996static int handle_monitor(struct kvm_vcpu *vcpu)
4997{
4998 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4999 return handle_nop(vcpu);
5000}
5001
Junaid Shahideb4b2482018-06-27 14:59:14 -07005002static int handle_invpcid(struct kvm_vcpu *vcpu)
5003{
5004 u32 vmx_instruction_info;
5005 unsigned long type;
5006 bool pcid_enabled;
5007 gva_t gva;
5008 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005009 unsigned i;
5010 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005011 struct {
5012 u64 pcid;
5013 u64 gla;
5014 } operand;
5015
5016 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5017 kvm_queue_exception(vcpu, UD_VECTOR);
5018 return 1;
5019 }
5020
5021 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5022 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5023
5024 if (type > 3) {
5025 kvm_inject_gp(vcpu, 0);
5026 return 1;
5027 }
5028
5029 /* According to the Intel instruction reference, the memory operand
5030 * is read even if it isn't needed (e.g., for type==all)
5031 */
5032 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5033 vmx_instruction_info, false, &gva))
5034 return 1;
5035
5036 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
5037 kvm_inject_page_fault(vcpu, &e);
5038 return 1;
5039 }
5040
5041 if (operand.pcid >> 12 != 0) {
5042 kvm_inject_gp(vcpu, 0);
5043 return 1;
5044 }
5045
5046 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
5047
5048 switch (type) {
5049 case INVPCID_TYPE_INDIV_ADDR:
5050 if ((!pcid_enabled && (operand.pcid != 0)) ||
5051 is_noncanonical_address(operand.gla, vcpu)) {
5052 kvm_inject_gp(vcpu, 0);
5053 return 1;
5054 }
5055 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
5056 return kvm_skip_emulated_instruction(vcpu);
5057
5058 case INVPCID_TYPE_SINGLE_CTXT:
5059 if (!pcid_enabled && (operand.pcid != 0)) {
5060 kvm_inject_gp(vcpu, 0);
5061 return 1;
5062 }
5063
5064 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
5065 kvm_mmu_sync_roots(vcpu);
5066 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
5067 }
5068
Junaid Shahidb94742c2018-06-27 14:59:20 -07005069 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005070 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07005071 == operand.pcid)
5072 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07005073
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02005074 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005075 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005076 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07005077 * given PCID, then nothing needs to be done here because a
5078 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005079 */
5080
5081 return kvm_skip_emulated_instruction(vcpu);
5082
5083 case INVPCID_TYPE_ALL_NON_GLOBAL:
5084 /*
5085 * Currently, KVM doesn't mark global entries in the shadow
5086 * page tables, so a non-global flush just degenerates to a
5087 * global flush. If needed, we could optimize this later by
5088 * keeping track of global entries in shadow page tables.
5089 */
5090
5091 /* fall-through */
5092 case INVPCID_TYPE_ALL_INCL_GLOBAL:
5093 kvm_mmu_unload(vcpu);
5094 return kvm_skip_emulated_instruction(vcpu);
5095
5096 default:
5097 BUG(); /* We have already checked above that type <= 3 */
5098 }
5099}
5100
Kai Huang843e4332015-01-28 10:54:28 +08005101static int handle_pml_full(struct kvm_vcpu *vcpu)
5102{
5103 unsigned long exit_qualification;
5104
5105 trace_kvm_pml_full(vcpu->vcpu_id);
5106
5107 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5108
5109 /*
5110 * PML buffer FULL happened while executing iret from NMI,
5111 * "blocked by NMI" bit has to be set before next VM entry.
5112 */
5113 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005114 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005115 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5116 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5117 GUEST_INTR_STATE_NMI);
5118
5119 /*
5120 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5121 * here.., and there's no userspace involvement needed for PML.
5122 */
5123 return 1;
5124}
5125
Yunhong Jiang64672c92016-06-13 14:19:59 -07005126static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5127{
Sean Christophersond264ee02018-08-27 15:21:12 -07005128 if (!to_vmx(vcpu)->req_immediate_exit)
5129 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005130 return 1;
5131}
5132
Sean Christophersone4027cf2018-12-03 13:53:12 -08005133/*
5134 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5135 * are overwritten by nested_vmx_setup() when nested=1.
5136 */
5137static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5138{
5139 kvm_queue_exception(vcpu, UD_VECTOR);
5140 return 1;
5141}
5142
Sean Christopherson0b665d32018-08-14 09:33:34 -07005143static int handle_encls(struct kvm_vcpu *vcpu)
5144{
5145 /*
5146 * SGX virtualization is not yet supported. There is no software
5147 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5148 * to prevent the guest from executing ENCLS.
5149 */
5150 kvm_queue_exception(vcpu, UD_VECTOR);
5151 return 1;
5152}
5153
Nadav Har'El0140cae2011-05-25 23:06:28 +03005154/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005155 * The exit handlers return 1 if the exit was handled fully and guest execution
5156 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5157 * to be done to userspace and return 0.
5158 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005159static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005160 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5161 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005162 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005163 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005165 [EXIT_REASON_CR_ACCESS] = handle_cr,
5166 [EXIT_REASON_DR_ACCESS] = handle_dr,
5167 [EXIT_REASON_CPUID] = handle_cpuid,
5168 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5169 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5170 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5171 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005172 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005173 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005174 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005175 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005176 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5177 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5178 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5179 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5180 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5181 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5182 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5183 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5184 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005185 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5186 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005187 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005188 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005189 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005190 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005191 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005192 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005193 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5194 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005195 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5196 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005197 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005198 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005199 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005200 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005201 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5202 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005203 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005204 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08005205 [EXIT_REASON_XSAVES] = handle_xsaves,
5206 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08005207 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005208 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005209 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005210 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005211 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005212};
5213
5214static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005215 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005216
Avi Kivity586f9602010-11-18 13:09:54 +02005217static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5218{
5219 *info1 = vmcs_readl(EXIT_QUALIFICATION);
5220 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
5221}
5222
Kai Huanga3eaa862015-11-04 13:46:05 +08005223static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005224{
Kai Huanga3eaa862015-11-04 13:46:05 +08005225 if (vmx->pml_pg) {
5226 __free_page(vmx->pml_pg);
5227 vmx->pml_pg = NULL;
5228 }
Kai Huang843e4332015-01-28 10:54:28 +08005229}
5230
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005231static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005232{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005233 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005234 u64 *pml_buf;
5235 u16 pml_idx;
5236
5237 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5238
5239 /* Do nothing if PML buffer is empty */
5240 if (pml_idx == (PML_ENTITY_NUM - 1))
5241 return;
5242
5243 /* PML index always points to next available PML buffer entity */
5244 if (pml_idx >= PML_ENTITY_NUM)
5245 pml_idx = 0;
5246 else
5247 pml_idx++;
5248
5249 pml_buf = page_address(vmx->pml_pg);
5250 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5251 u64 gpa;
5252
5253 gpa = pml_buf[pml_idx];
5254 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005255 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005256 }
5257
5258 /* reset PML index */
5259 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5260}
5261
5262/*
5263 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5264 * Called before reporting dirty_bitmap to userspace.
5265 */
5266static void kvm_flush_pml_buffers(struct kvm *kvm)
5267{
5268 int i;
5269 struct kvm_vcpu *vcpu;
5270 /*
5271 * We only need to kick vcpu out of guest mode here, as PML buffer
5272 * is flushed at beginning of all VMEXITs, and it's obvious that only
5273 * vcpus running in guest are possible to have unflushed GPAs in PML
5274 * buffer.
5275 */
5276 kvm_for_each_vcpu(i, vcpu, kvm)
5277 kvm_vcpu_kick(vcpu);
5278}
5279
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005280static void vmx_dump_sel(char *name, uint32_t sel)
5281{
5282 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005283 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005284 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5285 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5286 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5287}
5288
5289static void vmx_dump_dtsel(char *name, uint32_t limit)
5290{
5291 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5292 name, vmcs_read32(limit),
5293 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5294}
5295
5296static void dump_vmcs(void)
5297{
5298 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5299 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5300 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5301 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5302 u32 secondary_exec_control = 0;
5303 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005304 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005305 int i, n;
5306
5307 if (cpu_has_secondary_exec_ctrls())
5308 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5309
5310 pr_err("*** Guest State ***\n");
5311 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5312 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5313 vmcs_readl(CR0_GUEST_HOST_MASK));
5314 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5315 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5316 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5317 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5318 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5319 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005320 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5321 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5322 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5323 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005324 }
5325 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5326 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5327 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5328 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5329 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5330 vmcs_readl(GUEST_SYSENTER_ESP),
5331 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5332 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5333 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5334 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5335 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5336 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5337 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5338 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5339 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5340 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5341 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5342 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5343 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005344 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5345 efer, vmcs_read64(GUEST_IA32_PAT));
5346 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5347 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005348 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005349 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005350 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005351 pr_err("PerfGlobCtl = 0x%016llx\n",
5352 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005353 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005354 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005355 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5356 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5357 vmcs_read32(GUEST_ACTIVITY_STATE));
5358 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5359 pr_err("InterruptStatus = %04x\n",
5360 vmcs_read16(GUEST_INTR_STATUS));
5361
5362 pr_err("*** Host State ***\n");
5363 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5364 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5365 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5366 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5367 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5368 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5369 vmcs_read16(HOST_TR_SELECTOR));
5370 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5371 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5372 vmcs_readl(HOST_TR_BASE));
5373 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5374 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5375 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5376 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5377 vmcs_readl(HOST_CR4));
5378 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5379 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5380 vmcs_read32(HOST_IA32_SYSENTER_CS),
5381 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5382 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005383 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5384 vmcs_read64(HOST_IA32_EFER),
5385 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005386 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005387 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005388 pr_err("PerfGlobCtl = 0x%016llx\n",
5389 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005390
5391 pr_err("*** Control State ***\n");
5392 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5393 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5394 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5395 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5396 vmcs_read32(EXCEPTION_BITMAP),
5397 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5398 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5399 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5400 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5401 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5402 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5403 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5404 vmcs_read32(VM_EXIT_INTR_INFO),
5405 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5406 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5407 pr_err(" reason=%08x qualification=%016lx\n",
5408 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5409 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5410 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5411 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005412 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005413 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005414 pr_err("TSC Multiplier = 0x%016llx\n",
5415 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005416 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
5417 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
5418 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5419 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5420 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005421 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005422 n = vmcs_read32(CR3_TARGET_COUNT);
5423 for (i = 0; i + 1 < n; i += 4)
5424 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
5425 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
5426 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
5427 if (i < n)
5428 pr_err("CR3 target%u=%016lx\n",
5429 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
5430 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5431 pr_err("PLE Gap=%08x Window=%08x\n",
5432 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5433 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5434 pr_err("Virtual processor ID = 0x%04x\n",
5435 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5436}
5437
Avi Kivity6aa8b732006-12-10 02:21:36 -08005438/*
5439 * The guest has exited. See if we can fix it or if we need userspace
5440 * assistance.
5441 */
Avi Kivity851ba692009-08-24 11:10:17 +03005442static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005443{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005444 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005445 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005446 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005447
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01005448 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
5449
Kai Huang843e4332015-01-28 10:54:28 +08005450 /*
5451 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5452 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5453 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5454 * mode as if vcpus is in root mode, the PML buffer must has been
5455 * flushed already.
5456 */
5457 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005458 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005459
Mohammed Gamal80ced182009-09-01 12:48:18 +02005460 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005461 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005462 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005463
Paolo Bonzini7313c692017-07-27 10:31:25 +02005464 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
5465 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03005466
Mohammed Gamal51207022010-05-31 22:40:54 +03005467 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005468 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03005469 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5470 vcpu->run->fail_entry.hardware_entry_failure_reason
5471 = exit_reason;
5472 return 0;
5473 }
5474
Avi Kivity29bd8a72007-09-10 17:27:03 +03005475 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005476 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5477 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03005478 = vmcs_read32(VM_INSTRUCTION_ERROR);
5479 return 0;
5480 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005481
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005482 /*
5483 * Note:
5484 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
5485 * delivery event since it indicates guest is accessing MMIO.
5486 * The vm-exit can be triggered again after return to guest that
5487 * will cause infinite loop.
5488 */
Mike Dayd77c26f2007-10-08 09:02:08 -04005489 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08005490 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02005491 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00005492 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005493 exit_reason != EXIT_REASON_TASK_SWITCH)) {
5494 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5495 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005496 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005497 vcpu->run->internal.data[0] = vectoring_info;
5498 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005499 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
5500 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
5501 vcpu->run->internal.ndata++;
5502 vcpu->run->internal.data[3] =
5503 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5504 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005505 return 0;
5506 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005507
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005508 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005509 vmx->loaded_vmcs->soft_vnmi_blocked)) {
5510 if (vmx_interrupt_allowed(vcpu)) {
5511 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5512 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
5513 vcpu->arch.nmi_pending) {
5514 /*
5515 * This CPU don't support us in finding the end of an
5516 * NMI-blocked window if the guest runs with IRQs
5517 * disabled. So we pull the trigger after 1 s of
5518 * futile waiting, but inform the user about this.
5519 */
5520 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
5521 "state on VCPU %d after 1 s timeout\n",
5522 __func__, vcpu->vcpu_id);
5523 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5524 }
5525 }
5526
Avi Kivity6aa8b732006-12-10 02:21:36 -08005527 if (exit_reason < kvm_vmx_max_exit_handlers
5528 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03005529 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005530 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01005531 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
5532 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03005533 kvm_queue_exception(vcpu, UD_VECTOR);
5534 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005535 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536}
5537
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005538/*
5539 * Software based L1D cache flush which is used when microcode providing
5540 * the cache control MSR is not loaded.
5541 *
5542 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
5543 * flush it is required to read in 64 KiB because the replacement algorithm
5544 * is not exactly LRU. This could be sized at runtime via topology
5545 * information but as all relevant affected CPUs have 32KiB L1D cache size
5546 * there is no point in doing so.
5547 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005548static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005549{
5550 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005551
5552 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02005553 * This code is only executed when the the flush mode is 'cond' or
5554 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005555 */
Nicolai Stange427362a2018-07-21 22:25:00 +02005556 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02005557 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005558
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005559 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02005560 * Clear the per-vcpu flush bit, it gets set again
5561 * either from vcpu_run() or from one of the unsafe
5562 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005563 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02005564 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02005565 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02005566
5567 /*
5568 * Clear the per-cpu flush bit, it gets set again from
5569 * the interrupt handlers.
5570 */
5571 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
5572 kvm_clear_cpu_l1tf_flush_l1d();
5573
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005574 if (!flush_l1d)
5575 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005576 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005577
5578 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005579
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02005580 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
5581 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
5582 return;
5583 }
5584
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005585 asm volatile(
5586 /* First ensure the pages are in the TLB */
5587 "xorl %%eax, %%eax\n"
5588 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02005589 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005590 "addl $4096, %%eax\n\t"
5591 "cmpl %%eax, %[size]\n\t"
5592 "jne .Lpopulate_tlb\n\t"
5593 "xorl %%eax, %%eax\n\t"
5594 "cpuid\n\t"
5595 /* Now fill the cache */
5596 "xorl %%eax, %%eax\n"
5597 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02005598 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005599 "addl $64, %%eax\n\t"
5600 "cmpl %%eax, %[size]\n\t"
5601 "jne .Lfill_cache\n\t"
5602 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02005603 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005604 [size] "r" (size)
5605 : "eax", "ebx", "ecx", "edx");
5606}
5607
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005608static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005609{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08005610 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5611
5612 if (is_guest_mode(vcpu) &&
5613 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
5614 return;
5615
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005616 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005617 vmcs_write32(TPR_THRESHOLD, 0);
5618 return;
5619 }
5620
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005621 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005622}
5623
Sean Christopherson97b7ead2018-12-03 13:53:16 -08005624void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005625{
5626 u32 sec_exec_control;
5627
Jim Mattson8d860bb2018-05-09 16:56:05 -04005628 if (!lapic_in_kernel(vcpu))
5629 return;
5630
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07005631 if (!flexpriority_enabled &&
5632 !cpu_has_vmx_virtualize_x2apic_mode())
5633 return;
5634
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02005635 /* Postpone execution until vmcs01 is the current VMCS. */
5636 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04005637 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02005638 return;
5639 }
5640
Yang Zhang8d146952013-01-25 10:18:50 +08005641 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04005642 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
5643 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08005644
Jim Mattson8d860bb2018-05-09 16:56:05 -04005645 switch (kvm_get_apic_mode(vcpu)) {
5646 case LAPIC_MODE_INVALID:
5647 WARN_ONCE(true, "Invalid local APIC state");
5648 case LAPIC_MODE_DISABLED:
5649 break;
5650 case LAPIC_MODE_XAPIC:
5651 if (flexpriority_enabled) {
5652 sec_exec_control |=
5653 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5654 vmx_flush_tlb(vcpu, true);
5655 }
5656 break;
5657 case LAPIC_MODE_X2APIC:
5658 if (cpu_has_vmx_virtualize_x2apic_mode())
5659 sec_exec_control |=
5660 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5661 break;
Yang Zhang8d146952013-01-25 10:18:50 +08005662 }
5663 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
5664
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005665 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08005666}
5667
Tang Chen38b99172014-09-24 15:57:54 +08005668static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
5669{
Jim Mattsonab5df312018-05-09 17:02:03 -04005670 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08005671 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07005672 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07005673 }
Tang Chen38b99172014-09-24 15:57:54 +08005674}
5675
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005676static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005677{
5678 u16 status;
5679 u8 old;
5680
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005681 if (max_isr == -1)
5682 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08005683
5684 status = vmcs_read16(GUEST_INTR_STATUS);
5685 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005686 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005687 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005688 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08005689 vmcs_write16(GUEST_INTR_STATUS, status);
5690 }
5691}
5692
5693static void vmx_set_rvi(int vector)
5694{
5695 u16 status;
5696 u8 old;
5697
Wei Wang4114c272014-11-05 10:53:43 +08005698 if (vector == -1)
5699 vector = 0;
5700
Yang Zhangc7c9c562013-01-25 10:18:51 +08005701 status = vmcs_read16(GUEST_INTR_STATUS);
5702 old = (u8)status & 0xff;
5703 if ((u8)vector != old) {
5704 status &= ~0xff;
5705 status |= (u8)vector;
5706 vmcs_write16(GUEST_INTR_STATUS, status);
5707 }
5708}
5709
5710static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5711{
Liran Alon851c1a182017-12-24 18:12:56 +02005712 /*
5713 * When running L2, updating RVI is only relevant when
5714 * vmcs12 virtual-interrupt-delivery enabled.
5715 * However, it can be enabled only when L1 also
5716 * intercepts external-interrupts and in that case
5717 * we should not update vmcs02 RVI but instead intercept
5718 * interrupt. Therefore, do nothing when running L2.
5719 */
5720 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08005721 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005722}
5723
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005724static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005725{
5726 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005727 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02005728 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005729
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005730 WARN_ON(!vcpu->arch.apicv_active);
5731 if (pi_test_on(&vmx->pi_desc)) {
5732 pi_clear_on(&vmx->pi_desc);
5733 /*
5734 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
5735 * But on x86 this is just a compiler barrier anyway.
5736 */
5737 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02005738 max_irr_updated =
5739 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
5740
5741 /*
5742 * If we are running L2 and L1 has a new pending interrupt
5743 * which can be injected, we should re-evaluate
5744 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02005745 * If L1 intercepts external-interrupts, we should
5746 * exit from L2 to L1. Otherwise, interrupt should be
5747 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02005748 */
Liran Alon851c1a182017-12-24 18:12:56 +02005749 if (is_guest_mode(vcpu) && max_irr_updated) {
5750 if (nested_exit_on_intr(vcpu))
5751 kvm_vcpu_exiting_guest_mode(vcpu);
5752 else
5753 kvm_make_request(KVM_REQ_EVENT, vcpu);
5754 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005755 } else {
5756 max_irr = kvm_lapic_find_highest_irr(vcpu);
5757 }
5758 vmx_hwapic_irr_update(vcpu, max_irr);
5759 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005760}
5761
Andrey Smetanin63086302015-11-10 15:36:32 +03005762static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005763{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005764 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08005765 return;
5766
Yang Zhangc7c9c562013-01-25 10:18:51 +08005767 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
5768 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
5769 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
5770 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
5771}
5772
Paolo Bonzini967235d2016-12-19 14:03:45 +01005773static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
5774{
5775 struct vcpu_vmx *vmx = to_vmx(vcpu);
5776
5777 pi_clear_on(&vmx->pi_desc);
5778 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
5779}
5780
Avi Kivity51aa01d2010-07-20 14:31:20 +03005781static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03005782{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005783 u32 exit_intr_info = 0;
5784 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02005785
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005786 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
5787 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02005788 return;
5789
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005790 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
5791 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5792 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08005793
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005794 /* if exit due to PF check for async PF */
5795 if (is_page_fault(exit_intr_info))
5796 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
5797
Andi Kleena0861c02009-06-08 17:37:09 +08005798 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005799 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
5800 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08005801 kvm_machine_check();
5802
Gleb Natapov20f65982009-05-11 13:35:55 +03005803 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08005804 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07005805 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03005806 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07005807 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005808 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03005809}
Gleb Natapov20f65982009-05-11 13:35:55 +03005810
Yang Zhanga547c6d2013-04-11 19:25:10 +08005811static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
5812{
5813 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5814
Yang Zhanga547c6d2013-04-11 19:25:10 +08005815 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
5816 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
5817 unsigned int vector;
5818 unsigned long entry;
5819 gate_desc *desc;
5820 struct vcpu_vmx *vmx = to_vmx(vcpu);
5821#ifdef CONFIG_X86_64
5822 unsigned long tmp;
5823#endif
5824
5825 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
5826 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02005827 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08005828 asm volatile(
5829#ifdef CONFIG_X86_64
5830 "mov %%" _ASM_SP ", %[sp]\n\t"
5831 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
5832 "push $%c[ss]\n\t"
5833 "push %[sp]\n\t"
5834#endif
5835 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08005836 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01005837 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08005838 :
5839#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06005840 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08005841#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05005842 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08005843 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01005844 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08005845 [ss]"i"(__KERNEL_DS),
5846 [cs]"i"(__KERNEL_CS)
5847 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005848 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08005849}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005850STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08005851
Tom Lendackybc226f02018-05-10 22:06:39 +02005852static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005853{
Tom Lendackybc226f02018-05-10 22:06:39 +02005854 switch (index) {
5855 case MSR_IA32_SMBASE:
5856 /*
5857 * We cannot do SMM unless we can run the guest in big
5858 * real mode.
5859 */
5860 return enable_unrestricted_guest || emulate_invalid_guest_state;
5861 case MSR_AMD64_VIRT_SPEC_CTRL:
5862 /* This is AMD only. */
5863 return false;
5864 default:
5865 return true;
5866 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005867}
5868
Avi Kivity51aa01d2010-07-20 14:31:20 +03005869static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
5870{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02005871 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03005872 bool unblock_nmi;
5873 u8 vector;
5874 bool idtv_info_valid;
5875
5876 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03005877
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005878 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005879 if (vmx->loaded_vmcs->nmi_known_unmasked)
5880 return;
5881 /*
5882 * Can't use vmx->exit_intr_info since we're not sure what
5883 * the exit reason is.
5884 */
5885 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5886 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
5887 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
5888 /*
5889 * SDM 3: 27.7.1.2 (September 2008)
5890 * Re-set bit "block by NMI" before VM entry if vmexit caused by
5891 * a guest IRET fault.
5892 * SDM 3: 23.2.2 (September 2008)
5893 * Bit 12 is undefined in any of the following cases:
5894 * If the VM exit sets the valid bit in the IDT-vectoring
5895 * information field.
5896 * If the VM exit is due to a double fault.
5897 */
5898 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
5899 vector != DF_VECTOR && !idtv_info_valid)
5900 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5901 GUEST_INTR_STATE_NMI);
5902 else
5903 vmx->loaded_vmcs->nmi_known_unmasked =
5904 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
5905 & GUEST_INTR_STATE_NMI);
5906 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
5907 vmx->loaded_vmcs->vnmi_blocked_time +=
5908 ktime_to_ns(ktime_sub(ktime_get(),
5909 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03005910}
5911
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005912static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03005913 u32 idt_vectoring_info,
5914 int instr_len_field,
5915 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03005916{
Avi Kivity51aa01d2010-07-20 14:31:20 +03005917 u8 vector;
5918 int type;
5919 bool idtv_info_valid;
5920
5921 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03005922
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005923 vcpu->arch.nmi_injected = false;
5924 kvm_clear_exception_queue(vcpu);
5925 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005926
5927 if (!idtv_info_valid)
5928 return;
5929
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005930 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03005931
Avi Kivity668f6122008-07-02 09:28:55 +03005932 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
5933 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03005934
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005935 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03005936 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005937 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03005938 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03005939 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03005940 * Clear bit "block by NMI" before VM entry if a NMI
5941 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03005942 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005943 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005944 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03005945 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005946 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005947 /* fall through */
5948 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03005949 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03005950 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03005951 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03005952 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03005953 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005954 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005955 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005956 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005957 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03005958 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005959 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005960 break;
5961 default:
5962 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03005963 }
Avi Kivitycf393f72008-07-01 16:20:21 +03005964}
5965
Avi Kivity83422e12010-07-20 14:43:23 +03005966static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
5967{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005968 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03005969 VM_EXIT_INSTRUCTION_LEN,
5970 IDT_VECTORING_ERROR_CODE);
5971}
5972
Avi Kivityb463a6f2010-07-20 15:06:17 +03005973static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
5974{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005975 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005976 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5977 VM_ENTRY_INSTRUCTION_LEN,
5978 VM_ENTRY_EXCEPTION_ERROR_CODE);
5979
5980 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
5981}
5982
Gleb Natapovd7cd9792011-10-05 14:01:23 +02005983static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
5984{
5985 int i, nr_msrs;
5986 struct perf_guest_switch_msr *msrs;
5987
5988 msrs = perf_guest_get_msrs(&nr_msrs);
5989
5990 if (!msrs)
5991 return;
5992
5993 for (i = 0; i < nr_msrs; i++)
5994 if (msrs[i].host == msrs[i].guest)
5995 clear_atomic_switch_msr(vmx, msrs[i].msr);
5996 else
5997 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04005998 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02005999}
6000
Sean Christophersonf459a702018-08-27 15:21:11 -07006001static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
6002{
6003 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
6004 if (!vmx->loaded_vmcs->hv_timer_armed)
6005 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
6006 PIN_BASED_VMX_PREEMPTION_TIMER);
6007 vmx->loaded_vmcs->hv_timer_armed = true;
6008}
6009
6010static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006011{
6012 struct vcpu_vmx *vmx = to_vmx(vcpu);
6013 u64 tscl;
6014 u32 delta_tsc;
6015
Sean Christophersond264ee02018-08-27 15:21:12 -07006016 if (vmx->req_immediate_exit) {
6017 vmx_arm_hv_timer(vmx, 0);
6018 return;
6019 }
6020
Sean Christophersonf459a702018-08-27 15:21:11 -07006021 if (vmx->hv_deadline_tsc != -1) {
6022 tscl = rdtsc();
6023 if (vmx->hv_deadline_tsc > tscl)
6024 /* set_hv_timer ensures the delta fits in 32-bits */
6025 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6026 cpu_preemption_timer_multi);
6027 else
6028 delta_tsc = 0;
6029
6030 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -07006031 return;
Sean Christophersonf459a702018-08-27 15:21:11 -07006032 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006033
Sean Christophersonf459a702018-08-27 15:21:11 -07006034 if (vmx->loaded_vmcs->hv_timer_armed)
6035 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
6036 PIN_BASED_VMX_PREEMPTION_TIMER);
6037 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006038}
6039
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08006040static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006041{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006042 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006043 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02006044
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006045 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006046 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006047 vmx->loaded_vmcs->soft_vnmi_blocked))
6048 vmx->loaded_vmcs->entry_time = ktime_get();
6049
Avi Kivity104f2262010-11-18 13:12:52 +02006050 /* Don't enter VMX if guest state is invalid, let the exit handler
6051 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02006052 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02006053 return;
6054
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006055 if (vmx->ple_window_dirty) {
6056 vmx->ple_window_dirty = false;
6057 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6058 }
6059
Sean Christophersonff241482018-12-03 13:53:14 -08006060 if (vmx->nested.need_vmcs12_sync)
6061 nested_sync_from_vmcs12(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03006062
Avi Kivity104f2262010-11-18 13:12:52 +02006063 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6064 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6065 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6066 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6067
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006068 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -07006069 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006070 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -07006071 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006072 }
6073
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006074 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -07006075 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006076 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -07006077 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006078 }
6079
Avi Kivity104f2262010-11-18 13:12:52 +02006080 /* When single-stepping over STI and MOV SS, we must clear the
6081 * corresponding interruptibility bits in the guest state. Otherwise
6082 * vmentry fails as it then expects bit 14 (BS) in pending debug
6083 * exceptions being set, but that's not correct for the guest debugging
6084 * case. */
6085 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6086 vmx_set_interrupt_shadow(vcpu, 0);
6087
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02006088 if (static_cpu_has(X86_FEATURE_PKU) &&
6089 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
6090 vcpu->arch.pkru != vmx->host_pkru)
6091 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006092
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006093 atomic_switch_perf_msrs(vmx);
6094
Sean Christophersonf459a702018-08-27 15:21:11 -07006095 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07006096
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006097 /*
6098 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6099 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6100 * is no need to worry about the conditional branch over the wrmsr
6101 * being speculatively taken.
6102 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006103 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006104
Nadav Har'Eld462b812011-05-24 15:26:10 +03006105 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006106
6107 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
6108 (unsigned long)&current_evmcs->host_rsp : 0;
6109
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006110 if (static_branch_unlikely(&vmx_l1d_should_flush))
6111 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006112
Avi Kivity104f2262010-11-18 13:12:52 +02006113 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08006114 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006115 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6116 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6117 "push %%" _ASM_CX " \n\t"
6118 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006119 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006120 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006121 /* Avoid VMWRITE when Enlightened VMCS is in use */
6122 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
6123 "jz 2f \n\t"
6124 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
6125 "jmp 1f \n\t"
6126 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006127 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006128 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006129 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006130 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6131 "mov %%cr2, %%" _ASM_DX " \n\t"
6132 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006133 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006134 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006135 "3: \n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +02006136 /* Check if vmlaunch or vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02006137 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006138 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006139 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6140 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6141 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6142 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6143 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6144 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006145#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006146 "mov %c[r8](%0), %%r8 \n\t"
6147 "mov %c[r9](%0), %%r9 \n\t"
6148 "mov %c[r10](%0), %%r10 \n\t"
6149 "mov %c[r11](%0), %%r11 \n\t"
6150 "mov %c[r12](%0), %%r12 \n\t"
6151 "mov %c[r13](%0), %%r13 \n\t"
6152 "mov %c[r14](%0), %%r14 \n\t"
6153 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006154#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006155 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03006156
Avi Kivity6aa8b732006-12-10 02:21:36 -08006157 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03006158 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006159 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006160 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006161 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006162 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08006163 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006164 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02006165 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08006166 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006167 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6168 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6169 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6170 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6171 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6172 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6173 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006174#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006175 "mov %%r8, %c[r8](%0) \n\t"
6176 "mov %%r9, %c[r9](%0) \n\t"
6177 "mov %%r10, %c[r10](%0) \n\t"
6178 "mov %%r11, %c[r11](%0) \n\t"
6179 "mov %%r12, %c[r12](%0) \n\t"
6180 "mov %%r13, %c[r13](%0) \n\t"
6181 "mov %%r14, %c[r14](%0) \n\t"
6182 "mov %%r15, %c[r15](%0) \n\t"
Uros Bizjak43ce76c2018-10-17 16:46:57 +02006183 /*
6184 * Clear host registers marked as clobbered to prevent
6185 * speculative use.
6186 */
Jim Mattson0cb5b302018-01-03 14:31:38 -08006187 "xor %%r8d, %%r8d \n\t"
6188 "xor %%r9d, %%r9d \n\t"
6189 "xor %%r10d, %%r10d \n\t"
6190 "xor %%r11d, %%r11d \n\t"
6191 "xor %%r12d, %%r12d \n\t"
6192 "xor %%r13d, %%r13d \n\t"
6193 "xor %%r14d, %%r14d \n\t"
6194 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006195#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006196 "mov %%cr2, %%" _ASM_AX " \n\t"
6197 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006198
Jim Mattson0cb5b302018-01-03 14:31:38 -08006199 "xor %%eax, %%eax \n\t"
6200 "xor %%ebx, %%ebx \n\t"
6201 "xor %%esi, %%esi \n\t"
6202 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006203 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006204 ".pushsection .rodata \n\t"
6205 ".global vmx_return \n\t"
6206 "vmx_return: " _ASM_PTR " 2b \n\t"
6207 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006208 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03006209 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02006210 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03006211 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006212 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6213 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6214 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6215 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6216 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6217 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6218 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006219#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006220 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6221 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6222 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6223 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6224 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6225 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6226 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6227 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08006228#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02006229 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6230 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02006231 : "cc", "memory"
6232#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006233 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006234 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006235#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006236 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006237#endif
6238 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006240 /*
6241 * We do not use IBRS in the kernel. If this vCPU has used the
6242 * SPEC_CTRL MSR it may have left it on; save the value and
6243 * turn it off. This is much more efficient than blindly adding
6244 * it to the atomic save/restore list. Especially as the former
6245 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6246 *
6247 * For non-nested case:
6248 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6249 * save it.
6250 *
6251 * For nested case:
6252 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6253 * save it.
6254 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006255 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006256 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006257
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006258 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006259
David Woodhouse117cc7a2018-01-12 11:11:27 +00006260 /* Eliminate branch target predictions from guest mode */
6261 vmexit_fill_RSB();
6262
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006263 /* All fields are clean at this point */
6264 if (static_branch_unlikely(&enable_evmcs))
6265 current_evmcs->hv_clean_fields |=
6266 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6267
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006268 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006269 if (vmx->host_debugctlmsr)
6270 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006271
Avi Kivityaa67f602012-08-01 16:48:03 +03006272#ifndef CONFIG_X86_64
6273 /*
6274 * The sysexit path does not restore ds/es, so we must set them to
6275 * a reasonable value ourselves.
6276 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006277 * We can't defer this to vmx_prepare_switch_to_host() since that
6278 * function may be executed in interrupt context, which saves and
6279 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006280 */
6281 loadsegment(ds, __USER_DS);
6282 loadsegment(es, __USER_DS);
6283#endif
6284
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006285 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02006286 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006287 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03006288 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006289 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006290 vcpu->arch.regs_dirty = 0;
6291
Gleb Natapove0b890d2013-09-25 12:51:33 +03006292 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006293 * eager fpu is enabled if PKEY is supported and CR4 is switched
6294 * back on host, so it is safe to read guest PKRU from current
6295 * XSAVE.
6296 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02006297 if (static_cpu_has(X86_FEATURE_PKU) &&
6298 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
6299 vcpu->arch.pkru = __read_pkru();
6300 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006301 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006302 }
6303
Gleb Natapove0b890d2013-09-25 12:51:33 +03006304 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006305 vmx->idt_vectoring_info = 0;
6306
6307 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
6308 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
6309 return;
6310
6311 vmx->loaded_vmcs->launched = 1;
6312 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006313
Avi Kivity51aa01d2010-07-20 14:31:20 +03006314 vmx_complete_atomic_exit(vmx);
6315 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006316 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05006318STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319
Sean Christopherson434a1e92018-03-20 12:17:18 -07006320static struct kvm *vmx_vm_alloc(void)
6321{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07006322 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006323 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07006324}
6325
6326static void vmx_vm_free(struct kvm *kvm)
6327{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07006328 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07006329}
6330
Avi Kivity6aa8b732006-12-10 02:21:36 -08006331static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6332{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006333 struct vcpu_vmx *vmx = to_vmx(vcpu);
6334
Kai Huang843e4332015-01-28 10:54:28 +08006335 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006336 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006337 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006338 leave_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006339 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006340 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006341 kfree(vmx->guest_msrs);
6342 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10006343 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006344}
6345
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006346static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006347{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006348 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10006349 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006350 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03006351 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006352
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006353 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006354 return ERR_PTR(-ENOMEM);
6355
Wanpeng Li991e7a02015-09-16 17:30:05 +08006356 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08006357
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006358 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6359 if (err)
6360 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006361
Peter Feiner4e595162016-07-07 14:49:58 -07006362 err = -ENOMEM;
6363
6364 /*
6365 * If PML is turned on, failure on enabling PML just results in failure
6366 * of creating the vcpu, therefore we can simplify PML logic (by
6367 * avoiding dealing with cases, such as enabling PML partially on vcpus
6368 * for the guest, etc.
6369 */
6370 if (enable_pml) {
6371 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
6372 if (!vmx->pml_pg)
6373 goto uninit_vcpu;
6374 }
6375
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006376 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02006377 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
6378 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03006379
Peter Feiner4e595162016-07-07 14:49:58 -07006380 if (!vmx->guest_msrs)
6381 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006382
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006383 err = alloc_loaded_vmcs(&vmx->vmcs01);
6384 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006385 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006386
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006387 msr_bitmap = vmx->vmcs01.msr_bitmap;
6388 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
6389 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
6390 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6391 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6392 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6393 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
6394 vmx->msr_bitmap_mode = 0;
6395
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006396 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006397 cpu = get_cpu();
6398 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10006399 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02006400 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006401 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006402 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02006403 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006404 err = alloc_apic_access_page(kvm);
6405 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006406 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006407 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006408
Sean Christophersone90008d2018-03-05 12:04:37 -08006409 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +08006410 err = init_rmode_identity_map(kvm);
6411 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006412 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006413 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006414
Roman Kagan63aff652018-07-19 21:59:07 +03006415 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006416 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
Sean Christopherson7caaa712018-12-03 13:53:01 -08006417 vmx_capability.ept,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006418 kvm_vcpu_apicv_active(&vmx->vcpu));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006419 else
6420 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006421
Wincy Van705699a2015-02-03 23:58:17 +08006422 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006423 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006424
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006425 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
6426
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02006427 /*
6428 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
6429 * or POSTED_INTR_WAKEUP_VECTOR.
6430 */
6431 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
6432 vmx->pi_desc.sn = 1;
6433
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006434 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006435
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006436free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006437 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006438free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006439 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07006440free_pml:
6441 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006442uninit_vcpu:
6443 kvm_vcpu_uninit(&vmx->vcpu);
6444free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08006445 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10006446 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006447 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006448}
6449
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006450#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"
6451#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 -04006452
Wanpeng Lib31c1142018-03-12 04:53:04 -07006453static int vmx_vm_init(struct kvm *kvm)
6454{
Tianyu Lan877ad952018-07-19 08:40:23 +00006455 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
6456
Wanpeng Lib31c1142018-03-12 04:53:04 -07006457 if (!ple_gap)
6458 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006459
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006460 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
6461 switch (l1tf_mitigation) {
6462 case L1TF_MITIGATION_OFF:
6463 case L1TF_MITIGATION_FLUSH_NOWARN:
6464 /* 'I explicitly don't care' is set */
6465 break;
6466 case L1TF_MITIGATION_FLUSH:
6467 case L1TF_MITIGATION_FLUSH_NOSMT:
6468 case L1TF_MITIGATION_FULL:
6469 /*
6470 * Warn upon starting the first VM in a potentially
6471 * insecure environment.
6472 */
6473 if (cpu_smt_control == CPU_SMT_ENABLED)
6474 pr_warn_once(L1TF_MSG_SMT);
6475 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
6476 pr_warn_once(L1TF_MSG_L1D);
6477 break;
6478 case L1TF_MITIGATION_FULL_FORCE:
6479 /* Flush is enforced */
6480 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006481 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006482 }
Wanpeng Lib31c1142018-03-12 04:53:04 -07006483 return 0;
6484}
6485
Yang, Sheng002c7f72007-07-31 14:23:01 +03006486static void __init vmx_check_processor_compat(void *rtn)
6487{
6488 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006489 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006490
6491 *(int *)rtn = 0;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006492 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03006493 *(int *)rtn = -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006494 if (nested)
6495 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept,
6496 enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +03006497 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6498 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6499 smp_processor_id());
6500 *(int *)rtn = -EIO;
6501 }
6502}
6503
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006504static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006505{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006506 u8 cache;
6507 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006508
Sheng Yang522c68c2009-04-27 20:35:43 +08006509 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02006510 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08006511 * 2. EPT with VT-d:
6512 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02006513 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08006514 * b. VT-d with snooping control feature: snooping control feature of
6515 * VT-d engine can guarantee the cache correctness. Just set it
6516 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08006517 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08006518 * consistent with host MTRR
6519 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02006520 if (is_mmio) {
6521 cache = MTRR_TYPE_UNCACHABLE;
6522 goto exit;
6523 }
6524
6525 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006526 ipat = VMX_EPT_IPAT_BIT;
6527 cache = MTRR_TYPE_WRBACK;
6528 goto exit;
6529 }
6530
6531 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
6532 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02006533 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08006534 cache = MTRR_TYPE_WRBACK;
6535 else
6536 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006537 goto exit;
6538 }
6539
Xiao Guangrongff536042015-06-15 16:55:22 +08006540 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006541
6542exit:
6543 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08006544}
6545
Sheng Yang17cc3932010-01-05 19:02:27 +08006546static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02006547{
Sheng Yang878403b2010-01-05 19:02:29 +08006548 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6549 return PT_DIRECTORY_LEVEL;
6550 else
6551 /* For shadow and EPT supported 1GB page */
6552 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02006553}
6554
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006555static void vmcs_set_secondary_exec_control(u32 new_ctl)
6556{
6557 /*
6558 * These bits in the secondary execution controls field
6559 * are dynamic, the others are mostly based on the hypervisor
6560 * architecture and the guest's CPUID. Do not touch the
6561 * dynamic bits.
6562 */
6563 u32 mask =
6564 SECONDARY_EXEC_SHADOW_VMCS |
6565 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02006566 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6567 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006568
6569 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6570
6571 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6572 (new_ctl & ~mask) | (cur_ctl & mask));
6573}
6574
David Matlack8322ebb2016-11-29 18:14:09 -08006575/*
6576 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
6577 * (indicating "allowed-1") if they are supported in the guest's CPUID.
6578 */
6579static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
6580{
6581 struct vcpu_vmx *vmx = to_vmx(vcpu);
6582 struct kvm_cpuid_entry2 *entry;
6583
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006584 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
6585 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08006586
6587#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
6588 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006589 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08006590} while (0)
6591
6592 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
6593 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
6594 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
6595 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
6596 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
6597 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
6598 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
6599 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
6600 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
6601 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
6602 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
6603 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
6604 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
6605 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
6606 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
6607
6608 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
6609 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
6610 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
6611 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
6612 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01006613 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08006614
6615#undef cr4_fixed1_update
6616}
6617
Liran Alon5f76f6f2018-09-14 03:25:52 +03006618static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
6619{
6620 struct vcpu_vmx *vmx = to_vmx(vcpu);
6621
6622 if (kvm_mpx_supported()) {
6623 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
6624
6625 if (mpx_enabled) {
6626 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
6627 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
6628 } else {
6629 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
6630 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
6631 }
6632 }
6633}
6634
Sheng Yang0e851882009-12-18 16:48:46 +08006635static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
6636{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006637 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006638
Paolo Bonzini80154d72017-08-24 13:55:35 +02006639 if (cpu_has_secondary_exec_ctrls()) {
6640 vmx_compute_secondary_exec_control(vmx);
6641 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006642 }
Mao, Junjiead756a12012-07-02 01:18:48 +00006643
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006644 if (nested_vmx_allowed(vcpu))
6645 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
6646 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
6647 else
6648 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
6649 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08006650
Liran Alon5f76f6f2018-09-14 03:25:52 +03006651 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08006652 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03006653 nested_vmx_entry_exit_ctls_update(vcpu);
6654 }
Sheng Yang0e851882009-12-18 16:48:46 +08006655}
6656
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006657static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
6658{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03006659 if (func == 1 && nested)
6660 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006661}
6662
Sean Christophersond264ee02018-08-27 15:21:12 -07006663static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
6664{
6665 to_vmx(vcpu)->req_immediate_exit = true;
6666}
6667
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006668static int vmx_check_intercept(struct kvm_vcpu *vcpu,
6669 struct x86_instruction_info *info,
6670 enum x86_intercept_stage stage)
6671{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02006672 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6673 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
6674
6675 /*
6676 * RDPID causes #UD if disabled through secondary execution controls.
6677 * Because it is marked as EmulateOnUD, we need to intercept it here.
6678 */
6679 if (info->intercept == x86_intercept_rdtscp &&
6680 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
6681 ctxt->exception.vector = UD_VECTOR;
6682 ctxt->exception.error_code_valid = false;
6683 return X86EMUL_PROPAGATE_FAULT;
6684 }
6685
6686 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006687 return X86EMUL_CONTINUE;
6688}
6689
Yunhong Jiang64672c92016-06-13 14:19:59 -07006690#ifdef CONFIG_X86_64
6691/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
6692static inline int u64_shl_div_u64(u64 a, unsigned int shift,
6693 u64 divisor, u64 *result)
6694{
6695 u64 low = a << shift, high = a >> (64 - shift);
6696
6697 /* To avoid the overflow on divq */
6698 if (high >= divisor)
6699 return 1;
6700
6701 /* Low hold the result, high hold rem which is discarded */
6702 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
6703 "rm" (divisor), "0" (low), "1" (high));
6704 *result = low;
6705
6706 return 0;
6707}
6708
6709static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
6710{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02006711 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08006712 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02006713
6714 if (kvm_mwait_in_guest(vcpu->kvm))
6715 return -EOPNOTSUPP;
6716
6717 vmx = to_vmx(vcpu);
6718 tscl = rdtsc();
6719 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
6720 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08006721 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
6722
6723 if (delta_tsc > lapic_timer_advance_cycles)
6724 delta_tsc -= lapic_timer_advance_cycles;
6725 else
6726 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006727
6728 /* Convert to host delta tsc if tsc scaling is enabled */
6729 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
6730 u64_shl_div_u64(delta_tsc,
6731 kvm_tsc_scaling_ratio_frac_bits,
6732 vcpu->arch.tsc_scaling_ratio,
6733 &delta_tsc))
6734 return -ERANGE;
6735
6736 /*
6737 * If the delta tsc can't fit in the 32 bit after the multi shift,
6738 * we can't use the preemption timer.
6739 * It's possible that it fits on later vmentries, but checking
6740 * on every vmentry is costly so we just use an hrtimer.
6741 */
6742 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
6743 return -ERANGE;
6744
6745 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -07006746 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006747}
6748
6749static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
6750{
Sean Christophersonf459a702018-08-27 15:21:11 -07006751 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006752}
6753#endif
6754
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006755static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006756{
Wanpeng Lib31c1142018-03-12 04:53:04 -07006757 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006758 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006759}
6760
Kai Huang843e4332015-01-28 10:54:28 +08006761static void vmx_slot_enable_log_dirty(struct kvm *kvm,
6762 struct kvm_memory_slot *slot)
6763{
6764 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
6765 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
6766}
6767
6768static void vmx_slot_disable_log_dirty(struct kvm *kvm,
6769 struct kvm_memory_slot *slot)
6770{
6771 kvm_mmu_slot_set_dirty(kvm, slot);
6772}
6773
6774static void vmx_flush_log_dirty(struct kvm *kvm)
6775{
6776 kvm_flush_pml_buffers(kvm);
6777}
6778
Bandan Dasc5f983f2017-05-05 15:25:14 -04006779static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
6780{
6781 struct vmcs12 *vmcs12;
6782 struct vcpu_vmx *vmx = to_vmx(vcpu);
6783 gpa_t gpa;
6784 struct page *page = NULL;
6785 u64 *pml_address;
6786
6787 if (is_guest_mode(vcpu)) {
6788 WARN_ON_ONCE(vmx->nested.pml_full);
6789
6790 /*
6791 * Check if PML is enabled for the nested guest.
6792 * Whether eptp bit 6 is set is already checked
6793 * as part of A/D emulation.
6794 */
6795 vmcs12 = get_vmcs12(vcpu);
6796 if (!nested_cpu_has_pml(vmcs12))
6797 return 0;
6798
Dan Carpenter47698862017-05-10 22:43:17 +03006799 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -04006800 vmx->nested.pml_full = true;
6801 return 1;
6802 }
6803
6804 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
6805
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02006806 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
6807 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -04006808 return 0;
6809
6810 pml_address = kmap(page);
6811 pml_address[vmcs12->guest_pml_index--] = gpa;
6812 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02006813 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -04006814 }
6815
6816 return 0;
6817}
6818
Kai Huang843e4332015-01-28 10:54:28 +08006819static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
6820 struct kvm_memory_slot *memslot,
6821 gfn_t offset, unsigned long mask)
6822{
6823 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
6824}
6825
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006826static void __pi_post_block(struct kvm_vcpu *vcpu)
6827{
6828 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6829 struct pi_desc old, new;
6830 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006831
6832 do {
6833 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006834 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
6835 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006836
6837 dest = cpu_physical_id(vcpu->cpu);
6838
6839 if (x2apic_enabled())
6840 new.ndst = dest;
6841 else
6842 new.ndst = (dest << 8) & 0xFF00;
6843
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006844 /* set 'NV' to 'notification vector' */
6845 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02006846 } while (cmpxchg64(&pi_desc->control, old.control,
6847 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006848
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006849 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
6850 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006851 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006852 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006853 vcpu->pre_pcpu = -1;
6854 }
6855}
6856
Feng Wuefc64402015-09-18 22:29:51 +08006857/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08006858 * This routine does the following things for vCPU which is going
6859 * to be blocked if VT-d PI is enabled.
6860 * - Store the vCPU to the wakeup list, so when interrupts happen
6861 * we can find the right vCPU to wake up.
6862 * - Change the Posted-interrupt descriptor as below:
6863 * 'NDST' <-- vcpu->pre_pcpu
6864 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
6865 * - If 'ON' is set during this process, which means at least one
6866 * interrupt is posted for this vCPU, we cannot block it, in
6867 * this case, return 1, otherwise, return 0.
6868 *
6869 */
Yunhong Jiangbc225122016-06-13 14:19:58 -07006870static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006871{
Feng Wubf9f6ac2015-09-18 22:29:55 +08006872 unsigned int dest;
6873 struct pi_desc old, new;
6874 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6875
6876 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08006877 !irq_remapping_cap(IRQ_POSTING_CAP) ||
6878 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +08006879 return 0;
6880
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006881 WARN_ON(irqs_disabled());
6882 local_irq_disable();
6883 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
6884 vcpu->pre_pcpu = vcpu->cpu;
6885 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
6886 list_add_tail(&vcpu->blocked_vcpu_list,
6887 &per_cpu(blocked_vcpu_on_cpu,
6888 vcpu->pre_pcpu));
6889 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
6890 }
Feng Wubf9f6ac2015-09-18 22:29:55 +08006891
6892 do {
6893 old.control = new.control = pi_desc->control;
6894
Feng Wubf9f6ac2015-09-18 22:29:55 +08006895 WARN((pi_desc->sn == 1),
6896 "Warning: SN field of posted-interrupts "
6897 "is set before blocking\n");
6898
6899 /*
6900 * Since vCPU can be preempted during this process,
6901 * vcpu->cpu could be different with pre_pcpu, we
6902 * need to set pre_pcpu as the destination of wakeup
6903 * notification event, then we can find the right vCPU
6904 * to wakeup in wakeup handler if interrupts happen
6905 * when the vCPU is in blocked state.
6906 */
6907 dest = cpu_physical_id(vcpu->pre_pcpu);
6908
6909 if (x2apic_enabled())
6910 new.ndst = dest;
6911 else
6912 new.ndst = (dest << 8) & 0xFF00;
6913
6914 /* set 'NV' to 'wakeup vector' */
6915 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02006916 } while (cmpxchg64(&pi_desc->control, old.control,
6917 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006918
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006919 /* We should not block the vCPU if an interrupt is posted for it. */
6920 if (pi_test_on(pi_desc) == 1)
6921 __pi_post_block(vcpu);
6922
6923 local_irq_enable();
6924 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006925}
6926
Yunhong Jiangbc225122016-06-13 14:19:58 -07006927static int vmx_pre_block(struct kvm_vcpu *vcpu)
6928{
6929 if (pi_pre_block(vcpu))
6930 return 1;
6931
Yunhong Jiang64672c92016-06-13 14:19:59 -07006932 if (kvm_lapic_hv_timer_in_use(vcpu))
6933 kvm_lapic_switch_to_sw_timer(vcpu);
6934
Yunhong Jiangbc225122016-06-13 14:19:58 -07006935 return 0;
6936}
6937
6938static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006939{
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006940 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006941 return;
6942
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006943 WARN_ON(irqs_disabled());
6944 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006945 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006946 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +08006947}
6948
Yunhong Jiangbc225122016-06-13 14:19:58 -07006949static void vmx_post_block(struct kvm_vcpu *vcpu)
6950{
Yunhong Jiang64672c92016-06-13 14:19:59 -07006951 if (kvm_x86_ops->set_hv_timer)
6952 kvm_lapic_switch_to_hv_timer(vcpu);
6953
Yunhong Jiangbc225122016-06-13 14:19:58 -07006954 pi_post_block(vcpu);
6955}
6956
Feng Wubf9f6ac2015-09-18 22:29:55 +08006957/*
Feng Wuefc64402015-09-18 22:29:51 +08006958 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
6959 *
6960 * @kvm: kvm
6961 * @host_irq: host irq of the interrupt
6962 * @guest_irq: gsi of the interrupt
6963 * @set: set or unset PI
6964 * returns 0 on success, < 0 on failure
6965 */
6966static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
6967 uint32_t guest_irq, bool set)
6968{
6969 struct kvm_kernel_irq_routing_entry *e;
6970 struct kvm_irq_routing_table *irq_rt;
6971 struct kvm_lapic_irq irq;
6972 struct kvm_vcpu *vcpu;
6973 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01006974 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +08006975
6976 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08006977 !irq_remapping_cap(IRQ_POSTING_CAP) ||
6978 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +08006979 return 0;
6980
6981 idx = srcu_read_lock(&kvm->irq_srcu);
6982 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01006983 if (guest_irq >= irq_rt->nr_rt_entries ||
6984 hlist_empty(&irq_rt->map[guest_irq])) {
6985 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
6986 guest_irq, irq_rt->nr_rt_entries);
6987 goto out;
6988 }
Feng Wuefc64402015-09-18 22:29:51 +08006989
6990 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
6991 if (e->type != KVM_IRQ_ROUTING_MSI)
6992 continue;
6993 /*
6994 * VT-d PI cannot support posting multicast/broadcast
6995 * interrupts to a vCPU, we still use interrupt remapping
6996 * for these kind of interrupts.
6997 *
6998 * For lowest-priority interrupts, we only support
6999 * those with single CPU as the destination, e.g. user
7000 * configures the interrupts via /proc/irq or uses
7001 * irqbalance to make the interrupts single-CPU.
7002 *
7003 * We will support full lowest-priority interrupt later.
7004 */
7005
Radim Krčmář371313132016-07-12 22:09:27 +02007006 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +08007007 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
7008 /*
7009 * Make sure the IRTE is in remapped mode if
7010 * we don't handle it in posted mode.
7011 */
7012 ret = irq_set_vcpu_affinity(host_irq, NULL);
7013 if (ret < 0) {
7014 printk(KERN_INFO
7015 "failed to back to remapped mode, irq: %u\n",
7016 host_irq);
7017 goto out;
7018 }
7019
Feng Wuefc64402015-09-18 22:29:51 +08007020 continue;
Feng Wu23a1c252016-01-25 16:53:32 +08007021 }
Feng Wuefc64402015-09-18 22:29:51 +08007022
7023 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
7024 vcpu_info.vector = irq.vector;
7025
hu huajun2698d822018-04-11 15:16:40 +08007026 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +08007027 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
7028
7029 if (set)
7030 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +08007031 else
Feng Wuefc64402015-09-18 22:29:51 +08007032 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +08007033
7034 if (ret < 0) {
7035 printk(KERN_INFO "%s: failed to update PI IRTE\n",
7036 __func__);
7037 goto out;
7038 }
7039 }
7040
7041 ret = 0;
7042out:
7043 srcu_read_unlock(&kvm->irq_srcu, idx);
7044 return ret;
7045}
7046
Ashok Rajc45dcc72016-06-22 14:59:56 +08007047static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7048{
7049 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7050 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
7051 FEATURE_CONTROL_LMCE;
7052 else
7053 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
7054 ~FEATURE_CONTROL_LMCE;
7055}
7056
Ladi Prosek72d7b372017-10-11 16:54:41 +02007057static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
7058{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007059 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7060 if (to_vmx(vcpu)->nested.nested_run_pending)
7061 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +02007062 return 1;
7063}
7064
Ladi Prosek0234bf82017-10-11 16:54:40 +02007065static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7066{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007067 struct vcpu_vmx *vmx = to_vmx(vcpu);
7068
7069 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7070 if (vmx->nested.smm.guest_mode)
7071 nested_vmx_vmexit(vcpu, -1, 0, 0);
7072
7073 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7074 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007075 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007076 return 0;
7077}
7078
7079static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
7080{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007081 struct vcpu_vmx *vmx = to_vmx(vcpu);
7082 int ret;
7083
7084 if (vmx->nested.smm.vmxon) {
7085 vmx->nested.vmxon = true;
7086 vmx->nested.smm.vmxon = false;
7087 }
7088
7089 if (vmx->nested.smm.guest_mode) {
7090 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -07007091 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007092 vcpu->arch.hflags |= HF_SMM_MASK;
7093 if (ret)
7094 return ret;
7095
7096 vmx->nested.smm.guest_mode = false;
7097 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007098 return 0;
7099}
7100
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007101static int enable_smi_window(struct kvm_vcpu *vcpu)
7102{
7103 return 0;
7104}
7105
Sean Christophersona3203382018-12-03 13:53:11 -08007106static __init int hardware_setup(void)
7107{
7108 unsigned long host_bndcfgs;
7109 int r, i;
7110
7111 rdmsrl_safe(MSR_EFER, &host_efer);
7112
7113 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7114 kvm_define_shared_msr(i, vmx_msr_index[i]);
7115
7116 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7117 return -EIO;
7118
7119 if (boot_cpu_has(X86_FEATURE_NX))
7120 kvm_enable_efer_bits(EFER_NX);
7121
7122 if (boot_cpu_has(X86_FEATURE_MPX)) {
7123 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7124 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7125 }
7126
7127 if (boot_cpu_has(X86_FEATURE_XSAVES))
7128 rdmsrl(MSR_IA32_XSS, host_xss);
7129
7130 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7131 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7132 enable_vpid = 0;
7133
7134 if (!cpu_has_vmx_ept() ||
7135 !cpu_has_vmx_ept_4levels() ||
7136 !cpu_has_vmx_ept_mt_wb() ||
7137 !cpu_has_vmx_invept_global())
7138 enable_ept = 0;
7139
7140 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7141 enable_ept_ad_bits = 0;
7142
7143 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7144 enable_unrestricted_guest = 0;
7145
7146 if (!cpu_has_vmx_flexpriority())
7147 flexpriority_enabled = 0;
7148
7149 if (!cpu_has_virtual_nmis())
7150 enable_vnmi = 0;
7151
7152 /*
7153 * set_apic_access_page_addr() is used to reload apic access
7154 * page upon invalidation. No need to do anything if not
7155 * using the APIC_ACCESS_ADDR VMCS field.
7156 */
7157 if (!flexpriority_enabled)
7158 kvm_x86_ops->set_apic_access_page_addr = NULL;
7159
7160 if (!cpu_has_vmx_tpr_shadow())
7161 kvm_x86_ops->update_cr8_intercept = NULL;
7162
7163 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7164 kvm_disable_largepages();
7165
7166#if IS_ENABLED(CONFIG_HYPERV)
7167 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7168 && enable_ept)
7169 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7170#endif
7171
7172 if (!cpu_has_vmx_ple()) {
7173 ple_gap = 0;
7174 ple_window = 0;
7175 ple_window_grow = 0;
7176 ple_window_max = 0;
7177 ple_window_shrink = 0;
7178 }
7179
7180 if (!cpu_has_vmx_apicv()) {
7181 enable_apicv = 0;
7182 kvm_x86_ops->sync_pir_to_irr = NULL;
7183 }
7184
7185 if (cpu_has_vmx_tsc_scaling()) {
7186 kvm_has_tsc_control = true;
7187 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7188 kvm_tsc_scaling_ratio_frac_bits = 48;
7189 }
7190
7191 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7192
7193 if (enable_ept)
7194 vmx_enable_tdp();
7195 else
7196 kvm_disable_tdp();
7197
Sean Christophersona3203382018-12-03 13:53:11 -08007198 /*
7199 * Only enable PML when hardware supports PML feature, and both EPT
7200 * and EPT A/D bit features are enabled -- PML depends on them to work.
7201 */
7202 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7203 enable_pml = 0;
7204
7205 if (!enable_pml) {
7206 kvm_x86_ops->slot_enable_log_dirty = NULL;
7207 kvm_x86_ops->slot_disable_log_dirty = NULL;
7208 kvm_x86_ops->flush_log_dirty = NULL;
7209 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7210 }
7211
7212 if (!cpu_has_vmx_preemption_timer())
7213 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7214
7215 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7216 u64 vmx_msr;
7217
7218 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7219 cpu_preemption_timer_multi =
7220 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7221 } else {
7222 kvm_x86_ops->set_hv_timer = NULL;
7223 kvm_x86_ops->cancel_hv_timer = NULL;
7224 }
7225
Sean Christophersona3203382018-12-03 13:53:11 -08007226 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Sean Christophersona3203382018-12-03 13:53:11 -08007227
7228 kvm_mce_cap_supported |= MCG_LMCE_P;
7229
7230 if (nested) {
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007231 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7232 vmx_capability.ept, enable_apicv);
7233
Sean Christophersone4027cf2018-12-03 13:53:12 -08007234 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Sean Christophersona3203382018-12-03 13:53:11 -08007235 if (r)
7236 return r;
7237 }
7238
7239 r = alloc_kvm_area();
7240 if (r)
7241 nested_vmx_hardware_unsetup();
7242 return r;
7243}
7244
7245static __exit void hardware_unsetup(void)
7246{
7247 if (nested)
7248 nested_vmx_hardware_unsetup();
7249
7250 free_kvm_area();
7251}
7252
Kees Cook404f6aa2016-08-08 16:29:06 -07007253static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007254 .cpu_has_kvm_support = cpu_has_kvm_support,
7255 .disabled_by_bios = vmx_disabled_by_bios,
7256 .hardware_setup = hardware_setup,
7257 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007258 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007259 .hardware_enable = hardware_enable,
7260 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007261 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007262 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007263
Wanpeng Lib31c1142018-03-12 04:53:04 -07007264 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -07007265 .vm_alloc = vmx_vm_alloc,
7266 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -07007267
Avi Kivity6aa8b732006-12-10 02:21:36 -08007268 .vcpu_create = vmx_create_vcpu,
7269 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007270 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007271
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007272 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007273 .vcpu_load = vmx_vcpu_load,
7274 .vcpu_put = vmx_vcpu_put,
7275
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007276 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007277 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007278 .get_msr = vmx_get_msr,
7279 .set_msr = vmx_set_msr,
7280 .get_segment_base = vmx_get_segment_base,
7281 .get_segment = vmx_get_segment,
7282 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007283 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007284 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007285 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007286 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007287 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007288 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 .set_cr3 = vmx_set_cr3,
7290 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007291 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292 .get_idt = vmx_get_idt,
7293 .set_idt = vmx_set_idt,
7294 .get_gdt = vmx_get_gdt,
7295 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007296 .get_dr6 = vmx_get_dr6,
7297 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007298 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007299 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007300 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007301 .get_rflags = vmx_get_rflags,
7302 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007303
Avi Kivity6aa8b732006-12-10 02:21:36 -08007304 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007305 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007306
Avi Kivity6aa8b732006-12-10 02:21:36 -08007307 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007308 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007309 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007310 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7311 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007312 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007313 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007314 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007315 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007316 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007317 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007318 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007319 .get_nmi_mask = vmx_get_nmi_mask,
7320 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007321 .enable_nmi_window = enable_nmi_window,
7322 .enable_irq_window = enable_irq_window,
7323 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007324 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007325 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007326 .get_enable_apicv = vmx_get_enable_apicv,
7327 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007328 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007329 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007330 .hwapic_irr_update = vmx_hwapic_irr_update,
7331 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007332 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007333 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7334 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007335
Izik Eiduscbc94022007-10-25 00:29:55 +02007336 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007337 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007338 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007339 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007340
Avi Kivity586f9602010-11-18 13:09:54 +02007341 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007342
Sheng Yang17cc3932010-01-05 19:02:27 +08007343 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007344
7345 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007346
7347 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007348 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007349
7350 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007351
7352 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007353
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02007354 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +02007355 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007356
7357 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007358
7359 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08007360 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007361 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007362 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007363 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007364
Sean Christophersond264ee02018-08-27 15:21:12 -07007365 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007366
7367 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007368
7369 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7370 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7371 .flush_log_dirty = vmx_flush_log_dirty,
7372 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -04007373 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +02007374
Feng Wubf9f6ac2015-09-18 22:29:55 +08007375 .pre_block = vmx_pre_block,
7376 .post_block = vmx_post_block,
7377
Wei Huang25462f72015-06-19 15:45:05 +02007378 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007379
7380 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007381
7382#ifdef CONFIG_X86_64
7383 .set_hv_timer = vmx_set_hv_timer,
7384 .cancel_hv_timer = vmx_cancel_hv_timer,
7385#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007386
7387 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007388
Ladi Prosek72d7b372017-10-11 16:54:41 +02007389 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007390 .pre_enter_smm = vmx_pre_enter_smm,
7391 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007392 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007393
Sean Christophersone4027cf2018-12-03 13:53:12 -08007394 .check_nested_events = NULL,
7395 .get_nested_state = NULL,
7396 .set_nested_state = NULL,
7397 .get_vmcs12_pages = NULL,
7398 .nested_enable_evmcs = NULL,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007399};
7400
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +02007401static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02007402{
7403 if (vmx_l1d_flush_pages) {
7404 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7405 vmx_l1d_flush_pages = NULL;
7406 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +02007407 /* Restore state so sysfs ignores VMX */
7408 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +02007409}
7410
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007411static void vmx_exit(void)
7412{
7413#ifdef CONFIG_KEXEC_CORE
7414 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7415 synchronize_rcu();
7416#endif
7417
7418 kvm_exit();
7419
7420#if IS_ENABLED(CONFIG_HYPERV)
7421 if (static_branch_unlikely(&enable_evmcs)) {
7422 int cpu;
7423 struct hv_vp_assist_page *vp_ap;
7424 /*
7425 * Reset everything to support using non-enlightened VMCS
7426 * access later (e.g. when we reload the module with
7427 * enlightened_vmcs=0)
7428 */
7429 for_each_online_cpu(cpu) {
7430 vp_ap = hv_get_vp_assist_page(cpu);
7431
7432 if (!vp_ap)
7433 continue;
7434
7435 vp_ap->current_nested_vmcs = 0;
7436 vp_ap->enlighten_vmentry = 0;
7437 }
7438
7439 static_branch_disable(&enable_evmcs);
7440 }
7441#endif
7442 vmx_cleanup_l1d_flush();
7443}
7444module_exit(vmx_exit);
7445
Avi Kivity6aa8b732006-12-10 02:21:36 -08007446static int __init vmx_init(void)
7447{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01007448 int r;
7449
7450#if IS_ENABLED(CONFIG_HYPERV)
7451 /*
7452 * Enlightened VMCS usage should be recommended and the host needs
7453 * to support eVMCS v1 or above. We can also disable eVMCS support
7454 * with module parameter.
7455 */
7456 if (enlightened_vmcs &&
7457 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
7458 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
7459 KVM_EVMCS_VERSION) {
7460 int cpu;
7461
7462 /* Check that we have assist pages on all online CPUs */
7463 for_each_online_cpu(cpu) {
7464 if (!hv_get_vp_assist_page(cpu)) {
7465 enlightened_vmcs = false;
7466 break;
7467 }
7468 }
7469
7470 if (enlightened_vmcs) {
7471 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
7472 static_branch_enable(&enable_evmcs);
7473 }
7474 } else {
7475 enlightened_vmcs = false;
7476 }
7477#endif
7478
7479 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007480 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007481 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007482 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +08007483
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007484 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +02007485 * Must be called after kvm_init() so enable_ept is properly set
7486 * up. Hand the parameter mitigation value in which was stored in
7487 * the pre module init parser. If no parameter was given, it will
7488 * contain 'auto' which will be turned into the default 'cond'
7489 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007490 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +02007491 if (boot_cpu_has(X86_BUG_L1TF)) {
7492 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
7493 if (r) {
7494 vmx_exit();
7495 return r;
7496 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02007497 }
7498
Dave Young2965faa2015-09-09 15:38:55 -07007499#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007500 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7501 crash_vmclear_local_loaded_vmcss);
7502#endif
Jim Mattson21ebf532018-05-01 15:40:28 -07007503 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007504
He, Qingfdef3ad2007-04-30 09:45:24 +03007505 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007506}
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007507module_init(vmx_init);