blob: 338977e6f5529c211411dd01145ce327e6185f97 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Sean Christopherson199b1182018-12-03 13:52:53 -080019#include <linux/frame.h>
20#include <linux/highmem.h>
21#include <linux/hrtimer.h>
22#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020025#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070026#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080027#include <linux/mm.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080028#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040030#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080031#include <linux/trace_events.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
Sean Christopherson199b1182018-12-03 13:52:53 -080033#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020034#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080035#include <asm/cpu.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010036#include <asm/debugreg.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080037#include <asm/desc.h>
38#include <asm/fpu/internal.h>
39#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080040#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080041#include <asm/kexec.h>
42#include <asm/perf_event.h>
43#include <asm/mce.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070044#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010045#include <asm/mshyperv.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080046#include <asm/spec-ctrl.h>
47#include <asm/virtext.h>
48#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Sean Christopherson3077c192018-12-03 13:53:02 -080050#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080051#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080052#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080053#include "irq.h"
54#include "kvm_cache_regs.h"
55#include "lapic.h"
56#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080057#include "nested.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080058#include "ops.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080060#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080061#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080062#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080063#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080064#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Sean Christopherson2c4fd912018-12-03 13:53:03 -080075bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Sean Christopherson2c4fd912018-12-03 13:53:03 -080081bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Sean Christopherson2c4fd912018-12-03 13:53:03 -080084bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Sean Christopherson2c4fd912018-12-03 13:53:03 -080087bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Sean Christopherson2c4fd912018-12-03 13:53:03 -080091bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080092module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Nadav Har'El801d3422011-05-25 23:02:23 +0300103/*
104 * If nested=1, nested virtualization is supported, i.e., guests may use
105 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
106 * use VMX instructions.
107 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200108static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300109module_param(nested, bool, S_IRUGO);
110
Wanpeng Li20300092014-12-02 19:14:59 +0800111static u64 __read_mostly host_xss;
112
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800113bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800114module_param_named(pml, enable_pml, bool, S_IRUGO);
115
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100116#define MSR_BITMAP_MODE_X2APIC 1
117#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118
Haozhong Zhang64903d62015-10-20 15:39:09 +0800119#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
120
Yunhong Jiang64672c92016-06-13 14:19:59 -0700121/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
122static int __read_mostly cpu_preemption_timer_multi;
123static bool __read_mostly enable_preemption_timer = 1;
124#ifdef CONFIG_X86_64
125module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
126#endif
127
Sean Christopherson3de63472018-07-13 08:42:30 -0700128#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800129#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
130#define KVM_VM_CR0_ALWAYS_ON \
131 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
132 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200133#define KVM_CR4_GUEST_OWNED_BITS \
134 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800135 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200136
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800137#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200138#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
139#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
140
Avi Kivity78ac8b42010-04-08 18:19:35 +0300141#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
142
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800143/*
144 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
145 * ple_gap: upper bound on the amount of time between two successive
146 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500147 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800148 * ple_window: upper bound on the amount of time a guest is allowed to execute
149 * in a PAUSE loop. Tests indicate that most spinlocks are held for
150 * less than 2^12 cycles
151 * Time is measured based on a counter that runs at the same rate as the TSC,
152 * refer SDM volume 3b section 21.6.13 & 22.1.3.
153 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400154static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500155module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200156
Babu Moger7fbc85a2018-03-16 16:37:22 -0400157static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
158module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800159
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200160/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400161static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400162module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200163
164/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400165static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400166module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200167
168/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400169static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
170module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200171
Chao Pengf99e3da2018-10-24 16:05:10 +0800172/* Default is SYSTEM mode, 1 for host-guest mode */
173int __read_mostly pt_mode = PT_MODE_SYSTEM;
174module_param(pt_mode, int, S_IRUGO);
175
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200176static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200177static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200178static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200179
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200180/* Storage for pre module init parameter parsing */
181static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200182
183static const struct {
184 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200185 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200186} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200187 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
188 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
189 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
190 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
191 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
192 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200193};
194
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200195#define L1D_CACHE_ORDER 4
196static void *vmx_l1d_flush_pages;
197
198static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
199{
200 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200201 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200202
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200203 if (!enable_ept) {
204 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
205 return 0;
206 }
207
Yi Wangd806afa2018-08-16 13:42:39 +0800208 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
209 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200210
Yi Wangd806afa2018-08-16 13:42:39 +0800211 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
212 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
213 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
214 return 0;
215 }
216 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200217
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200218 /* If set to auto use the default l1tf mitigation method */
219 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
220 switch (l1tf_mitigation) {
221 case L1TF_MITIGATION_OFF:
222 l1tf = VMENTER_L1D_FLUSH_NEVER;
223 break;
224 case L1TF_MITIGATION_FLUSH_NOWARN:
225 case L1TF_MITIGATION_FLUSH:
226 case L1TF_MITIGATION_FLUSH_NOSMT:
227 l1tf = VMENTER_L1D_FLUSH_COND;
228 break;
229 case L1TF_MITIGATION_FULL:
230 case L1TF_MITIGATION_FULL_FORCE:
231 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
232 break;
233 }
234 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
235 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
236 }
237
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200238 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
239 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
240 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
241 if (!page)
242 return -ENOMEM;
243 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200244
245 /*
246 * Initialize each page with a different pattern in
247 * order to protect against KSM in the nested
248 * virtualization case.
249 */
250 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
251 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
252 PAGE_SIZE);
253 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200254 }
255
256 l1tf_vmx_mitigation = l1tf;
257
Thomas Gleixner895ae472018-07-13 16:23:22 +0200258 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
259 static_branch_enable(&vmx_l1d_should_flush);
260 else
261 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200262
Nicolai Stange427362a2018-07-21 22:25:00 +0200263 if (l1tf == VMENTER_L1D_FLUSH_COND)
264 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200265 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200266 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200267 return 0;
268}
269
270static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200271{
272 unsigned int i;
273
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200274 if (s) {
275 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200276 if (vmentry_l1d_param[i].for_parse &&
277 sysfs_streq(s, vmentry_l1d_param[i].option))
278 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200279 }
280 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200281 return -EINVAL;
282}
283
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200284static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
285{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200286 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200287
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200288 l1tf = vmentry_l1d_flush_parse(s);
289 if (l1tf < 0)
290 return l1tf;
291
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200292 if (!boot_cpu_has(X86_BUG_L1TF))
293 return 0;
294
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200295 /*
296 * Has vmx_init() run already? If not then this is the pre init
297 * parameter parsing. In that case just store the value and let
298 * vmx_init() do the proper setup after enable_ept has been
299 * established.
300 */
301 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
302 vmentry_l1d_flush_param = l1tf;
303 return 0;
304 }
305
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200306 mutex_lock(&vmx_l1d_flush_mutex);
307 ret = vmx_setup_l1d_flush(l1tf);
308 mutex_unlock(&vmx_l1d_flush_mutex);
309 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200310}
311
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200312static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
313{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200314 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
315 return sprintf(s, "???\n");
316
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200317 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200318}
319
320static const struct kernel_param_ops vmentry_l1d_flush_ops = {
321 .set = vmentry_l1d_flush_set,
322 .get = vmentry_l1d_flush_get,
323};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200324module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200325
Gleb Natapovd99e4152012-12-20 16:57:45 +0200326static bool guest_state_valid(struct kvm_vcpu *vcpu);
327static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yi Wang1e4329ee2018-11-08 11:22:21 +0800328static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +0100329 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300330
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800332DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300333/*
334 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
335 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
336 */
337static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338
Feng Wubf9f6ac2015-09-18 22:29:55 +0800339/*
340 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
341 * can find which vCPU should be waken up.
342 */
343static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
344static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
345
Sheng Yang2384d2b2008-01-17 15:14:33 +0800346static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
347static DEFINE_SPINLOCK(vmx_vpid_lock);
348
Sean Christopherson3077c192018-12-03 13:53:02 -0800349struct vmcs_config vmcs_config;
350struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800351
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352#define VMX_SEGMENT_FIELD(seg) \
353 [VCPU_SREG_##seg] = { \
354 .selector = GUEST_##seg##_SELECTOR, \
355 .base = GUEST_##seg##_BASE, \
356 .limit = GUEST_##seg##_LIMIT, \
357 .ar_bytes = GUEST_##seg##_AR_BYTES, \
358 }
359
Mathias Krause772e0312012-08-30 01:30:19 +0200360static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 unsigned selector;
362 unsigned base;
363 unsigned limit;
364 unsigned ar_bytes;
365} kvm_vmx_segment_fields[] = {
366 VMX_SEGMENT_FIELD(CS),
367 VMX_SEGMENT_FIELD(DS),
368 VMX_SEGMENT_FIELD(ES),
369 VMX_SEGMENT_FIELD(FS),
370 VMX_SEGMENT_FIELD(GS),
371 VMX_SEGMENT_FIELD(SS),
372 VMX_SEGMENT_FIELD(TR),
373 VMX_SEGMENT_FIELD(LDTR),
374};
375
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800376u64 host_efer;
Avi Kivity26bb0982009-09-07 11:14:12 +0300377
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300378/*
Jim Mattson898a8112018-12-05 15:28:59 -0800379 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
380 * will emulate SYSCALL in legacy mode if the vendor string in guest
381 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
382 * support this emulation, IA32_STAR must always be included in
383 * vmx_msr_index[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300384 */
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800385const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800386#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300387 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400389 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100392#if IS_ENABLED(CONFIG_HYPERV)
393static bool __read_mostly enlightened_vmcs = true;
394module_param(enlightened_vmcs, bool, 0444);
395
Tianyu Lan877ad952018-07-19 08:40:23 +0000396/* check_ept_pointer() should be under protection of ept_pointer_lock. */
397static void check_ept_pointer_match(struct kvm *kvm)
398{
399 struct kvm_vcpu *vcpu;
400 u64 tmp_eptp = INVALID_PAGE;
401 int i;
402
403 kvm_for_each_vcpu(i, vcpu, kvm) {
404 if (!VALID_PAGE(tmp_eptp)) {
405 tmp_eptp = to_vmx(vcpu)->ept_pointer;
406 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
407 to_kvm_vmx(kvm)->ept_pointers_match
408 = EPT_POINTERS_MISMATCH;
409 return;
410 }
411 }
412
413 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
414}
415
416static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
417{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800418 struct kvm_vcpu *vcpu;
419 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000420
421 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
422
423 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
424 check_ept_pointer_match(kvm);
425
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200426 /*
427 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
428 * base of EPT PML4 table, strip off EPT configuration information.
Lan Tianyu53963a72018-12-06 15:34:36 +0800429 * If ept_pointer is invalid pointer, bypass the flush request.
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +0200430 */
Tianyu Lan877ad952018-07-19 08:40:23 +0000431 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyu53963a72018-12-06 15:34:36 +0800432 kvm_for_each_vcpu(i, vcpu, kvm) {
433 u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
434
435 if (!VALID_PAGE(ept_pointer))
436 continue;
437
Lan Tianyua5c214d2018-10-13 22:54:05 +0800438 ret |= hyperv_flush_guest_mapping(
Lan Tianyu53963a72018-12-06 15:34:36 +0800439 ept_pointer & PAGE_MASK);
440 }
Lan Tianyua5c214d2018-10-13 22:54:05 +0800441 } else {
442 ret = hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -0700443 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
Tianyu Lan877ad952018-07-19 08:40:23 +0000444 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000445
Tianyu Lan877ad952018-07-19 08:40:23 +0000446 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
447 return ret;
448}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100449#endif /* IS_ENABLED(CONFIG_HYPERV) */
450
Yunhong Jiang64672c92016-06-13 14:19:59 -0700451/*
452 * Comment's format: document - errata name - stepping - processor name.
453 * Refer from
454 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
455 */
456static u32 vmx_preemption_cpu_tfms[] = {
457/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
4580x000206E6,
459/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
460/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
461/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
4620x00020652,
463/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
4640x00020655,
465/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
466/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
467/*
468 * 320767.pdf - AAP86 - B1 -
469 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
470 */
4710x000106E5,
472/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
4730x000106A0,
474/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
4750x000106A1,
476/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
4770x000106A4,
478 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
479 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
480 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
4810x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600482 /* Xeon E3-1220 V2 */
4830x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700484};
485
486static inline bool cpu_has_broken_vmx_preemption_timer(void)
487{
488 u32 eax = cpuid_eax(0x00000001), i;
489
490 /* Clear the reserved bits */
491 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000492 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700493 if (eax == vmx_preemption_cpu_tfms[i])
494 return true;
495
496 return false;
497}
498
Paolo Bonzini35754c92015-07-29 12:05:37 +0200499static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800500{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200501 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800502}
503
Sheng Yang04547152009-04-01 15:52:31 +0800504static inline bool report_flexpriority(void)
505{
506 return flexpriority_enabled;
507}
508
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800509static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800510{
511 int i;
512
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400513 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300514 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300515 return i;
516 return -1;
517}
518
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800519struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300520{
521 int i;
522
Rusty Russell8b9cf982007-07-30 16:31:43 +1000523 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300524 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400525 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000526 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800527}
528
Sean Christopherson7c97fcb2018-12-03 13:53:17 -0800529void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
530{
531 vmcs_clear(loaded_vmcs->vmcs);
532 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
533 vmcs_clear(loaded_vmcs->shadow_vmcs);
534 loaded_vmcs->cpu = -1;
535 loaded_vmcs->launched = 0;
536}
537
Dave Young2965faa2015-09-09 15:38:55 -0700538#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800539/*
540 * This bitmap is used to indicate whether the vmclear
541 * operation is enabled on all cpus. All disabled by
542 * default.
543 */
544static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
545
546static inline void crash_enable_local_vmclear(int cpu)
547{
548 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
549}
550
551static inline void crash_disable_local_vmclear(int cpu)
552{
553 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
554}
555
556static inline int crash_local_vmclear_enabled(int cpu)
557{
558 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
559}
560
561static void crash_vmclear_local_loaded_vmcss(void)
562{
563 int cpu = raw_smp_processor_id();
564 struct loaded_vmcs *v;
565
566 if (!crash_local_vmclear_enabled(cpu))
567 return;
568
569 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
570 loaded_vmcss_on_cpu_link)
571 vmcs_clear(v->vmcs);
572}
573#else
574static inline void crash_enable_local_vmclear(int cpu) { }
575static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -0700576#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800577
Nadav Har'Eld462b812011-05-24 15:26:10 +0300578static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300580 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800581 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582
Nadav Har'Eld462b812011-05-24 15:26:10 +0300583 if (loaded_vmcs->cpu != cpu)
584 return; /* vcpu migration can race with cpu offline */
585 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800587 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300588 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800589
590 /*
591 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
592 * is before setting loaded_vmcs->vcpu to -1 which is done in
593 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
594 * then adds the vmcs into percpu list before it is deleted.
595 */
596 smp_wmb();
597
Nadav Har'Eld462b812011-05-24 15:26:10 +0300598 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800599 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600}
601
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800602void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800603{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800604 int cpu = loaded_vmcs->cpu;
605
606 if (cpu != -1)
607 smp_call_function_single(cpu,
608 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800609}
610
Avi Kivity2fb92db2011-04-27 19:42:18 +0300611static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
612 unsigned field)
613{
614 bool ret;
615 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
616
617 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
618 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
619 vmx->segment_cache.bitmask = 0;
620 }
621 ret = vmx->segment_cache.bitmask & mask;
622 vmx->segment_cache.bitmask |= mask;
623 return ret;
624}
625
626static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
627{
628 u16 *p = &vmx->segment_cache.seg[seg].selector;
629
630 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
631 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
632 return *p;
633}
634
635static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
636{
637 ulong *p = &vmx->segment_cache.seg[seg].base;
638
639 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
640 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
641 return *p;
642}
643
644static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
645{
646 u32 *p = &vmx->segment_cache.seg[seg].limit;
647
648 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
649 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
650 return *p;
651}
652
653static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
654{
655 u32 *p = &vmx->segment_cache.seg[seg].ar;
656
657 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
658 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
659 return *p;
660}
661
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800662void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300663{
664 u32 eb;
665
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100666 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800667 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200668 /*
669 * Guest access to VMware backdoor ports could legitimately
670 * trigger #GP because of TSS I/O permission bitmap.
671 * We intercept those #GP and allow access to them anyway
672 * as VMware does.
673 */
674 if (enable_vmware_backdoor)
675 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100676 if ((vcpu->guest_debug &
677 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
678 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
679 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300680 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300681 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200682 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800683 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300684
685 /* When we are running a nested L2 guest and L1 specified for it a
686 * certain exception bitmap, we must trap the same exceptions and pass
687 * them to L1. When running L2, we will only handle the exceptions
688 * specified above if L1 did not want them.
689 */
690 if (is_guest_mode(vcpu))
691 eb |= get_vmcs12(vcpu)->exception_bitmap;
692
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300693 vmcs_write32(EXCEPTION_BITMAP, eb);
694}
695
Ashok Raj15d45072018-02-01 22:59:43 +0100696/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100697 * Check if MSR is intercepted for currently loaded MSR bitmap.
698 */
699static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
700{
701 unsigned long *msr_bitmap;
702 int f = sizeof(unsigned long);
703
704 if (!cpu_has_vmx_msr_bitmap())
705 return true;
706
707 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
708
709 if (msr <= 0x1fff) {
710 return !!test_bit(msr, msr_bitmap + 0x800 / f);
711 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
712 msr &= 0x1fff;
713 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
714 }
715
716 return true;
717}
718
Gleb Natapov2961e8762013-11-25 15:37:13 +0200719static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
720 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200721{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200722 vm_entry_controls_clearbit(vmx, entry);
723 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200724}
725
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400726static int find_msr(struct vmx_msrs *m, unsigned int msr)
727{
728 unsigned int i;
729
730 for (i = 0; i < m->nr; ++i) {
731 if (m->val[i].index == msr)
732 return i;
733 }
734 return -ENOENT;
735}
736
Avi Kivity61d2ef22010-04-28 16:40:38 +0300737static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
738{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400739 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300740 struct msr_autoload *m = &vmx->msr_autoload;
741
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200742 switch (msr) {
743 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800744 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200745 clear_atomic_switch_msr_special(vmx,
746 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200747 VM_EXIT_LOAD_IA32_EFER);
748 return;
749 }
750 break;
751 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800752 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200753 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200754 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
755 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
756 return;
757 }
758 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200759 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400760 i = find_msr(&m->guest, msr);
761 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400762 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400763 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400764 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400765 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200766
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400767skip_guest:
768 i = find_msr(&m->host, msr);
769 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300770 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400771
772 --m->host.nr;
773 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400774 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300775}
776
Gleb Natapov2961e8762013-11-25 15:37:13 +0200777static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
778 unsigned long entry, unsigned long exit,
779 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
780 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200781{
782 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700783 if (host_val_vmcs != HOST_IA32_EFER)
784 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200785 vm_entry_controls_setbit(vmx, entry);
786 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200787}
788
Avi Kivity61d2ef22010-04-28 16:40:38 +0300789static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400790 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300791{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400792 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300793 struct msr_autoload *m = &vmx->msr_autoload;
794
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200795 switch (msr) {
796 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800797 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200798 add_atomic_switch_msr_special(vmx,
799 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200800 VM_EXIT_LOAD_IA32_EFER,
801 GUEST_IA32_EFER,
802 HOST_IA32_EFER,
803 guest_val, host_val);
804 return;
805 }
806 break;
807 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800808 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200809 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200810 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
811 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
812 GUEST_IA32_PERF_GLOBAL_CTRL,
813 HOST_IA32_PERF_GLOBAL_CTRL,
814 guest_val, host_val);
815 return;
816 }
817 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100818 case MSR_IA32_PEBS_ENABLE:
819 /* PEBS needs a quiescent period after being disabled (to write
820 * a record). Disabling PEBS through VMX MSR swapping doesn't
821 * provide that period, so a CPU could write host's record into
822 * guest's memory.
823 */
824 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200825 }
826
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400827 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400828 if (!entry_only)
829 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300830
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400831 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200832 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200833 "Can't add msr %x\n", msr);
834 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300835 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400836 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400837 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400838 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400839 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400840 m->guest.val[i].index = msr;
841 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300842
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400843 if (entry_only)
844 return;
845
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400846 if (j < 0) {
847 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400848 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300849 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400850 m->host.val[j].index = msr;
851 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300852}
853
Avi Kivity92c0d902009-10-29 11:00:16 +0200854static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300855{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100856 u64 guest_efer = vmx->vcpu.arch.efer;
857 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +0300858
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100859 if (!enable_ept) {
860 /*
861 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
862 * host CPUID is more efficient than testing guest CPUID
863 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
864 */
865 if (boot_cpu_has(X86_FEATURE_SMEP))
866 guest_efer |= EFER_NX;
867 else if (!(guest_efer & EFER_NX))
868 ignore_bits |= EFER_NX;
869 }
Roel Kluin3a34a882009-08-04 02:08:45 -0700870
Avi Kivity51c6cf62007-08-29 03:48:05 +0300871 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100872 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +0300873 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100874 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300875#ifdef CONFIG_X86_64
876 ignore_bits |= EFER_LMA | EFER_LME;
877 /* SCE is meaningful only in long mode on Intel */
878 if (guest_efer & EFER_LMA)
879 ignore_bits &= ~(u64)EFER_SCE;
880#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +0300881
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800882 /*
883 * On EPT, we can't emulate NX, so we must switch EFER atomically.
884 * On CPUs that support "load IA32_EFER", always switch EFER
885 * atomically, since it's faster than switching it manually.
886 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800887 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800888 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +0300889 if (!(guest_efer & EFER_LMA))
890 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -0800891 if (guest_efer != host_efer)
892 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400893 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -0700894 else
895 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +0300896 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100897 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -0700898 clear_atomic_switch_msr(vmx, MSR_EFER);
899
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100900 guest_efer &= ~ignore_bits;
901 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300902
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100903 vmx->guest_msrs[efer_offset].data = guest_efer;
904 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
905
906 return true;
907 }
Avi Kivity51c6cf62007-08-29 03:48:05 +0300908}
909
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800910#ifdef CONFIG_X86_32
911/*
912 * On 32-bit kernels, VM exits still load the FS and GS bases from the
913 * VMCS rather than the segment table. KVM uses this helper to figure
914 * out the current bases to poke them into the VMCS before entry.
915 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200916static unsigned long segment_base(u16 selector)
917{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800918 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200919 unsigned long v;
920
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800921 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200922 return 0;
923
Thomas Garnier45fc8752017-03-14 10:05:08 -0700924 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200925
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800926 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200927 u16 ldt_selector = kvm_read_ldt();
928
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800929 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200930 return 0;
931
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800932 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200933 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -0800934 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200935 return v;
936}
Andy Lutomirskie28baea2017-02-20 08:56:11 -0800937#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200938
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800939void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300940{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -0700942 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +0200943#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +0100944 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +0200945#endif
Sean Christophersone368b872018-07-23 12:32:41 -0700946 unsigned long fs_base, gs_base;
947 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +0300948 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300949
Sean Christophersond264ee02018-08-27 15:21:12 -0700950 vmx->req_immediate_exit = false;
951
Liran Alonf48b4712018-11-20 18:03:25 +0200952 /*
953 * Note that guest MSRs to be saved/restored can also be changed
954 * when guest state is loaded. This happens when guest transitions
955 * to/from long-mode by setting MSR_EFER.LMA.
956 */
957 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
958 vmx->guest_msrs_dirty = false;
959 for (i = 0; i < vmx->save_nmsrs; ++i)
960 kvm_set_shared_msr(vmx->guest_msrs[i].index,
961 vmx->guest_msrs[i].data,
962 vmx->guest_msrs[i].mask);
963
964 }
965
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700966 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +0300967 return;
968
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700969 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700970 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700971
Avi Kivity33ed6322007-05-02 16:54:03 +0300972 /*
973 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
974 * allow segment selectors with cpl > 0 or ti == 1.
975 */
Sean Christophersond7ee0392018-07-23 12:32:47 -0700976 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +0100977
978#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -0700979 savesegment(ds, host_state->ds_sel);
980 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -0700981
982 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200983 if (likely(is_64bit_mm(current->mm))) {
984 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -0700985 fs_sel = current->thread.fsindex;
986 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200987 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -0700988 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200989 } else {
Sean Christophersone368b872018-07-23 12:32:41 -0700990 savesegment(fs, fs_sel);
991 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +0200992 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -0700993 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +0300994 }
995
Paolo Bonzini4679b612018-09-24 17:23:01 +0200996 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +0300997#else
Sean Christophersone368b872018-07-23 12:32:41 -0700998 savesegment(fs, fs_sel);
999 savesegment(gs, gs_sel);
1000 fs_base = segment_base(fs_sel);
1001 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001002#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001003
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07001004 if (unlikely(fs_sel != host_state->fs_sel)) {
1005 if (!(fs_sel & 7))
1006 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1007 else
1008 vmcs_write16(HOST_FS_SELECTOR, 0);
1009 host_state->fs_sel = fs_sel;
1010 }
1011 if (unlikely(gs_sel != host_state->gs_sel)) {
1012 if (!(gs_sel & 7))
1013 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1014 else
1015 vmcs_write16(HOST_GS_SELECTOR, 0);
1016 host_state->gs_sel = gs_sel;
1017 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07001018 if (unlikely(fs_base != host_state->fs_base)) {
1019 vmcs_writel(HOST_FS_BASE, fs_base);
1020 host_state->fs_base = fs_base;
1021 }
1022 if (unlikely(gs_base != host_state->gs_base)) {
1023 vmcs_writel(HOST_GS_BASE, gs_base);
1024 host_state->gs_base = gs_base;
1025 }
Avi Kivity33ed6322007-05-02 16:54:03 +03001026}
1027
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001028static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001029{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001030 struct vmcs_host_state *host_state;
1031
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001032 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03001033 return;
1034
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001035 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001036 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001037
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001038 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001039 vmx->loaded_cpu_state = NULL;
1040
Avi Kivityc8770e72010-11-11 12:37:26 +02001041#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001042 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001043#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001044 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1045 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001046#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001047 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001048#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001049 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001050#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001051 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001052 if (host_state->fs_sel & 7)
1053 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001054#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001055 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1056 loadsegment(ds, host_state->ds_sel);
1057 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001058 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001059#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001060 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001061#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001062 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001063#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001064 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03001065}
1066
Sean Christopherson678e3152018-07-23 12:32:43 -07001067#ifdef CONFIG_X86_64
1068static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001069{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001070 preempt_disable();
1071 if (vmx->loaded_cpu_state)
1072 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1073 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001074 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001075}
1076
Sean Christopherson678e3152018-07-23 12:32:43 -07001077static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1078{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001079 preempt_disable();
1080 if (vmx->loaded_cpu_state)
1081 wrmsrl(MSR_KERNEL_GS_BASE, data);
1082 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001083 vmx->msr_guest_kernel_gs_base = data;
1084}
1085#endif
1086
Feng Wu28b835d2015-09-18 22:29:54 +08001087static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1088{
1089 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1090 struct pi_desc old, new;
1091 unsigned int dest;
1092
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001093 /*
1094 * In case of hot-plug or hot-unplug, we may have to undo
1095 * vmx_vcpu_pi_put even if there is no assigned device. And we
1096 * always keep PI.NDST up to date for simplicity: it makes the
1097 * code easier, and CPU migration is not a fast path.
1098 */
1099 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08001100 return;
1101
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001102 /*
1103 * First handle the simple case where no cmpxchg is necessary; just
1104 * allow posting non-urgent interrupts.
1105 *
1106 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
1107 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
1108 * expects the VCPU to be on the blocked_vcpu_list that matches
1109 * PI.NDST.
1110 */
1111 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
1112 vcpu->cpu == cpu) {
1113 pi_clear_sn(pi_desc);
1114 return;
1115 }
1116
1117 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08001118 do {
1119 old.control = new.control = pi_desc->control;
1120
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001121 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08001122
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001123 if (x2apic_enabled())
1124 new.ndst = dest;
1125 else
1126 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08001127
Feng Wu28b835d2015-09-18 22:29:54 +08001128 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02001129 } while (cmpxchg64(&pi_desc->control, old.control,
1130 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08001131}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001132
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133/*
1134 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1135 * vcpu mutex is already taken.
1136 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001137void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001139 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001140 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001142 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001143 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001144 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001145 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001146
1147 /*
1148 * Read loaded_vmcs->cpu should be before fetching
1149 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1150 * See the comments in __loaded_vmcs_clear().
1151 */
1152 smp_rmb();
1153
Nadav Har'Eld462b812011-05-24 15:26:10 +03001154 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1155 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001156 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001157 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001158 }
1159
1160 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1161 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1162 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01001163 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001164 }
1165
1166 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001167 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001168 unsigned long sysenter_esp;
1169
1170 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001171
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 /*
1173 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001174 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001176 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001177 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001178 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001180 /*
1181 * VM exits change the host TR limit to 0x67 after a VM
1182 * exit. This is okay, since 0x67 covers everything except
1183 * the IO bitmap and have have code to handle the IO bitmap
1184 * being lost after a VM exit.
1185 */
1186 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
1187
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1189 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001190
Nadav Har'Eld462b812011-05-24 15:26:10 +03001191 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192 }
Feng Wu28b835d2015-09-18 22:29:54 +08001193
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001194 /* Setup TSC multiplier */
1195 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001196 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1197 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001198
Feng Wu28b835d2015-09-18 22:29:54 +08001199 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001200 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08001201 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001202}
1203
1204static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
1205{
1206 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1207
1208 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08001209 !irq_remapping_cap(IRQ_POSTING_CAP) ||
1210 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08001211 return;
1212
1213 /* Set SN when the vCPU is preempted */
1214 if (vcpu->preempted)
1215 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216}
1217
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001218void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219{
Feng Wu28b835d2015-09-18 22:29:54 +08001220 vmx_vcpu_pi_put(vcpu);
1221
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001222 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223}
1224
Wanpeng Lif244dee2017-07-20 01:11:54 -07001225static bool emulation_required(struct kvm_vcpu *vcpu)
1226{
1227 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
1228}
1229
Avi Kivityedcafe32009-12-30 18:07:40 +02001230static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1231
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001232unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001234 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001235
Avi Kivity6de12732011-03-07 12:51:22 +02001236 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1237 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1238 rflags = vmcs_readl(GUEST_RFLAGS);
1239 if (to_vmx(vcpu)->rmode.vm86_active) {
1240 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1241 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1242 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1243 }
1244 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001245 }
Avi Kivity6de12732011-03-07 12:51:22 +02001246 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247}
1248
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001249void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250{
Wanpeng Lif244dee2017-07-20 01:11:54 -07001251 unsigned long old_rflags = vmx_get_rflags(vcpu);
1252
Avi Kivity6de12732011-03-07 12:51:22 +02001253 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1254 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001255 if (to_vmx(vcpu)->rmode.vm86_active) {
1256 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001257 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001258 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001260
1261 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
1262 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263}
1264
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001265u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001266{
1267 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1268 int ret = 0;
1269
1270 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001271 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001272 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001273 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001274
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001275 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001276}
1277
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001278void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001279{
1280 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1281 u32 interruptibility = interruptibility_old;
1282
1283 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1284
Jan Kiszka48005f62010-02-19 19:38:07 +01001285 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001286 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001287 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001288 interruptibility |= GUEST_INTR_STATE_STI;
1289
1290 if ((interruptibility != interruptibility_old))
1291 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1292}
1293
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1295{
1296 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001298 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001300 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301
Glauber Costa2809f5d2009-05-12 16:21:05 -04001302 /* skipping an emulated instruction also counts */
1303 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304}
1305
Wanpeng Licaa057a2018-03-12 04:53:03 -07001306static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1307{
1308 /*
1309 * Ensure that we clear the HLT state in the VMCS. We don't need to
1310 * explicitly skip the instruction because if the HLT state is set,
1311 * then the instruction is already executing and RIP has already been
1312 * advanced.
1313 */
1314 if (kvm_hlt_in_guest(vcpu->kvm) &&
1315 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1316 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1317}
1318
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001319static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001320{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001321 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001322 unsigned nr = vcpu->arch.exception.nr;
1323 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001324 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001325 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001326
Jim Mattsonda998b42018-10-16 14:29:22 -07001327 kvm_deliver_exception_payload(vcpu);
1328
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001329 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001330 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001331 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1332 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001333
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001334 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001335 int inc_eip = 0;
1336 if (kvm_exception_is_soft(nr))
1337 inc_eip = vcpu->arch.event_exit_inst_len;
1338 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001339 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001340 return;
1341 }
1342
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001343 WARN_ON_ONCE(vmx->emulation_required);
1344
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001345 if (kvm_exception_is_soft(nr)) {
1346 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1347 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001348 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1349 } else
1350 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1351
1352 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001353
1354 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001355}
1356
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001357static bool vmx_rdtscp_supported(void)
1358{
1359 return cpu_has_vmx_rdtscp();
1360}
1361
Mao, Junjiead756a12012-07-02 01:18:48 +00001362static bool vmx_invpcid_supported(void)
1363{
Junaid Shahideb4b2482018-06-27 14:59:14 -07001364 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00001365}
1366
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367/*
Eddie Donga75beee2007-05-17 18:55:15 +03001368 * Swap MSR entry in host/guest MSR entry array.
1369 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001370static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001371{
Avi Kivity26bb0982009-09-07 11:14:12 +03001372 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001373
1374 tmp = vmx->guest_msrs[to];
1375 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1376 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001377}
1378
1379/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001380 * Set up the vmcs to automatically save and restore system
1381 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1382 * mode, as fiddling with msrs is very expensive.
1383 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001384static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001385{
Avi Kivity26bb0982009-09-07 11:14:12 +03001386 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001387
Eddie Donga75beee2007-05-17 18:55:15 +03001388 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001389#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001390 /*
1391 * The SYSCALL MSRs are only needed on long mode guests, and only
1392 * when EFER.SCE is set.
1393 */
1394 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
1395 index = __find_msr_index(vmx, MSR_STAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001396 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001397 move_msr_up(vmx, index, save_nmsrs++);
1398 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001399 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001400 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001401 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
1402 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001403 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001404 }
Eddie Donga75beee2007-05-17 18:55:15 +03001405#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001406 index = __find_msr_index(vmx, MSR_EFER);
1407 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001408 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson0023ef32018-12-05 15:28:58 -08001409 index = __find_msr_index(vmx, MSR_TSC_AUX);
1410 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
1411 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001412
Avi Kivity26bb0982009-09-07 11:14:12 +03001413 vmx->save_nmsrs = save_nmsrs;
Liran Alonf48b4712018-11-20 18:03:25 +02001414 vmx->guest_msrs_dirty = true;
Avi Kivity58972972009-02-24 22:26:47 +02001415
Yang Zhang8d146952013-01-25 10:18:50 +08001416 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001417 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001418}
1419
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001420static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001422 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001424 if (is_guest_mode(vcpu) &&
1425 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1426 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
1427
1428 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429}
1430
Leonid Shatz326e7422018-11-06 12:14:25 +02001431static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001433 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1434 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001435
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001436 /*
1437 * We're here if L1 chose not to trap WRMSR to TSC. According
1438 * to the spec, this should set L1's TSC; The offset that L1
1439 * set for L2 remains unchanged, and still needs to be added
1440 * to the newly set TSC to get L2's TSC.
1441 */
1442 if (is_guest_mode(vcpu) &&
1443 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1444 g_tsc_offset = vmcs12->tsc_offset;
1445
1446 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1447 vcpu->arch.tsc_offset - g_tsc_offset,
1448 offset);
1449 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1450 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451}
1452
Nadav Har'El801d3422011-05-25 23:02:23 +03001453/*
1454 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1455 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1456 * all guests if the "nested" module option is off, and can also be disabled
1457 * for a single guest by disabling its VMX cpuid bit.
1458 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001459bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001460{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001461 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001462}
1463
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001464static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1465 uint64_t val)
1466{
1467 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1468
1469 return !(val & ~valid_bits);
1470}
1471
Tom Lendacky801e4592018-02-21 13:39:51 -06001472static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1473{
Paolo Bonzini13893092018-02-26 13:40:09 +01001474 switch (msr->index) {
1475 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1476 if (!nested)
1477 return 1;
1478 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
1479 default:
1480 return 1;
1481 }
1482
1483 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06001484}
1485
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001486/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 * Reads an msr value (of 'msr_index') into 'pdata'.
1488 * Returns 0 on success, non-0 otherwise.
1489 * Assumes vcpu_load() was already called.
1490 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001491static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001493 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001494 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001496 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001497#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001499 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500 break;
1501 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001502 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001504 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001505 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001506 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001507#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001509 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001510 case MSR_IA32_SPEC_CTRL:
1511 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001512 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1513 return 1;
1514
1515 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1516 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01001517 case MSR_IA32_ARCH_CAPABILITIES:
1518 if (!msr_info->host_initiated &&
1519 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
1520 return 1;
1521 msr_info->data = to_vmx(vcpu)->arch_capabilities;
1522 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001524 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525 break;
1526 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001527 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 break;
1529 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001530 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001532 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001533 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001534 (!msr_info->host_initiated &&
1535 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001536 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001537 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001538 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001539 case MSR_IA32_MCG_EXT_CTL:
1540 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001541 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08001542 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01001543 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001544 msr_info->data = vcpu->arch.mcg_ext_ctl;
1545 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001546 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001547 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001548 break;
1549 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1550 if (!nested_vmx_allowed(vcpu))
1551 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001552 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1553 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08001554 case MSR_IA32_XSS:
1555 if (!vmx_xsaves_supported())
1556 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001557 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08001558 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001559 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001560 if (!msr_info->host_initiated &&
1561 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001562 return 1;
1563 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001565 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001566 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001567 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001568 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001570 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 }
1572
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573 return 0;
1574}
1575
1576/*
1577 * Writes msr value into into the appropriate "register".
1578 * Returns 0 on success, non-0 otherwise.
1579 * Assumes vcpu_load() was already called.
1580 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001581static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001583 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001584 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001585 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08001586 u32 msr_index = msr_info->index;
1587 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03001588
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001590 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08001591 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03001592 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001593#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001595 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 vmcs_writel(GUEST_FS_BASE, data);
1597 break;
1598 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001599 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 vmcs_writel(GUEST_GS_BASE, data);
1601 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001602 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001603 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001604 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605#endif
1606 case MSR_IA32_SYSENTER_CS:
1607 vmcs_write32(GUEST_SYSENTER_CS, data);
1608 break;
1609 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001610 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 break;
1612 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001613 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001615 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001616 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001617 (!msr_info->host_initiated &&
1618 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001619 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08001620 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07001621 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001623 vmcs_write64(GUEST_BNDCFGS, data);
1624 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001625 case MSR_IA32_SPEC_CTRL:
1626 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001627 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1628 return 1;
1629
1630 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02001631 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001632 return 1;
1633
1634 vmx->spec_ctrl = data;
1635
1636 if (!data)
1637 break;
1638
1639 /*
1640 * For non-nested:
1641 * When it's written (to non-zero) for the first time, pass
1642 * it through.
1643 *
1644 * For nested:
1645 * The handling of the MSR bitmap for L2 guests is done in
1646 * nested_vmx_merge_msr_bitmap. We should not touch the
1647 * vmcs02.msr_bitmap here since it gets completely overwritten
1648 * in the merging. We update the vmcs01 here for L1 as well
1649 * since it will end up touching the MSR anyway now.
1650 */
1651 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
1652 MSR_IA32_SPEC_CTRL,
1653 MSR_TYPE_RW);
1654 break;
Ashok Raj15d45072018-02-01 22:59:43 +01001655 case MSR_IA32_PRED_CMD:
1656 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01001657 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1658 return 1;
1659
1660 if (data & ~PRED_CMD_IBPB)
1661 return 1;
1662
1663 if (!data)
1664 break;
1665
1666 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
1667
1668 /*
1669 * For non-nested:
1670 * When it's written (to non-zero) for the first time, pass
1671 * it through.
1672 *
1673 * For nested:
1674 * The handling of the MSR bitmap for L2 guests is done in
1675 * nested_vmx_merge_msr_bitmap. We should not touch the
1676 * vmcs02.msr_bitmap here since it gets completely overwritten
1677 * in the merging.
1678 */
1679 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
1680 MSR_TYPE_W);
1681 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01001682 case MSR_IA32_ARCH_CAPABILITIES:
1683 if (!msr_info->host_initiated)
1684 return 1;
1685 vmx->arch_capabilities = data;
1686 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001687 case MSR_IA32_CR_PAT:
1688 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03001689 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
1690 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08001691 vmcs_write64(GUEST_IA32_PAT, data);
1692 vcpu->arch.pat = data;
1693 break;
1694 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001695 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001696 break;
Will Auldba904632012-11-29 12:42:50 -08001697 case MSR_IA32_TSC_ADJUST:
1698 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001699 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001700 case MSR_IA32_MCG_EXT_CTL:
1701 if ((!msr_info->host_initiated &&
1702 !(to_vmx(vcpu)->msr_ia32_feature_control &
1703 FEATURE_CONTROL_LMCE)) ||
1704 (data & ~MCG_EXT_CTL_LMCE_EN))
1705 return 1;
1706 vcpu->arch.mcg_ext_ctl = data;
1707 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01001708 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001709 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08001710 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01001711 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
1712 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001713 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01001714 if (msr_info->host_initiated && data == 0)
1715 vmx_leave_nested(vcpu);
1716 break;
1717 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08001718 if (!msr_info->host_initiated)
1719 return 1; /* they are read-only */
1720 if (!nested_vmx_allowed(vcpu))
1721 return 1;
1722 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08001723 case MSR_IA32_XSS:
1724 if (!vmx_xsaves_supported())
1725 return 1;
1726 /*
1727 * The only supported bit as of Skylake is bit 8, but
1728 * it is not supported on KVM.
1729 */
1730 if (data != 0)
1731 return 1;
1732 vcpu->arch.ia32_xss = data;
1733 if (vcpu->arch.ia32_xss != host_xss)
1734 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04001735 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08001736 else
1737 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
1738 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001739 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001740 if (!msr_info->host_initiated &&
1741 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001742 return 1;
1743 /* Check reserved bit, higher 32 bits should be zero */
1744 if ((data >> 32) != 0)
1745 return 1;
1746 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001748 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001749 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07001750 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001751 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03001752 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
1753 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07001754 ret = kvm_set_shared_msr(msr->index, msr->data,
1755 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03001756 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07001757 if (ret)
1758 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03001759 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001760 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761 }
Will Auld8fe8ab42012-11-29 12:42:12 -08001762 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763 }
1764
Eddie Dong2cc51562007-05-21 07:28:09 +03001765 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766}
1767
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001768static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001770 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1771 switch (reg) {
1772 case VCPU_REGS_RSP:
1773 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1774 break;
1775 case VCPU_REGS_RIP:
1776 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1777 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001778 case VCPU_EXREG_PDPTR:
1779 if (enable_ept)
1780 ept_save_pdptrs(vcpu);
1781 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001782 default:
1783 break;
1784 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785}
1786
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787static __init int cpu_has_kvm_support(void)
1788{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001789 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790}
1791
1792static __init int vmx_disabled_by_bios(void)
1793{
1794 u64 msr;
1795
1796 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001797 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001798 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001799 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1800 && tboot_enabled())
1801 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001802 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001803 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001804 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001805 && !tboot_enabled()) {
1806 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001807 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001808 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001809 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001810 /* launched w/o TXT and VMX disabled */
1811 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1812 && !tboot_enabled())
1813 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001814 }
1815
1816 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817}
1818
Dongxiao Xu7725b892010-05-11 18:29:38 +08001819static void kvm_cpu_vmxon(u64 addr)
1820{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001821 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001822 intel_pt_handle_vmx(1);
1823
Uros Bizjak4b1e5472018-10-11 19:40:44 +02001824 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08001825}
1826
Radim Krčmář13a34e02014-08-28 15:13:03 +02001827static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828{
1829 int cpu = raw_smp_processor_id();
1830 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001831 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001833 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02001834 return -EBUSY;
1835
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001836 /*
1837 * This can happen if we hot-added a CPU but failed to allocate
1838 * VP assist page for it.
1839 */
1840 if (static_branch_unlikely(&enable_evmcs) &&
1841 !hv_get_vp_assist_page(cpu))
1842 return -EFAULT;
1843
Nadav Har'Eld462b812011-05-24 15:26:10 +03001844 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08001845 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
1846 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001847
1848 /*
1849 * Now we can enable the vmclear operation in kdump
1850 * since the loaded_vmcss_on_cpu list on this cpu
1851 * has been initialized.
1852 *
1853 * Though the cpu is not in VMX operation now, there
1854 * is no problem to enable the vmclear operation
1855 * for the loaded_vmcss_on_cpu list is empty!
1856 */
1857 crash_enable_local_vmclear(cpu);
1858
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001860
1861 test_bits = FEATURE_CONTROL_LOCKED;
1862 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1863 if (tboot_enabled())
1864 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1865
1866 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001868 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1869 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001870 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02001871 if (enable_ept)
1872 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02001873
1874 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875}
1876
Nadav Har'Eld462b812011-05-24 15:26:10 +03001877static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03001878{
1879 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001880 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03001881
Nadav Har'Eld462b812011-05-24 15:26:10 +03001882 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
1883 loaded_vmcss_on_cpu_link)
1884 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03001885}
1886
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001887
1888/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1889 * tricks.
1890 */
1891static void kvm_cpu_vmxoff(void)
1892{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02001893 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001894
1895 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001896 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001897}
1898
Radim Krčmář13a34e02014-08-28 15:13:03 +02001899static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001901 vmclear_local_loaded_vmcss();
1902 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903}
1904
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001905static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001906 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907{
1908 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001909 u32 ctl = ctl_min | ctl_opt;
1910
1911 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1912
1913 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1914 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1915
1916 /* Ensure minimum (required) set of control bits are supported. */
1917 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001918 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001919
1920 *result = ctl;
1921 return 0;
1922}
1923
Sean Christopherson7caaa712018-12-03 13:53:01 -08001924static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
1925 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001926{
1927 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001928 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001929 u32 _pin_based_exec_control = 0;
1930 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001931 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001932 u32 _vmexit_control = 0;
1933 u32 _vmentry_control = 0;
1934
Paolo Bonzini13893092018-02-26 13:40:09 +01001935 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05301936 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001937#ifdef CONFIG_X86_64
1938 CPU_BASED_CR8_LOAD_EXITING |
1939 CPU_BASED_CR8_STORE_EXITING |
1940#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001941 CPU_BASED_CR3_LOAD_EXITING |
1942 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08001943 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001944 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001945 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001946 CPU_BASED_MWAIT_EXITING |
1947 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02001948 CPU_BASED_INVLPG_EXITING |
1949 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001950
Sheng Yangf78e0e22007-10-29 09:40:42 +08001951 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001952 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001953 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001954 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1955 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001956 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001957#ifdef CONFIG_X86_64
1958 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1959 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1960 ~CPU_BASED_CR8_STORE_EXITING;
1961#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001962 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001963 min2 = 0;
1964 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08001965 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001966 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001967 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001968 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001969 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001970 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02001971 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00001972 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08001973 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08001974 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03001975 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08001976 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08001977 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02001978 SECONDARY_EXEC_RDSEED_EXITING |
1979 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08001980 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04001981 SECONDARY_EXEC_TSC_SCALING |
Chao Pengf99e3da2018-10-24 16:05:10 +08001982 SECONDARY_EXEC_PT_USE_GPA |
1983 SECONDARY_EXEC_PT_CONCEAL_VMX |
Sean Christopherson0b665d32018-08-14 09:33:34 -07001984 SECONDARY_EXEC_ENABLE_VMFUNC |
1985 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001986 if (adjust_vmx_controls(min2, opt2,
1987 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001988 &_cpu_based_2nd_exec_control) < 0)
1989 return -EIO;
1990 }
1991#ifndef CONFIG_X86_64
1992 if (!(_cpu_based_2nd_exec_control &
1993 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1994 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1995#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08001996
1997 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1998 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08001999 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002000 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2001 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002002
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002003 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002004 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002005
Sheng Yangd56f5462008-04-25 10:13:16 +08002006 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002007 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2008 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002009 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2010 CPU_BASED_CR3_STORE_EXITING |
2011 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002012 } else if (vmx_cap->ept) {
2013 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002014 pr_warn_once("EPT CAP should not exist if not support "
2015 "1-setting enable EPT VM-execution control\n");
2016 }
2017 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002018 vmx_cap->vpid) {
2019 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002020 pr_warn_once("VPID CAP should not exist if not support "
2021 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002022 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002023
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002024 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002025#ifdef CONFIG_X86_64
2026 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2027#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002028 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
2029 VM_EXIT_SAVE_IA32_PAT |
2030 VM_EXIT_LOAD_IA32_PAT |
2031 VM_EXIT_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002032 VM_EXIT_CLEAR_BNDCFGS |
2033 VM_EXIT_PT_CONCEAL_PIP |
2034 VM_EXIT_CLEAR_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002035 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2036 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002037 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002038
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002039 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2040 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2041 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002042 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2043 &_pin_based_exec_control) < 0)
2044 return -EIO;
2045
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002046 if (cpu_has_broken_vmx_preemption_timer())
2047 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002048 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002049 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002050 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2051
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002052 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002053 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2054 VM_ENTRY_LOAD_IA32_PAT |
2055 VM_ENTRY_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002056 VM_ENTRY_LOAD_BNDCFGS |
2057 VM_ENTRY_PT_CONCEAL_PIP |
2058 VM_ENTRY_LOAD_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002059 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2060 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002061 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002063 /*
2064 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2065 * can't be used due to an errata where VM Exit may incorrectly clear
2066 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2067 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2068 */
2069 if (boot_cpu_data.x86 == 0x6) {
2070 switch (boot_cpu_data.x86_model) {
2071 case 26: /* AAK155 */
2072 case 30: /* AAP115 */
2073 case 37: /* AAT100 */
2074 case 44: /* BC86,AAY89,BD102 */
2075 case 46: /* BA97 */
2076 _vmexit_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
2077 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2078 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2079 "does not work properly. Using workaround\n");
2080 break;
2081 default:
2082 break;
2083 }
2084 }
2085
2086
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002087 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002088
2089 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2090 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002091 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002092
2093#ifdef CONFIG_X86_64
2094 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2095 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002096 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002097#endif
2098
2099 /* Require Write-Back (WB) memory type for VMCS accesses. */
2100 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002101 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002102
Yang, Sheng002c7f72007-07-31 14:23:01 +03002103 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002104 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002105 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002106
Liran Alon2307af12018-06-29 22:59:04 +03002107 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002108
Yang, Sheng002c7f72007-07-31 14:23:01 +03002109 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2110 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002111 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002112 vmcs_conf->vmexit_ctrl = _vmexit_control;
2113 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002114
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002115 if (static_branch_unlikely(&enable_evmcs))
2116 evmcs_sanitize_exec_ctrls(vmcs_conf);
2117
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002118 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002119}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002121struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122{
2123 int node = cpu_to_node(cpu);
2124 struct page *pages;
2125 struct vmcs *vmcs;
2126
Vlastimil Babka96db8002015-09-08 15:03:50 -07002127 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128 if (!pages)
2129 return NULL;
2130 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002131 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002132
2133 /* KVM supports Enlightened VMCS v1 only */
2134 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002135 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002136 else
Liran Alon392b2f22018-06-23 02:35:01 +03002137 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002138
Liran Alon491a6032018-06-23 02:35:12 +03002139 if (shadow)
2140 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 return vmcs;
2142}
2143
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002144void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002146 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147}
2148
Nadav Har'Eld462b812011-05-24 15:26:10 +03002149/*
2150 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2151 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002152void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002153{
2154 if (!loaded_vmcs->vmcs)
2155 return;
2156 loaded_vmcs_clear(loaded_vmcs);
2157 free_vmcs(loaded_vmcs->vmcs);
2158 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002159 if (loaded_vmcs->msr_bitmap)
2160 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002161 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002162}
2163
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002164int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002165{
Liran Alon491a6032018-06-23 02:35:12 +03002166 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002167 if (!loaded_vmcs->vmcs)
2168 return -ENOMEM;
2169
2170 loaded_vmcs->shadow_vmcs = NULL;
2171 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002172
2173 if (cpu_has_vmx_msr_bitmap()) {
2174 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
2175 if (!loaded_vmcs->msr_bitmap)
2176 goto out_vmcs;
2177 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002178
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002179 if (IS_ENABLED(CONFIG_HYPERV) &&
2180 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002181 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2182 struct hv_enlightened_vmcs *evmcs =
2183 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2184
2185 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2186 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002187 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002188
2189 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
2190
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002191 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002192
2193out_vmcs:
2194 free_loaded_vmcs(loaded_vmcs);
2195 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002196}
2197
Sam Ravnborg39959582007-06-01 00:47:13 -07002198static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199{
2200 int cpu;
2201
Zachary Amsden3230bb42009-09-29 11:38:37 -10002202 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002204 per_cpu(vmxarea, cpu) = NULL;
2205 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206}
2207
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208static __init int alloc_kvm_area(void)
2209{
2210 int cpu;
2211
Zachary Amsden3230bb42009-09-29 11:38:37 -10002212 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213 struct vmcs *vmcs;
2214
Liran Alon491a6032018-06-23 02:35:12 +03002215 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216 if (!vmcs) {
2217 free_kvm_area();
2218 return -ENOMEM;
2219 }
2220
Liran Alon2307af12018-06-29 22:59:04 +03002221 /*
2222 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2223 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2224 * revision_id reported by MSR_IA32_VMX_BASIC.
2225 *
2226 * However, even though not explictly documented by
2227 * TLFS, VMXArea passed as VMXON argument should
2228 * still be marked with revision_id reported by
2229 * physical CPU.
2230 */
2231 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002232 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002233
Avi Kivity6aa8b732006-12-10 02:21:36 -08002234 per_cpu(vmxarea, cpu) = vmcs;
2235 }
2236 return 0;
2237}
2238
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002239static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002240 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002242 if (!emulate_invalid_guest_state) {
2243 /*
2244 * CS and SS RPL should be equal during guest entry according
2245 * to VMX spec, but in reality it is not always so. Since vcpu
2246 * is in the middle of the transition from real mode to
2247 * protected mode it is safe to assume that RPL 0 is a good
2248 * default value.
2249 */
2250 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002251 save->selector &= ~SEGMENT_RPL_MASK;
2252 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002253 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002255 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256}
2257
2258static void enter_pmode(struct kvm_vcpu *vcpu)
2259{
2260 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002261 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262
Gleb Natapovd99e4152012-12-20 16:57:45 +02002263 /*
2264 * Update real mode segment cache. It may be not up-to-date if sement
2265 * register was written while vcpu was in a guest mode.
2266 */
2267 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2268 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2269 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2270 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2271 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2272 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2273
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002274 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275
Avi Kivity2fb92db2011-04-27 19:42:18 +03002276 vmx_segment_cache_clear(vmx);
2277
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002278 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279
2280 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002281 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2282 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283 vmcs_writel(GUEST_RFLAGS, flags);
2284
Rusty Russell66aee912007-07-17 23:34:16 +10002285 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2286 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287
2288 update_exception_bitmap(vcpu);
2289
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002290 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2291 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2292 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2293 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2294 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2295 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296}
2297
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002298static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299{
Mathias Krause772e0312012-08-30 01:30:19 +02002300 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002301 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302
Gleb Natapovd99e4152012-12-20 16:57:45 +02002303 var.dpl = 0x3;
2304 if (seg == VCPU_SREG_CS)
2305 var.type = 0x3;
2306
2307 if (!emulate_invalid_guest_state) {
2308 var.selector = var.base >> 4;
2309 var.base = var.base & 0xffff0;
2310 var.limit = 0xffff;
2311 var.g = 0;
2312 var.db = 0;
2313 var.present = 1;
2314 var.s = 1;
2315 var.l = 0;
2316 var.unusable = 0;
2317 var.type = 0x3;
2318 var.avl = 0;
2319 if (save->base & 0xf)
2320 printk_once(KERN_WARNING "kvm: segment base is not "
2321 "paragraph aligned when entering "
2322 "protected mode (seg=%d)", seg);
2323 }
2324
2325 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002326 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002327 vmcs_write32(sf->limit, var.limit);
2328 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329}
2330
2331static void enter_rmode(struct kvm_vcpu *vcpu)
2332{
2333 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002334 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002335 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002337 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2338 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2339 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2340 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2341 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002342 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2343 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002344
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002345 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346
Gleb Natapov776e58e2011-03-13 12:34:27 +02002347 /*
2348 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002349 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002350 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002351 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002352 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2353 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002354
Avi Kivity2fb92db2011-04-27 19:42:18 +03002355 vmx_segment_cache_clear(vmx);
2356
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002357 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2360
2361 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002362 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002364 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365
2366 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002367 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368 update_exception_bitmap(vcpu);
2369
Gleb Natapovd99e4152012-12-20 16:57:45 +02002370 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2371 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2372 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2373 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2374 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2375 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002376
Eddie Dong8668a3c2007-10-10 14:26:45 +08002377 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002380void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302381{
2382 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002383 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2384
2385 if (!msr)
2386 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302387
Avi Kivityf6801df2010-01-21 15:31:50 +02002388 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302389 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002390 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302391 msr->data = efer;
2392 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002393 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302394
2395 msr->data = efer & ~EFER_LME;
2396 }
2397 setup_msrs(vmx);
2398}
2399
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002400#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401
2402static void enter_lmode(struct kvm_vcpu *vcpu)
2403{
2404 u32 guest_tr_ar;
2405
Avi Kivity2fb92db2011-04-27 19:42:18 +03002406 vmx_segment_cache_clear(to_vmx(vcpu));
2407
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002409 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002410 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2411 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002413 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2414 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 }
Avi Kivityda38f432010-07-06 11:30:49 +03002416 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417}
2418
2419static void exit_lmode(struct kvm_vcpu *vcpu)
2420{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002421 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002422 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423}
2424
2425#endif
2426
Junaid Shahidfaff8752018-06-29 13:10:05 -07002427static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2428{
2429 int vpid = to_vmx(vcpu)->vpid;
2430
2431 if (!vpid_sync_vcpu_addr(vpid, addr))
2432 vpid_sync_context(vpid);
2433
2434 /*
2435 * If VPIDs are not supported or enabled, then the above is a no-op.
2436 * But we don't really need a TLB flush in that case anyway, because
2437 * each VM entry/exit includes an implicit flush when VPID is 0.
2438 */
2439}
2440
Avi Kivitye8467fd2009-12-29 18:43:06 +02002441static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2442{
2443 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2444
2445 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2446 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2447}
2448
Avi Kivityaff48ba2010-12-05 18:56:11 +02002449static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2450{
Sean Christophersonb4d18512018-03-05 12:04:40 -08002451 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02002452 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2453 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2454}
2455
Anthony Liguori25c4c272007-04-27 09:29:21 +03002456static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002457{
Avi Kivityfc78f512009-12-07 12:16:48 +02002458 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2459
2460 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2461 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002462}
2463
Sheng Yang14394422008-04-28 12:24:45 +08002464static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2465{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002466 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2467
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002468 if (!test_bit(VCPU_EXREG_PDPTR,
2469 (unsigned long *)&vcpu->arch.regs_dirty))
2470 return;
2471
Sheng Yang14394422008-04-28 12:24:45 +08002472 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002473 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2474 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2475 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2476 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002477 }
2478}
2479
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002480void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002481{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002482 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2483
Avi Kivity8f5d5492009-05-31 18:41:29 +03002484 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002485 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2486 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2487 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2488 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002489 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002490
2491 __set_bit(VCPU_EXREG_PDPTR,
2492 (unsigned long *)&vcpu->arch.regs_avail);
2493 __set_bit(VCPU_EXREG_PDPTR,
2494 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002495}
2496
Sheng Yang14394422008-04-28 12:24:45 +08002497static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2498 unsigned long cr0,
2499 struct kvm_vcpu *vcpu)
2500{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002501 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2502 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002503 if (!(cr0 & X86_CR0_PG)) {
2504 /* From paging/starting to nonpaging */
2505 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002506 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002507 (CPU_BASED_CR3_LOAD_EXITING |
2508 CPU_BASED_CR3_STORE_EXITING));
2509 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002510 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002511 } else if (!is_paging(vcpu)) {
2512 /* From nonpaging to paging */
2513 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002514 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002515 ~(CPU_BASED_CR3_LOAD_EXITING |
2516 CPU_BASED_CR3_STORE_EXITING));
2517 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002518 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002519 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002520
2521 if (!(cr0 & X86_CR0_WP))
2522 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002523}
2524
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002525void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002527 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002528 unsigned long hw_cr0;
2529
Sean Christopherson3de63472018-07-13 08:42:30 -07002530 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002531 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02002532 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02002533 else {
Gleb Natapov50378782013-02-04 16:00:28 +02002534 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002535
Gleb Natapov218e7632013-01-21 15:36:45 +02002536 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
2537 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538
Gleb Natapov218e7632013-01-21 15:36:45 +02002539 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
2540 enter_rmode(vcpu);
2541 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002543#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002544 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002545 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10002547 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 exit_lmode(vcpu);
2549 }
2550#endif
2551
Sean Christophersonb4d18512018-03-05 12:04:40 -08002552 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08002553 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2554
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002556 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002557 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02002558
2559 /* depends on vcpu->arch.cr0 to be set to a new value */
2560 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561}
2562
Yu Zhang855feb62017-08-24 20:27:55 +08002563static int get_ept_level(struct kvm_vcpu *vcpu)
2564{
2565 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
2566 return 5;
2567 return 4;
2568}
2569
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002570u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08002571{
Yu Zhang855feb62017-08-24 20:27:55 +08002572 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08002573
Yu Zhang855feb62017-08-24 20:27:55 +08002574 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08002575
Peter Feiner995f00a2017-06-30 17:26:32 -07002576 if (enable_ept_ad_bits &&
2577 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02002578 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08002579 eptp |= (root_hpa & PAGE_MASK);
2580
2581 return eptp;
2582}
2583
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002584void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585{
Tianyu Lan877ad952018-07-19 08:40:23 +00002586 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08002587 unsigned long guest_cr3;
2588 u64 eptp;
2589
2590 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002591 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07002592 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08002593 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00002594
2595 if (kvm_x86_ops->tlb_remote_flush) {
2596 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
2597 to_vmx(vcpu)->ept_pointer = eptp;
2598 to_kvm_vmx(kvm)->ept_pointers_match
2599 = EPT_POINTERS_CHECK;
2600 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
2601 }
2602
Sean Christophersone90008d2018-03-05 12:04:37 -08002603 if (enable_unrestricted_guest || is_paging(vcpu) ||
2604 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02002605 guest_cr3 = kvm_read_cr3(vcpu);
2606 else
Tianyu Lan877ad952018-07-19 08:40:23 +00002607 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02002608 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002609 }
2610
Sheng Yang14394422008-04-28 12:24:45 +08002611 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612}
2613
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002614int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615{
Ben Serebrin085e68e2015-04-16 11:58:05 -07002616 /*
2617 * Pass through host's Machine Check Enable value to hw_cr4, which
2618 * is in force while we are in guest mode. Do not let guests control
2619 * this bit, even if host CR4.MCE == 0.
2620 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002621 unsigned long hw_cr4;
2622
2623 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
2624 if (enable_unrestricted_guest)
2625 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
2626 else if (to_vmx(vcpu)->rmode.vm86_active)
2627 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
2628 else
2629 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002630
Sean Christopherson64f7a112018-04-30 10:01:06 -07002631 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
2632 if (cr4 & X86_CR4_UMIP) {
2633 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02002634 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07002635 hw_cr4 &= ~X86_CR4_UMIP;
2636 } else if (!is_guest_mode(vcpu) ||
2637 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
2638 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
2639 SECONDARY_EXEC_DESC);
2640 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02002641
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002642 if (cr4 & X86_CR4_VMXE) {
2643 /*
2644 * To use VMXON (and later other VMX instructions), a guest
2645 * must first be able to turn on cr4.VMXE (see handle_vmon()).
2646 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02002647 * is here. We operate under the default treatment of SMM,
2648 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002649 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02002650 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002651 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01002652 }
David Matlack38991522016-11-29 18:14:08 -08002653
2654 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002655 return 1;
2656
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002657 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08002658
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002659 if (!enable_unrestricted_guest) {
2660 if (enable_ept) {
2661 if (!is_paging(vcpu)) {
2662 hw_cr4 &= ~X86_CR4_PAE;
2663 hw_cr4 |= X86_CR4_PSE;
2664 } else if (!(cr4 & X86_CR4_PAE)) {
2665 hw_cr4 &= ~X86_CR4_PAE;
2666 }
2667 }
2668
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002669 /*
Huaitong Handdba2622016-03-22 16:51:15 +08002670 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
2671 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
2672 * to be manually disabled when guest switches to non-paging
2673 * mode.
2674 *
2675 * If !enable_unrestricted_guest, the CPU is always running
2676 * with CR0.PG=1 and CR4 needs to be modified.
2677 * If enable_unrestricted_guest, the CPU automatically
2678 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002679 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08002680 if (!is_paging(vcpu))
2681 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
2682 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01002683
Sheng Yang14394422008-04-28 12:24:45 +08002684 vmcs_writel(CR4_READ_SHADOW, cr4);
2685 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002686 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687}
2688
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002689void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690{
Avi Kivitya9179492011-01-03 14:28:52 +02002691 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 u32 ar;
2693
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002694 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002695 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02002696 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002697 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002698 return;
Avi Kivity1390a282012-08-21 17:07:08 +03002699 var->base = vmx_read_guest_seg_base(vmx, seg);
2700 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2701 return;
Avi Kivitya9179492011-01-03 14:28:52 +02002702 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002703 var->base = vmx_read_guest_seg_base(vmx, seg);
2704 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2705 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2706 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03002707 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708 var->type = ar & 15;
2709 var->s = (ar >> 4) & 1;
2710 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03002711 /*
2712 * Some userspaces do not preserve unusable property. Since usable
2713 * segment has to be present according to VMX spec we can use present
2714 * property to amend userspace bug by making unusable segment always
2715 * nonpresent. vmx_segment_access_rights() already marks nonpresent
2716 * segment as unusable.
2717 */
2718 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 var->avl = (ar >> 12) & 1;
2720 var->l = (ar >> 13) & 1;
2721 var->db = (ar >> 14) & 1;
2722 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723}
2724
Avi Kivitya9179492011-01-03 14:28:52 +02002725static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2726{
Avi Kivitya9179492011-01-03 14:28:52 +02002727 struct kvm_segment s;
2728
2729 if (to_vmx(vcpu)->rmode.vm86_active) {
2730 vmx_get_segment(vcpu, &s, seg);
2731 return s.base;
2732 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002733 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002734}
2735
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002736int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002737{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02002738 struct vcpu_vmx *vmx = to_vmx(vcpu);
2739
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002740 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002741 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002742 else {
2743 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002744 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002745 }
Avi Kivity69c73022011-03-07 15:26:44 +02002746}
2747
Avi Kivity653e3102007-05-07 10:55:37 +03002748static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 u32 ar;
2751
Avi Kivityf0495f92012-06-07 17:06:10 +03002752 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 ar = 1 << 16;
2754 else {
2755 ar = var->type & 15;
2756 ar |= (var->s & 1) << 4;
2757 ar |= (var->dpl & 3) << 5;
2758 ar |= (var->present & 1) << 7;
2759 ar |= (var->avl & 1) << 12;
2760 ar |= (var->l & 1) << 13;
2761 ar |= (var->db & 1) << 14;
2762 ar |= (var->g & 1) << 15;
2763 }
Avi Kivity653e3102007-05-07 10:55:37 +03002764
2765 return ar;
2766}
2767
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002768void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03002769{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002770 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02002771 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03002772
Avi Kivity2fb92db2011-04-27 19:42:18 +03002773 vmx_segment_cache_clear(vmx);
2774
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02002775 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
2776 vmx->rmode.segs[seg] = *var;
2777 if (seg == VCPU_SREG_TR)
2778 vmcs_write16(sf->selector, var->selector);
2779 else if (var->s)
2780 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002781 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03002782 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02002783
Avi Kivity653e3102007-05-07 10:55:37 +03002784 vmcs_writel(sf->base, var->base);
2785 vmcs_write32(sf->limit, var->limit);
2786 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002787
2788 /*
2789 * Fix the "Accessed" bit in AR field of segment registers for older
2790 * qemu binaries.
2791 * IA32 arch specifies that at the time of processor reset the
2792 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08002793 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002794 * state vmexit when "unrestricted guest" mode is turned on.
2795 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2796 * tree. Newer qemu binaries with that qemu fix would not need this
2797 * kvm hack.
2798 */
2799 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02002800 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002801
Gleb Natapovf924d662012-12-12 19:10:55 +02002802 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02002803
2804out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01002805 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806}
2807
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2809{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002810 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811
2812 *db = (ar >> 14) & 1;
2813 *l = (ar >> 13) & 1;
2814}
2815
Gleb Natapov89a27f42010-02-16 10:51:48 +02002816static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002818 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2819 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820}
2821
Gleb Natapov89a27f42010-02-16 10:51:48 +02002822static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002824 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2825 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826}
2827
Gleb Natapov89a27f42010-02-16 10:51:48 +02002828static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002830 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2831 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832}
2833
Gleb Natapov89a27f42010-02-16 10:51:48 +02002834static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002836 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2837 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838}
2839
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002840static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2841{
2842 struct kvm_segment var;
2843 u32 ar;
2844
2845 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02002846 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02002847 if (seg == VCPU_SREG_CS)
2848 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002849 ar = vmx_segment_access_rights(&var);
2850
2851 if (var.base != (var.selector << 4))
2852 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02002853 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002854 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02002855 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002856 return false;
2857
2858 return true;
2859}
2860
2861static bool code_segment_valid(struct kvm_vcpu *vcpu)
2862{
2863 struct kvm_segment cs;
2864 unsigned int cs_rpl;
2865
2866 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03002867 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002868
Avi Kivity1872a3f2009-01-04 23:26:52 +02002869 if (cs.unusable)
2870 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002871 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002872 return false;
2873 if (!cs.s)
2874 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002875 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002876 if (cs.dpl > cs_rpl)
2877 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002878 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002879 if (cs.dpl != cs_rpl)
2880 return false;
2881 }
2882 if (!cs.present)
2883 return false;
2884
2885 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2886 return true;
2887}
2888
2889static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2890{
2891 struct kvm_segment ss;
2892 unsigned int ss_rpl;
2893
2894 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03002895 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002896
Avi Kivity1872a3f2009-01-04 23:26:52 +02002897 if (ss.unusable)
2898 return true;
2899 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002900 return false;
2901 if (!ss.s)
2902 return false;
2903 if (ss.dpl != ss_rpl) /* DPL != RPL */
2904 return false;
2905 if (!ss.present)
2906 return false;
2907
2908 return true;
2909}
2910
2911static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2912{
2913 struct kvm_segment var;
2914 unsigned int rpl;
2915
2916 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03002917 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002918
Avi Kivity1872a3f2009-01-04 23:26:52 +02002919 if (var.unusable)
2920 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002921 if (!var.s)
2922 return false;
2923 if (!var.present)
2924 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002925 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002926 if (var.dpl < rpl) /* DPL < RPL */
2927 return false;
2928 }
2929
2930 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2931 * rights flags
2932 */
2933 return true;
2934}
2935
2936static bool tr_valid(struct kvm_vcpu *vcpu)
2937{
2938 struct kvm_segment tr;
2939
2940 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2941
Avi Kivity1872a3f2009-01-04 23:26:52 +02002942 if (tr.unusable)
2943 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03002944 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002945 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002946 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002947 return false;
2948 if (!tr.present)
2949 return false;
2950
2951 return true;
2952}
2953
2954static bool ldtr_valid(struct kvm_vcpu *vcpu)
2955{
2956 struct kvm_segment ldtr;
2957
2958 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2959
Avi Kivity1872a3f2009-01-04 23:26:52 +02002960 if (ldtr.unusable)
2961 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03002962 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002963 return false;
2964 if (ldtr.type != 2)
2965 return false;
2966 if (!ldtr.present)
2967 return false;
2968
2969 return true;
2970}
2971
2972static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2973{
2974 struct kvm_segment cs, ss;
2975
2976 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2977 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2978
Nadav Amitb32a9912015-03-29 16:33:04 +03002979 return ((cs.selector & SEGMENT_RPL_MASK) ==
2980 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002981}
2982
2983/*
2984 * Check if guest state is valid. Returns true if valid, false if
2985 * not.
2986 * We assume that registers are always usable
2987 */
2988static bool guest_state_valid(struct kvm_vcpu *vcpu)
2989{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02002990 if (enable_unrestricted_guest)
2991 return true;
2992
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002993 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03002994 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002995 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2996 return false;
2997 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2998 return false;
2999 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3000 return false;
3001 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3002 return false;
3003 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3004 return false;
3005 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3006 return false;
3007 } else {
3008 /* protected mode guest state checks */
3009 if (!cs_ss_rpl_check(vcpu))
3010 return false;
3011 if (!code_segment_valid(vcpu))
3012 return false;
3013 if (!stack_segment_valid(vcpu))
3014 return false;
3015 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3016 return false;
3017 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3018 return false;
3019 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3020 return false;
3021 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3022 return false;
3023 if (!tr_valid(vcpu))
3024 return false;
3025 if (!ldtr_valid(vcpu))
3026 return false;
3027 }
3028 /* TODO:
3029 * - Add checks on RIP
3030 * - Add checks on RFLAGS
3031 */
3032
3033 return true;
3034}
3035
Mike Dayd77c26f2007-10-08 09:02:08 -04003036static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003038 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003039 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003040 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003042 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003043 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003044 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3045 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003046 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003047 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003048 r = kvm_write_guest_page(kvm, fn++, &data,
3049 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003050 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003051 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003052 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3053 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003054 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003055 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3056 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003057 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003058 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003059 r = kvm_write_guest_page(kvm, fn, &data,
3060 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3061 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003062out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003063 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003064 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065}
3066
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003067static int init_rmode_identity_map(struct kvm *kvm)
3068{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003069 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08003070 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003071 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003072 u32 tmp;
3073
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003074 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003075 mutex_lock(&kvm->slots_lock);
3076
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003077 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08003078 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08003079
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003080 if (!kvm_vmx->ept_identity_map_addr)
3081 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3082 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003083
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003084 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003085 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003086 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08003087 goto out2;
3088
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003089 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003090 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3091 if (r < 0)
3092 goto out;
3093 /* Set up identity-mapping pagetable for EPT in real mode */
3094 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3095 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3096 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3097 r = kvm_write_guest_page(kvm, identity_map_pfn,
3098 &tmp, i * sizeof(tmp), sizeof(tmp));
3099 if (r < 0)
3100 goto out;
3101 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003102 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003103
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003104out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003105 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08003106
3107out2:
3108 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003109 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003110}
3111
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112static void seg_setup(int seg)
3113{
Mathias Krause772e0312012-08-30 01:30:19 +02003114 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003115 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116
3117 vmcs_write16(sf->selector, 0);
3118 vmcs_writel(sf->base, 0);
3119 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003120 ar = 0x93;
3121 if (seg == VCPU_SREG_CS)
3122 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003123
3124 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125}
3126
Sheng Yangf78e0e22007-10-29 09:40:42 +08003127static int alloc_apic_access_page(struct kvm *kvm)
3128{
Xiao Guangrong44841412012-09-07 14:14:20 +08003129 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003130 int r = 0;
3131
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003132 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003133 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003134 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003135 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3136 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003137 if (r)
3138 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003139
Tang Chen73a6d942014-09-11 13:38:00 +08003140 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003141 if (is_error_page(page)) {
3142 r = -EFAULT;
3143 goto out;
3144 }
3145
Tang Chenc24ae0d2014-09-24 15:57:58 +08003146 /*
3147 * Do not pin the page in memory, so that memory hot-unplug
3148 * is able to migrate it.
3149 */
3150 put_page(page);
3151 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003152out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003153 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003154 return r;
3155}
3156
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003157int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003158{
3159 int vpid;
3160
Avi Kivity919818a2009-03-23 18:01:29 +02003161 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003162 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003163 spin_lock(&vmx_vpid_lock);
3164 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003165 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003166 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003167 else
3168 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003169 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003170 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003171}
3172
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003173void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003174{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003175 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003176 return;
3177 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003178 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003179 spin_unlock(&vmx_vpid_lock);
3180}
3181
Yi Wang1e4329ee2018-11-08 11:22:21 +08003182static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003183 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003184{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003185 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003186
3187 if (!cpu_has_vmx_msr_bitmap())
3188 return;
3189
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003190 if (static_branch_unlikely(&enable_evmcs))
3191 evmcs_touch_msr_bitmap();
3192
Sheng Yang25c5f222008-03-28 13:18:56 +08003193 /*
3194 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3195 * have the write-low and read-high bitmap offsets the wrong way round.
3196 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3197 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003198 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003199 if (type & MSR_TYPE_R)
3200 /* read-low */
3201 __clear_bit(msr, msr_bitmap + 0x000 / f);
3202
3203 if (type & MSR_TYPE_W)
3204 /* write-low */
3205 __clear_bit(msr, msr_bitmap + 0x800 / f);
3206
Sheng Yang25c5f222008-03-28 13:18:56 +08003207 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3208 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003209 if (type & MSR_TYPE_R)
3210 /* read-high */
3211 __clear_bit(msr, msr_bitmap + 0x400 / f);
3212
3213 if (type & MSR_TYPE_W)
3214 /* write-high */
3215 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3216
3217 }
3218}
3219
Yi Wang1e4329ee2018-11-08 11:22:21 +08003220static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003221 u32 msr, int type)
3222{
3223 int f = sizeof(unsigned long);
3224
3225 if (!cpu_has_vmx_msr_bitmap())
3226 return;
3227
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003228 if (static_branch_unlikely(&enable_evmcs))
3229 evmcs_touch_msr_bitmap();
3230
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003231 /*
3232 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3233 * have the write-low and read-high bitmap offsets the wrong way round.
3234 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3235 */
3236 if (msr <= 0x1fff) {
3237 if (type & MSR_TYPE_R)
3238 /* read-low */
3239 __set_bit(msr, msr_bitmap + 0x000 / f);
3240
3241 if (type & MSR_TYPE_W)
3242 /* write-low */
3243 __set_bit(msr, msr_bitmap + 0x800 / f);
3244
3245 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3246 msr &= 0x1fff;
3247 if (type & MSR_TYPE_R)
3248 /* read-high */
3249 __set_bit(msr, msr_bitmap + 0x400 / f);
3250
3251 if (type & MSR_TYPE_W)
3252 /* write-high */
3253 __set_bit(msr, msr_bitmap + 0xc00 / f);
3254
3255 }
3256}
3257
Yi Wang1e4329ee2018-11-08 11:22:21 +08003258static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003259 u32 msr, int type, bool value)
3260{
3261 if (value)
3262 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
3263 else
3264 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
3265}
3266
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003267static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003268{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003269 u8 mode = 0;
3270
3271 if (cpu_has_secondary_exec_ctrls() &&
3272 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
3273 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3274 mode |= MSR_BITMAP_MODE_X2APIC;
3275 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3276 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3277 }
3278
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003279 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003280}
3281
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003282static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
3283 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003284{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003285 int msr;
3286
3287 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3288 unsigned word = msr / BITS_PER_LONG;
3289 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3290 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003291 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003292
3293 if (mode & MSR_BITMAP_MODE_X2APIC) {
3294 /*
3295 * TPR reads and writes can be virtualized even if virtual interrupt
3296 * delivery is not in use.
3297 */
3298 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
3299 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3300 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
3301 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3302 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
3303 }
3304 }
3305}
3306
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003307void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003308{
3309 struct vcpu_vmx *vmx = to_vmx(vcpu);
3310 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3311 u8 mode = vmx_msr_bitmap_mode(vcpu);
3312 u8 changed = mode ^ vmx->msr_bitmap_mode;
3313
3314 if (!changed)
3315 return;
3316
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003317 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
3318 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
3319
3320 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003321}
3322
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05003323static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003324{
Andrey Smetanind62caab2015-11-10 15:36:33 +03003325 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003326}
3327
Liran Alone6c67d82018-09-04 10:56:52 +03003328static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3329{
3330 struct vcpu_vmx *vmx = to_vmx(vcpu);
3331 void *vapic_page;
3332 u32 vppr;
3333 int rvi;
3334
3335 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3336 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
3337 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
3338 return false;
3339
Paolo Bonzini7e712682018-10-03 13:44:26 +02003340 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003341
3342 vapic_page = kmap(vmx->nested.virtual_apic_page);
3343 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
3344 kunmap(vmx->nested.virtual_apic_page);
3345
3346 return ((rvi & 0xf0) > (vppr & 0xf0));
3347}
3348
Wincy Van06a55242017-04-28 13:13:59 +08003349static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3350 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003351{
3352#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003353 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3354
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003355 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003356 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003357 * The vector of interrupt to be delivered to vcpu had
3358 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003359 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003360 * Following cases will be reached in this block, and
3361 * we always send a notification event in all cases as
3362 * explained below.
3363 *
3364 * Case 1: vcpu keeps in non-root mode. Sending a
3365 * notification event posts the interrupt to vcpu.
3366 *
3367 * Case 2: vcpu exits to root mode and is still
3368 * runnable. PIR will be synced to vIRR before the
3369 * next vcpu entry. Sending a notification event in
3370 * this case has no effect, as vcpu is not in root
3371 * mode.
3372 *
3373 * Case 3: vcpu exits to root mode and is blocked.
3374 * vcpu_block() has already synced PIR to vIRR and
3375 * never blocks vcpu if vIRR is not cleared. Therefore,
3376 * a blocked vcpu here does not wait for any requested
3377 * interrupts in PIR, and sending a notification event
3378 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003379 */
Feng Wu28b835d2015-09-18 22:29:54 +08003380
Wincy Van06a55242017-04-28 13:13:59 +08003381 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003382 return true;
3383 }
3384#endif
3385 return false;
3386}
3387
Wincy Van705699a2015-02-03 23:58:17 +08003388static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3389 int vector)
3390{
3391 struct vcpu_vmx *vmx = to_vmx(vcpu);
3392
3393 if (is_guest_mode(vcpu) &&
3394 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003395 /*
3396 * If a posted intr is not recognized by hardware,
3397 * we will accomplish it in the next vmentry.
3398 */
3399 vmx->nested.pi_pending = true;
3400 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003401 /* the PIR and ON have been set by L1. */
3402 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3403 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003404 return 0;
3405 }
3406 return -1;
3407}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003409 * Send interrupt to vcpu via posted interrupt way.
3410 * 1. If target vcpu is running(non-root mode), send posted interrupt
3411 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3412 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3413 * interrupt from PIR in next vmentry.
3414 */
3415static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
3416{
3417 struct vcpu_vmx *vmx = to_vmx(vcpu);
3418 int r;
3419
Wincy Van705699a2015-02-03 23:58:17 +08003420 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3421 if (!r)
3422 return;
3423
Yang Zhanga20ed542013-04-11 19:25:15 +08003424 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
3425 return;
3426
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003427 /* If a previous notification has sent the IPI, nothing to do. */
3428 if (pi_test_and_set_on(&vmx->pi_desc))
3429 return;
3430
Wincy Van06a55242017-04-28 13:13:59 +08003431 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003432 kvm_vcpu_kick(vcpu);
3433}
3434
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003436 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3437 * will not change in the lifetime of the guest.
3438 * Note that host-state that does change is set elsewhere. E.g., host-state
3439 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3440 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003441void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003442{
3443 u32 low32, high32;
3444 unsigned long tmpl;
3445 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003446 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003447
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003448 cr0 = read_cr0();
3449 WARN_ON(cr0 & X86_CR0_TS);
3450 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003451
3452 /*
3453 * Save the most likely value for this task's CR3 in the VMCS.
3454 * We can't use __get_current_cr3_fast() because we're not atomic.
3455 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07003456 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003457 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003458 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003459
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003460 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003461 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003462 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003463 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003464
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003465 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003466#ifdef CONFIG_X86_64
3467 /*
3468 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003469 * vmx_prepare_switch_to_host(), in case userspace uses
3470 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03003471 */
3472 vmcs_write16(HOST_DS_SELECTOR, 0);
3473 vmcs_write16(HOST_ES_SELECTOR, 0);
3474#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003475 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3476 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003477#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003478 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3479 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3480
Juergen Gross87930012017-09-04 12:25:27 +02003481 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003482 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08003483 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003484
Avi Kivity83287ea422012-09-16 15:10:57 +03003485 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003486
3487 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3488 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3489 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3490 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3491
3492 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3493 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3494 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3495 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003496
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003497 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07003498 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003499}
3500
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003501void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003502{
3503 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
3504 if (enable_ept)
3505 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003506 if (is_guest_mode(&vmx->vcpu))
3507 vmx->vcpu.arch.cr4_guest_owned_bits &=
3508 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003509 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3510}
3511
Yang Zhang01e439b2013-04-11 19:25:12 +08003512static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
3513{
3514 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
3515
Andrey Smetanind62caab2015-11-10 15:36:33 +03003516 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08003517 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003518
3519 if (!enable_vnmi)
3520 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
3521
Yunhong Jiang64672c92016-06-13 14:19:59 -07003522 /* Enable the preemption timer dynamically */
3523 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003524 return pin_based_exec_ctrl;
3525}
3526
Andrey Smetanind62caab2015-11-10 15:36:33 +03003527static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
3528{
3529 struct vcpu_vmx *vmx = to_vmx(vcpu);
3530
3531 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03003532 if (cpu_has_secondary_exec_ctrls()) {
3533 if (kvm_vcpu_apicv_active(vcpu))
3534 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
3535 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3536 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3537 else
3538 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
3539 SECONDARY_EXEC_APIC_REGISTER_VIRT |
3540 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3541 }
3542
3543 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003544 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03003545}
3546
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003547u32 vmx_exec_control(struct vcpu_vmx *vmx)
3548{
3549 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3550
3551 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
3552 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
3553
3554 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
3555 exec_control &= ~CPU_BASED_TPR_SHADOW;
3556#ifdef CONFIG_X86_64
3557 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3558 CPU_BASED_CR8_LOAD_EXITING;
3559#endif
3560 }
3561 if (!enable_ept)
3562 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3563 CPU_BASED_CR3_LOAD_EXITING |
3564 CPU_BASED_INVLPG_EXITING;
3565 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
3566 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
3567 CPU_BASED_MONITOR_EXITING);
3568 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
3569 exec_control &= ~CPU_BASED_HLT_EXITING;
3570 return exec_control;
3571}
3572
3573
Paolo Bonzini80154d72017-08-24 13:55:35 +02003574static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003575{
Paolo Bonzini80154d72017-08-24 13:55:35 +02003576 struct kvm_vcpu *vcpu = &vmx->vcpu;
3577
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003578 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02003579
Chao Pengf99e3da2018-10-24 16:05:10 +08003580 if (pt_mode == PT_MODE_SYSTEM)
3581 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02003582 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003583 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3584 if (vmx->vpid == 0)
3585 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
3586 if (!enable_ept) {
3587 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
3588 enable_unrestricted_guest = 0;
3589 }
3590 if (!enable_unrestricted_guest)
3591 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07003592 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003593 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02003594 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08003595 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
3596 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08003597 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02003598
3599 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
3600 * in vmx_set_cr4. */
3601 exec_control &= ~SECONDARY_EXEC_DESC;
3602
Abel Gordonabc4fc52013-04-18 14:35:25 +03003603 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
3604 (handle_vmptrld).
3605 We can NOT enable shadow_vmcs here because we don't have yet
3606 a current VMCS12
3607 */
3608 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08003609
3610 if (!enable_pml)
3611 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08003612
Paolo Bonzini3db13482017-08-24 14:48:03 +02003613 if (vmx_xsaves_supported()) {
3614 /* Exposing XSAVES only when XSAVE is exposed */
3615 bool xsaves_enabled =
3616 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
3617 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
3618
3619 if (!xsaves_enabled)
3620 exec_control &= ~SECONDARY_EXEC_XSAVES;
3621
3622 if (nested) {
3623 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003624 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02003625 SECONDARY_EXEC_XSAVES;
3626 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003627 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02003628 ~SECONDARY_EXEC_XSAVES;
3629 }
3630 }
3631
Paolo Bonzini80154d72017-08-24 13:55:35 +02003632 if (vmx_rdtscp_supported()) {
3633 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
3634 if (!rdtscp_enabled)
3635 exec_control &= ~SECONDARY_EXEC_RDTSCP;
3636
3637 if (nested) {
3638 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003639 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003640 SECONDARY_EXEC_RDTSCP;
3641 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003642 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003643 ~SECONDARY_EXEC_RDTSCP;
3644 }
3645 }
3646
3647 if (vmx_invpcid_supported()) {
3648 /* Exposing INVPCID only when PCID is exposed */
3649 bool invpcid_enabled =
3650 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
3651 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
3652
3653 if (!invpcid_enabled) {
3654 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
3655 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
3656 }
3657
3658 if (nested) {
3659 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003660 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003661 SECONDARY_EXEC_ENABLE_INVPCID;
3662 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02003664 ~SECONDARY_EXEC_ENABLE_INVPCID;
3665 }
3666 }
3667
Jim Mattson45ec3682017-08-23 16:32:04 -07003668 if (vmx_rdrand_supported()) {
3669 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
3670 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02003671 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003672
3673 if (nested) {
3674 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003675 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003676 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003677 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003679 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07003680 }
3681 }
3682
Jim Mattson75f4fc82017-08-23 16:32:03 -07003683 if (vmx_rdseed_supported()) {
3684 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
3685 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02003686 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003687
3688 if (nested) {
3689 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003690 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003691 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003692 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003693 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02003694 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07003695 }
3696 }
3697
Paolo Bonzini80154d72017-08-24 13:55:35 +02003698 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003699}
3700
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003701static void ept_set_mmio_spte_mask(void)
3702{
3703 /*
3704 * EPT Misconfigurations can be generated if the value of bits 2:0
3705 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003706 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07003707 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
3708 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003709}
3710
Wanpeng Lif53cd632014-12-02 19:14:58 +08003711#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712
Sean Christopherson944c3462018-12-03 13:53:09 -08003713/*
3714 * Sets up the vmcs for emulated real mode.
3715 */
3716static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
3717{
3718 int i;
3719
3720 if (nested)
3721 nested_vmx_vcpu_setup();
3722
Sheng Yang25c5f222008-03-28 13:18:56 +08003723 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003724 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08003725
Avi Kivity6aa8b732006-12-10 02:21:36 -08003726 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
3727
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08003729 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07003730 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003731
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003732 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003733
Dan Williamsdfa169b2016-06-02 11:17:24 -07003734 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02003735 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003736 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02003737 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07003738 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08003739
Andrey Smetanind62caab2015-11-10 15:36:33 +03003740 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08003741 vmcs_write64(EOI_EXIT_BITMAP0, 0);
3742 vmcs_write64(EOI_EXIT_BITMAP1, 0);
3743 vmcs_write64(EOI_EXIT_BITMAP2, 0);
3744 vmcs_write64(EOI_EXIT_BITMAP3, 0);
3745
3746 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08003747
Li RongQing0bcf2612015-12-03 13:29:34 +08003748 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08003749 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08003750 }
3751
Wanpeng Lib31c1142018-03-12 04:53:04 -07003752 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003753 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02003754 vmx->ple_window = ple_window;
3755 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003756 }
3757
Xiao Guangrongc3707952011-07-12 03:28:04 +08003758 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
3759 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
3761
Avi Kivity9581d442010-10-19 16:46:55 +02003762 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
3763 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08003764 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003765 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
3766 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003767
Bandan Das2a499e42017-08-03 15:54:41 -04003768 if (cpu_has_vmx_vmfunc())
3769 vmcs_write64(VM_FUNCTION_CONTROL, 0);
3770
Eddie Dong2cc51562007-05-21 07:28:09 +03003771 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
3772 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04003773 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03003774 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04003775 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776
Radim Krčmář74545702015-04-27 15:11:25 +02003777 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
3778 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08003779
Paolo Bonzini03916db2014-07-24 14:21:57 +02003780 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003781 u32 index = vmx_msr_index[i];
3782 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003783 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784
3785 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3786 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08003787 if (wrmsr_safe(index, data_low, data_high) < 0)
3788 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03003789 vmx->guest_msrs[j].index = i;
3790 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02003791 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003792 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02003795 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02003796
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003797 vm_exit_controls_init(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798
3799 /* 22.2.1, 20.8.1 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -08003800 vm_entry_controls_init(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003801
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08003802 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
3803 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
3804
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003805 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003806
Wanpeng Lif53cd632014-12-02 19:14:58 +08003807 if (vmx_xsaves_supported())
3808 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
3809
Peter Feiner4e595162016-07-07 14:49:58 -07003810 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07003811 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
3812 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
3813 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07003814
3815 if (cpu_has_vmx_encls_vmexit())
3816 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003817}
3818
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003819static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003820{
3821 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01003822 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003823 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003824
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003825 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003826 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003827
Wanpeng Li518e7b92018-02-28 14:03:31 +08003828 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003829 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003830 kvm_set_cr8(vcpu, 0);
3831
3832 if (!init_event) {
3833 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
3834 MSR_IA32_APICBASE_ENABLE;
3835 if (kvm_vcpu_is_reset_bsp(vcpu))
3836 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
3837 apic_base_msr.host_initiated = true;
3838 kvm_set_apic_base(vcpu, &apic_base_msr);
3839 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003840
Avi Kivity2fb92db2011-04-27 19:42:18 +03003841 vmx_segment_cache_clear(vmx);
3842
Avi Kivity5706be02008-08-20 15:07:31 +03003843 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01003844 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01003845 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003846
3847 seg_setup(VCPU_SREG_DS);
3848 seg_setup(VCPU_SREG_ES);
3849 seg_setup(VCPU_SREG_FS);
3850 seg_setup(VCPU_SREG_GS);
3851 seg_setup(VCPU_SREG_SS);
3852
3853 vmcs_write16(GUEST_TR_SELECTOR, 0);
3854 vmcs_writel(GUEST_TR_BASE, 0);
3855 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3856 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3857
3858 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3859 vmcs_writel(GUEST_LDTR_BASE, 0);
3860 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3861 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3862
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003863 if (!init_event) {
3864 vmcs_write32(GUEST_SYSENTER_CS, 0);
3865 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3866 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3867 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3868 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003869
Wanpeng Lic37c2872017-11-20 14:52:21 -08003870 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01003871 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003872
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003873 vmcs_writel(GUEST_GDTR_BASE, 0);
3874 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
3875
3876 vmcs_writel(GUEST_IDTR_BASE, 0);
3877 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
3878
Anthony Liguori443381a2010-12-06 10:53:38 -06003879 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003880 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01003881 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07003882 if (kvm_mpx_supported())
3883 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003884
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003885 setup_msrs(vmx);
3886
Avi Kivity6aa8b732006-12-10 02:21:36 -08003887 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3888
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003889 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08003890 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003891 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08003892 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003893 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08003894 vmcs_write32(TPR_THRESHOLD, 0);
3895 }
3896
Paolo Bonzinia73896c2014-11-02 07:54:30 +01003897 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898
Sheng Yang2384d2b2008-01-17 15:14:33 +08003899 if (vmx->vpid != 0)
3900 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3901
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003902 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003903 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06003904 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003905 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02003906 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08003907
Nadav Amitd28bc9d2015-04-13 14:34:08 +03003908 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003910 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003911 if (init_event)
3912 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003913}
3914
Jan Kiszkac9a79532014-03-07 20:03:15 +01003915static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003916{
Paolo Bonzini47c01522016-12-19 11:44:07 +01003917 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
3918 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003919}
3920
Jan Kiszkac9a79532014-03-07 20:03:15 +01003921static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003922{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003923 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003924 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01003925 enable_irq_window(vcpu);
3926 return;
3927 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02003928
Paolo Bonzini47c01522016-12-19 11:44:07 +01003929 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
3930 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003931}
3932
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003933static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003934{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003935 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003936 uint32_t intr;
3937 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003938
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003939 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003940
Avi Kivityfa89a812008-09-01 15:57:51 +03003941 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003942 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003943 int inc_eip = 0;
3944 if (vcpu->arch.interrupt.soft)
3945 inc_eip = vcpu->arch.event_exit_inst_len;
3946 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003947 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003948 return;
3949 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003950 intr = irq | INTR_INFO_VALID_MASK;
3951 if (vcpu->arch.interrupt.soft) {
3952 intr |= INTR_TYPE_SOFT_INTR;
3953 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3954 vmx->vcpu.arch.event_exit_inst_len);
3955 } else
3956 intr |= INTR_TYPE_EXT_INTR;
3957 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003958
3959 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003960}
3961
Sheng Yangf08864b2008-05-15 18:23:25 +08003962static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3963{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003964 struct vcpu_vmx *vmx = to_vmx(vcpu);
3965
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003966 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01003967 /*
3968 * Tracking the NMI-blocked state in software is built upon
3969 * finding the next open IRQ window. This, in turn, depends on
3970 * well-behaving guests: They have to keep IRQs disabled at
3971 * least as long as the NMI handler runs. Otherwise we may
3972 * cause NMI nesting, maybe breaking the guest. But as this is
3973 * highly unlikely, we can live with the residual risk.
3974 */
3975 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
3976 vmx->loaded_vmcs->vnmi_blocked_time = 0;
3977 }
3978
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003979 ++vcpu->stat.nmi_injections;
3980 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003981
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003982 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003983 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003984 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003985 return;
3986 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08003987
Sheng Yangf08864b2008-05-15 18:23:25 +08003988 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3989 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003990
3991 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003992}
3993
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003994bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003995{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02003996 struct vcpu_vmx *vmx = to_vmx(vcpu);
3997 bool masked;
3998
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01003999 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004000 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004001 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004002 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004003 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4004 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4005 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004006}
4007
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004008void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004009{
4010 struct vcpu_vmx *vmx = to_vmx(vcpu);
4011
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004012 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004013 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4014 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4015 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4016 }
4017 } else {
4018 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4019 if (masked)
4020 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4021 GUEST_INTR_STATE_NMI);
4022 else
4023 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4024 GUEST_INTR_STATE_NMI);
4025 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004026}
4027
Jan Kiszka2505dc92013-04-14 12:12:47 +02004028static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4029{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004030 if (to_vmx(vcpu)->nested.nested_run_pending)
4031 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004032
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004033 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004034 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4035 return 0;
4036
Jan Kiszka2505dc92013-04-14 12:12:47 +02004037 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4038 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4039 | GUEST_INTR_STATE_NMI));
4040}
4041
Gleb Natapov78646122009-03-23 12:12:11 +02004042static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4043{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004044 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4045 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004046 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4047 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004048}
4049
Izik Eiduscbc94022007-10-25 00:29:55 +02004050static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4051{
4052 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004053
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004054 if (enable_unrestricted_guest)
4055 return 0;
4056
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004057 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4058 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02004059 if (ret)
4060 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004061 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004062 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004063}
4064
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004065static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4066{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004067 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004068 return 0;
4069}
4070
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004071static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004073 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004074 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004075 /*
4076 * Update instruction length as we may reinject the exception
4077 * from user space while in guest debugging mode.
4078 */
4079 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4080 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004081 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004082 return false;
4083 /* fall through */
4084 case DB_VECTOR:
4085 if (vcpu->guest_debug &
4086 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4087 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004088 /* fall through */
4089 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004090 case OF_VECTOR:
4091 case BR_VECTOR:
4092 case UD_VECTOR:
4093 case DF_VECTOR:
4094 case SS_VECTOR:
4095 case GP_VECTOR:
4096 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004097 return true;
4098 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004099 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004100 return false;
4101}
4102
4103static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4104 int vec, u32 err_code)
4105{
4106 /*
4107 * Instruction with address size override prefix opcode 0x67
4108 * Cause the #SS fault with 0 error code in VM86 mode.
4109 */
4110 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004111 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004112 if (vcpu->arch.halt_request) {
4113 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004114 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004115 }
4116 return 1;
4117 }
4118 return 0;
4119 }
4120
4121 /*
4122 * Forward all other exceptions that are valid in real mode.
4123 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4124 * the required debugging infrastructure rework.
4125 */
4126 kvm_queue_exception(vcpu, vec);
4127 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128}
4129
Andi Kleena0861c02009-06-08 17:37:09 +08004130/*
4131 * Trigger machine check on the host. We assume all the MSRs are already set up
4132 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4133 * We pass a fake environment to the machine check handler because we want
4134 * the guest to be always treated like user space, no matter what context
4135 * it used internally.
4136 */
4137static void kvm_machine_check(void)
4138{
4139#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4140 struct pt_regs regs = {
4141 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4142 .flags = X86_EFLAGS_IF,
4143 };
4144
4145 do_machine_check(&regs, 0);
4146#endif
4147}
4148
Avi Kivity851ba692009-08-24 11:10:17 +03004149static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004150{
4151 /* already handled by vcpu_run */
4152 return 1;
4153}
4154
Avi Kivity851ba692009-08-24 11:10:17 +03004155static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156{
Avi Kivity1155f762007-11-22 11:30:47 +02004157 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004158 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004159 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004160 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 u32 vect_info;
4162 enum emulation_result er;
4163
Avi Kivity1155f762007-11-22 11:30:47 +02004164 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004165 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166
Andi Kleena0861c02009-06-08 17:37:09 +08004167 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004168 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004169
Jim Mattsonef85b672016-12-12 11:01:37 -08004170 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02004171 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004172
Wanpeng Li082d06e2018-04-03 16:28:48 -07004173 if (is_invalid_opcode(intr_info))
4174 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004175
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004177 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004179
Liran Alon9e869482018-03-12 13:12:51 +02004180 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4181 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004182 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02004183 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
4184 if (er == EMULATE_USER_EXIT)
4185 return 0;
4186 else if (er != EMULATE_DONE)
4187 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4188 return 1;
4189 }
4190
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004191 /*
4192 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4193 * MMIO, it is better to report an internal error.
4194 * See the comments in vmx_handle_exit.
4195 */
4196 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4197 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4198 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4199 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004200 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004201 vcpu->run->internal.data[0] = vect_info;
4202 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004203 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004204 return 0;
4205 }
4206
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207 if (is_page_fault(intr_info)) {
4208 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004209 /* EPT won't cause page fault directly */
4210 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02004211 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212 }
4213
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004214 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004215
4216 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4217 return handle_rmode_exception(vcpu, ex_no, error_code);
4218
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004219 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01004220 case AC_VECTOR:
4221 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4222 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004223 case DB_VECTOR:
4224 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4225 if (!(vcpu->guest_debug &
4226 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004227 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004228 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004229 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01004230 skip_emulated_instruction(vcpu);
4231
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004232 kvm_queue_exception(vcpu, DB_VECTOR);
4233 return 1;
4234 }
4235 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4236 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4237 /* fall through */
4238 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004239 /*
4240 * Update instruction length as we may reinject #BP from
4241 * user space while in guest debugging mode. Reading it for
4242 * #DB as well causes no harm, it is not used in that case.
4243 */
4244 vmx->vcpu.arch.event_exit_inst_len =
4245 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004246 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004247 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004248 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4249 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004250 break;
4251 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004252 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4253 kvm_run->ex.exception = ex_no;
4254 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004255 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004256 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004257 return 0;
4258}
4259
Avi Kivity851ba692009-08-24 11:10:17 +03004260static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004262 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263 return 1;
4264}
4265
Avi Kivity851ba692009-08-24 11:10:17 +03004266static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004267{
Avi Kivity851ba692009-08-24 11:10:17 +03004268 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004269 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004270 return 0;
4271}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272
Avi Kivity851ba692009-08-24 11:10:17 +03004273static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004274{
He, Qingbfdaab02007-09-12 14:18:28 +08004275 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004276 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004277 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004278
He, Qingbfdaab02007-09-12 14:18:28 +08004279 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004280 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004281
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004282 ++vcpu->stat.io_exits;
4283
Sean Christopherson432baf62018-03-08 08:57:26 -08004284 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004285 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004286
4287 port = exit_qualification >> 16;
4288 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004289 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004290
Sean Christophersondca7f122018-03-08 08:57:27 -08004291 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004292}
4293
Ingo Molnar102d8322007-02-19 14:37:47 +02004294static void
4295vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4296{
4297 /*
4298 * Patch in the VMCALL instruction:
4299 */
4300 hypercall[0] = 0x0f;
4301 hypercall[1] = 0x01;
4302 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004303}
4304
Guo Chao0fa06072012-06-28 15:16:19 +08004305/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004306static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4307{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004308 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004309 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4310 unsigned long orig_val = val;
4311
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004312 /*
4313 * We get here when L2 changed cr0 in a way that did not change
4314 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004315 * but did change L0 shadowed bits. So we first calculate the
4316 * effective cr0 value that L1 would like to write into the
4317 * hardware. It consists of the L2-owned bits from the new
4318 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004319 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004320 val = (val & ~vmcs12->cr0_guest_host_mask) |
4321 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4322
David Matlack38991522016-11-29 18:14:08 -08004323 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004324 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004325
4326 if (kvm_set_cr0(vcpu, val))
4327 return 1;
4328 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004329 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004330 } else {
4331 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004332 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004333 return 1;
David Matlack38991522016-11-29 18:14:08 -08004334
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004335 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004336 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004337}
4338
4339static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4340{
4341 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004342 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4343 unsigned long orig_val = val;
4344
4345 /* analogously to handle_set_cr0 */
4346 val = (val & ~vmcs12->cr4_guest_host_mask) |
4347 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4348 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004349 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004350 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004351 return 0;
4352 } else
4353 return kvm_set_cr4(vcpu, val);
4354}
4355
Paolo Bonzini0367f202016-07-12 10:44:55 +02004356static int handle_desc(struct kvm_vcpu *vcpu)
4357{
4358 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004359 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004360}
4361
Avi Kivity851ba692009-08-24 11:10:17 +03004362static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004363{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004364 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365 int cr;
4366 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004367 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004368 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004369
He, Qingbfdaab02007-09-12 14:18:28 +08004370 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371 cr = exit_qualification & 15;
4372 reg = (exit_qualification >> 8) & 15;
4373 switch ((exit_qualification >> 4) & 3) {
4374 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004375 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004376 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004377 switch (cr) {
4378 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004379 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004380 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004382 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004383 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004384 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004385 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004386 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004387 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004388 case 8: {
4389 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03004390 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004391 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004392 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004393 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08004394 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004395 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004396 return ret;
4397 /*
4398 * TODO: we might be squashing a
4399 * KVM_GUESTDBG_SINGLESTEP-triggered
4400 * KVM_EXIT_DEBUG here.
4401 */
Avi Kivity851ba692009-08-24 11:10:17 +03004402 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004403 return 0;
4404 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004405 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004407 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004408 WARN_ONCE(1, "Guest should always own CR0.TS");
4409 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02004410 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08004411 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412 case 1: /*mov from cr*/
4413 switch (cr) {
4414 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004415 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02004416 val = kvm_read_cr3(vcpu);
4417 kvm_register_write(vcpu, reg, val);
4418 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004419 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004420 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004421 val = kvm_get_cr8(vcpu);
4422 kvm_register_write(vcpu, reg, val);
4423 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004424 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425 }
4426 break;
4427 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004428 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004429 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004430 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431
Kyle Huey6affcbe2016-11-29 12:40:40 -08004432 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 default:
4434 break;
4435 }
Avi Kivity851ba692009-08-24 11:10:17 +03004436 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004437 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004438 (int)(exit_qualification >> 4) & 3, cr);
4439 return 0;
4440}
4441
Avi Kivity851ba692009-08-24 11:10:17 +03004442static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004443{
He, Qingbfdaab02007-09-12 14:18:28 +08004444 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004445 int dr, dr7, reg;
4446
4447 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4448 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4449
4450 /* First, if DR does not exist, trigger UD */
4451 if (!kvm_require_dr(vcpu, dr))
4452 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453
Jan Kiszkaf2483412010-01-20 18:20:20 +01004454 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004455 if (!kvm_require_cpl(vcpu, 0))
4456 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004457 dr7 = vmcs_readl(GUEST_DR7);
4458 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004459 /*
4460 * As the vm-exit takes precedence over the debug trap, we
4461 * need to emulate the latter, either for the host or the
4462 * guest debugging itself.
4463 */
4464 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03004465 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004466 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02004467 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004468 vcpu->run->debug.arch.exception = DB_VECTOR;
4469 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004470 return 0;
4471 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02004472 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004473 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004474 kvm_queue_exception(vcpu, DB_VECTOR);
4475 return 1;
4476 }
4477 }
4478
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004479 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01004480 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4481 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004482
4483 /*
4484 * No more DR vmexits; force a reload of the debug registers
4485 * and reenter on this instruction. The next vmexit will
4486 * retrieve the full state of the debug registers.
4487 */
4488 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4489 return 1;
4490 }
4491
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004492 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4493 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004494 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004495
4496 if (kvm_get_dr(vcpu, dr, &val))
4497 return 1;
4498 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03004499 } else
Nadav Amit57773922014-06-18 17:19:23 +03004500 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01004501 return 1;
4502
Kyle Huey6affcbe2016-11-29 12:40:40 -08004503 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504}
4505
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004506static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
4507{
4508 return vcpu->arch.dr6;
4509}
4510
4511static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
4512{
4513}
4514
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004515static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
4516{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004517 get_debugreg(vcpu->arch.db[0], 0);
4518 get_debugreg(vcpu->arch.db[1], 1);
4519 get_debugreg(vcpu->arch.db[2], 2);
4520 get_debugreg(vcpu->arch.db[3], 3);
4521 get_debugreg(vcpu->arch.dr6, 6);
4522 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
4523
4524 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01004525 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01004526}
4527
Gleb Natapov020df072010-04-13 10:05:23 +03004528static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4529{
4530 vmcs_writel(GUEST_DR7, val);
4531}
4532
Avi Kivity851ba692009-08-24 11:10:17 +03004533static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004534{
Kyle Huey6a908b62016-11-29 12:40:37 -08004535 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004536}
4537
Avi Kivity851ba692009-08-24 11:10:17 +03004538static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004540 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004541 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004542
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004543 msr_info.index = ecx;
4544 msr_info.host_initiated = false;
4545 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004546 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004547 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548 return 1;
4549 }
4550
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004551 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004552
Avi Kivity6aa8b732006-12-10 02:21:36 -08004553 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004554 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
4555 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
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_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004560{
Will Auld8fe8ab42012-11-29 12:42:12 -08004561 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004562 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4563 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4564 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565
Will Auld8fe8ab42012-11-29 12:42:12 -08004566 msr.data = data;
4567 msr.index = ecx;
4568 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004569 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02004570 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004571 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004572 return 1;
4573 }
4574
Avi Kivity59200272010-01-25 19:47:02 +02004575 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004576 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577}
4578
Avi Kivity851ba692009-08-24 11:10:17 +03004579static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004580{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01004581 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004582 return 1;
4583}
4584
Avi Kivity851ba692009-08-24 11:10:17 +03004585static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586{
Paolo Bonzini47c01522016-12-19 11:44:07 +01004587 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4588 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004589
Avi Kivity3842d132010-07-27 12:30:24 +03004590 kvm_make_request(KVM_REQ_EVENT, vcpu);
4591
Jan Kiszkaa26bf122008-09-26 09:30:45 +02004592 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004593 return 1;
4594}
4595
Avi Kivity851ba692009-08-24 11:10:17 +03004596static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004597{
Avi Kivityd3bef152007-06-05 15:53:05 +03004598 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004599}
4600
Avi Kivity851ba692009-08-24 11:10:17 +03004601static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02004602{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03004603 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02004604}
4605
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004606static int handle_invd(struct kvm_vcpu *vcpu)
4607{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004608 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004609}
4610
Avi Kivity851ba692009-08-24 11:10:17 +03004611static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03004612{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004613 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004614
4615 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004616 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004617}
4618
Avi Kivityfee84b02011-11-10 14:57:25 +02004619static int handle_rdpmc(struct kvm_vcpu *vcpu)
4620{
4621 int err;
4622
4623 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004624 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02004625}
4626
Avi Kivity851ba692009-08-24 11:10:17 +03004627static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02004628{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004629 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02004630}
4631
Dexuan Cui2acf9232010-06-10 11:27:12 +08004632static int handle_xsetbv(struct kvm_vcpu *vcpu)
4633{
4634 u64 new_bv = kvm_read_edx_eax(vcpu);
4635 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4636
4637 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004638 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08004639 return 1;
4640}
4641
Wanpeng Lif53cd632014-12-02 19:14:58 +08004642static int handle_xsaves(struct kvm_vcpu *vcpu)
4643{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004644 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08004645 WARN(1, "this should never happen\n");
4646 return 1;
4647}
4648
4649static int handle_xrstors(struct kvm_vcpu *vcpu)
4650{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004651 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08004652 WARN(1, "this should never happen\n");
4653 return 1;
4654}
4655
Avi Kivity851ba692009-08-24 11:10:17 +03004656static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004657{
Kevin Tian58fbbf22011-08-30 13:56:17 +03004658 if (likely(fasteoi)) {
4659 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4660 int access_type, offset;
4661
4662 access_type = exit_qualification & APIC_ACCESS_TYPE;
4663 offset = exit_qualification & APIC_ACCESS_OFFSET;
4664 /*
4665 * Sane guest uses MOV to write EOI, with written value
4666 * not cared. So make a short-circuit here by avoiding
4667 * heavy instruction emulation.
4668 */
4669 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4670 (offset == APIC_EOI)) {
4671 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004672 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03004673 }
4674 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004675 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004676}
4677
Yang Zhangc7c9c562013-01-25 10:18:51 +08004678static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
4679{
4680 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4681 int vector = exit_qualification & 0xff;
4682
4683 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
4684 kvm_apic_set_eoi_accelerated(vcpu, vector);
4685 return 1;
4686}
4687
Yang Zhang83d4c282013-01-25 10:18:49 +08004688static int handle_apic_write(struct kvm_vcpu *vcpu)
4689{
4690 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4691 u32 offset = exit_qualification & 0xfff;
4692
4693 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
4694 kvm_apic_write_nodecode(vcpu, offset);
4695 return 1;
4696}
4697
Avi Kivity851ba692009-08-24 11:10:17 +03004698static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02004699{
Jan Kiszka60637aa2008-09-26 09:30:47 +02004700 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004701 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02004702 bool has_error_code = false;
4703 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004704 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004705 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004706
4707 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004708 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004709 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02004710
4711 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4712
4713 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004714 if (reason == TASK_SWITCH_GATE && idt_v) {
4715 switch (type) {
4716 case INTR_TYPE_NMI_INTR:
4717 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02004718 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004719 break;
4720 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004721 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004722 kvm_clear_interrupt_queue(vcpu);
4723 break;
4724 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02004725 if (vmx->idt_vectoring_info &
4726 VECTORING_INFO_DELIVER_CODE_MASK) {
4727 has_error_code = true;
4728 error_code =
4729 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4730 }
4731 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004732 case INTR_TYPE_SOFT_EXCEPTION:
4733 kvm_clear_exception_queue(vcpu);
4734 break;
4735 default:
4736 break;
4737 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02004738 }
Izik Eidus37817f22008-03-24 23:14:53 +02004739 tss_selector = exit_qualification;
4740
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004741 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
4742 type != INTR_TYPE_EXT_INTR &&
4743 type != INTR_TYPE_NMI_INTR))
4744 skip_emulated_instruction(vcpu);
4745
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004746 if (kvm_task_switch(vcpu, tss_selector,
4747 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
4748 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03004749 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4750 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4751 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004752 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03004753 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004754
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004755 /*
4756 * TODO: What about debug traps on tss switch?
4757 * Are we supposed to inject them and update dr6?
4758 */
4759
4760 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02004761}
4762
Avi Kivity851ba692009-08-24 11:10:17 +03004763static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08004764{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004765 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08004766 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01004767 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08004768
Sheng Yangf9c617f2009-03-25 10:08:52 +08004769 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08004770
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03004771 /*
4772 * EPT violation happened while executing iret from NMI,
4773 * "blocked by NMI" bit has to be set before next VM entry.
4774 * There are errata that may cause this bit to not be set:
4775 * AAK134, BY25.
4776 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03004777 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004778 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03004779 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03004780 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
4781
Sheng Yang14394422008-04-28 12:24:45 +08004782 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004783 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004784
Junaid Shahid27959a42016-12-06 16:46:10 -08004785 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004786 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08004787 ? PFERR_USER_MASK : 0;
4788 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004789 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08004790 ? PFERR_WRITE_MASK : 0;
4791 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08004792 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08004793 ? PFERR_FETCH_MASK : 0;
4794 /* ept page table entry is present? */
4795 error_code |= (exit_qualification &
4796 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
4797 EPT_VIOLATION_EXECUTABLE))
4798 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004799
Paolo Bonzinieebed242016-11-28 14:39:58 +01004800 error_code |= (exit_qualification & 0x100) != 0 ?
4801 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03004802
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004803 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08004804 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08004805}
4806
Avi Kivity851ba692009-08-24 11:10:17 +03004807static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004808{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004809 gpa_t gpa;
4810
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02004811 /*
4812 * A nested guest cannot optimize MMIO vmexits, because we have an
4813 * nGPA here instead of the required GPA.
4814 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004815 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02004816 if (!is_guest_mode(vcpu) &&
4817 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08004818 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01004819 /*
4820 * Doing kvm_skip_emulated_instruction() depends on undefined
4821 * behavior: Intel's manual doesn't mandate
4822 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
4823 * occurs and while on real hardware it was observed to be set,
4824 * other hypervisors (namely Hyper-V) don't set it, we end up
4825 * advancing IP with some random value. Disable fast mmio when
4826 * running nested and keep it for real hardware in hope that
4827 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
4828 */
4829 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
4830 return kvm_skip_emulated_instruction(vcpu);
4831 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004832 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07004833 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03004834 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004835
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07004836 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004837}
4838
Avi Kivity851ba692009-08-24 11:10:17 +03004839static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08004840{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004841 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01004842 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
4843 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08004844 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03004845 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004846
4847 return 1;
4848}
4849
Mohammed Gamal80ced182009-09-01 12:48:18 +02004850static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004851{
Avi Kivity8b3079a2009-01-05 12:10:54 +02004852 struct vcpu_vmx *vmx = to_vmx(vcpu);
4853 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004854 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02004855 u32 cpu_exec_ctrl;
4856 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03004857 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02004858
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07004859 /*
4860 * We should never reach the point where we are emulating L2
4861 * due to invalid guest state as that means we incorrectly
4862 * allowed a nested VMEntry with an invalid vmcs12.
4863 */
4864 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
4865
Avi Kivity49e9d552010-09-19 14:34:08 +02004866 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4867 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004868
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004869 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03004870 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02004871 return handle_interrupt_window(&vmx->vcpu);
4872
Radim Krčmář72875d82017-04-26 22:32:19 +02004873 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03004874 return 1;
4875
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004876 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004877
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02004878 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02004879 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004880 ret = 0;
4881 goto out;
4882 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01004883
Sean Christophersonadd5ff72018-03-23 09:34:00 -07004884 if (err != EMULATE_DONE)
4885 goto emulation_error;
4886
4887 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
4888 vcpu->arch.exception.pending)
4889 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004890
Gleb Natapov8d76c492013-05-08 18:38:44 +03004891 if (vcpu->arch.halt_request) {
4892 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004893 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03004894 goto out;
4895 }
4896
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004897 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02004898 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004899 if (need_resched())
4900 schedule();
4901 }
4902
Mohammed Gamal80ced182009-09-01 12:48:18 +02004903out:
4904 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004905
Sean Christophersonadd5ff72018-03-23 09:34:00 -07004906emulation_error:
4907 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4908 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4909 vcpu->run->internal.ndata = 0;
4910 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004911}
4912
4913static void grow_ple_window(struct kvm_vcpu *vcpu)
4914{
4915 struct vcpu_vmx *vmx = to_vmx(vcpu);
4916 int old = vmx->ple_window;
4917
Babu Mogerc8e88712018-03-16 16:37:24 -04004918 vmx->ple_window = __grow_ple_window(old, ple_window,
4919 ple_window_grow,
4920 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004921
4922 if (vmx->ple_window != old)
4923 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02004924
4925 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004926}
4927
4928static void shrink_ple_window(struct kvm_vcpu *vcpu)
4929{
4930 struct vcpu_vmx *vmx = to_vmx(vcpu);
4931 int old = vmx->ple_window;
4932
Babu Mogerc8e88712018-03-16 16:37:24 -04004933 vmx->ple_window = __shrink_ple_window(old, ple_window,
4934 ple_window_shrink,
4935 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004936
4937 if (vmx->ple_window != old)
4938 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02004939
4940 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004941}
4942
4943/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08004944 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
4945 */
4946static void wakeup_handler(void)
4947{
4948 struct kvm_vcpu *vcpu;
4949 int cpu = smp_processor_id();
4950
4951 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4952 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
4953 blocked_vcpu_list) {
4954 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
4955
4956 if (pi_test_on(pi_desc) == 1)
4957 kvm_vcpu_kick(vcpu);
4958 }
4959 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
4960}
4961
Peng Haoe01bca22018-04-07 05:47:32 +08004962static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08004963{
4964 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
4965 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
4966 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
4967 0ull, VMX_EPT_EXECUTABLE_MASK,
4968 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004969 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08004970
4971 ept_set_mmio_spte_mask();
4972 kvm_enable_tdp();
4973}
4974
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004976 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
4977 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
4978 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03004979static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004980{
Wanpeng Lib31c1142018-03-12 04:53:04 -07004981 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02004982 grow_ple_window(vcpu);
4983
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004984 /*
4985 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
4986 * VM-execution control is ignored if CPL > 0. OTOH, KVM
4987 * never set PAUSE_EXITING and just set PLE if supported,
4988 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
4989 */
4990 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004991 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004992}
4993
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004994static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08004995{
Kyle Huey6affcbe2016-11-29 12:40:40 -08004996 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08004997}
4998
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004999static int handle_mwait(struct kvm_vcpu *vcpu)
5000{
5001 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5002 return handle_nop(vcpu);
5003}
5004
Jim Mattson45ec3682017-08-23 16:32:04 -07005005static int handle_invalid_op(struct kvm_vcpu *vcpu)
5006{
5007 kvm_queue_exception(vcpu, UD_VECTOR);
5008 return 1;
5009}
5010
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005011static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5012{
5013 return 1;
5014}
5015
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005016static int handle_monitor(struct kvm_vcpu *vcpu)
5017{
5018 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5019 return handle_nop(vcpu);
5020}
5021
Junaid Shahideb4b2482018-06-27 14:59:14 -07005022static int handle_invpcid(struct kvm_vcpu *vcpu)
5023{
5024 u32 vmx_instruction_info;
5025 unsigned long type;
5026 bool pcid_enabled;
5027 gva_t gva;
5028 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005029 unsigned i;
5030 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005031 struct {
5032 u64 pcid;
5033 u64 gla;
5034 } operand;
5035
5036 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5037 kvm_queue_exception(vcpu, UD_VECTOR);
5038 return 1;
5039 }
5040
5041 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5042 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5043
5044 if (type > 3) {
5045 kvm_inject_gp(vcpu, 0);
5046 return 1;
5047 }
5048
5049 /* According to the Intel instruction reference, the memory operand
5050 * is read even if it isn't needed (e.g., for type==all)
5051 */
5052 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5053 vmx_instruction_info, false, &gva))
5054 return 1;
5055
5056 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
5057 kvm_inject_page_fault(vcpu, &e);
5058 return 1;
5059 }
5060
5061 if (operand.pcid >> 12 != 0) {
5062 kvm_inject_gp(vcpu, 0);
5063 return 1;
5064 }
5065
5066 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
5067
5068 switch (type) {
5069 case INVPCID_TYPE_INDIV_ADDR:
5070 if ((!pcid_enabled && (operand.pcid != 0)) ||
5071 is_noncanonical_address(operand.gla, vcpu)) {
5072 kvm_inject_gp(vcpu, 0);
5073 return 1;
5074 }
5075 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
5076 return kvm_skip_emulated_instruction(vcpu);
5077
5078 case INVPCID_TYPE_SINGLE_CTXT:
5079 if (!pcid_enabled && (operand.pcid != 0)) {
5080 kvm_inject_gp(vcpu, 0);
5081 return 1;
5082 }
5083
5084 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
5085 kvm_mmu_sync_roots(vcpu);
5086 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
5087 }
5088
Junaid Shahidb94742c2018-06-27 14:59:20 -07005089 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005090 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07005091 == operand.pcid)
5092 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07005093
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02005094 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005095 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005096 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07005097 * given PCID, then nothing needs to be done here because a
5098 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005099 */
5100
5101 return kvm_skip_emulated_instruction(vcpu);
5102
5103 case INVPCID_TYPE_ALL_NON_GLOBAL:
5104 /*
5105 * Currently, KVM doesn't mark global entries in the shadow
5106 * page tables, so a non-global flush just degenerates to a
5107 * global flush. If needed, we could optimize this later by
5108 * keeping track of global entries in shadow page tables.
5109 */
5110
5111 /* fall-through */
5112 case INVPCID_TYPE_ALL_INCL_GLOBAL:
5113 kvm_mmu_unload(vcpu);
5114 return kvm_skip_emulated_instruction(vcpu);
5115
5116 default:
5117 BUG(); /* We have already checked above that type <= 3 */
5118 }
5119}
5120
Kai Huang843e4332015-01-28 10:54:28 +08005121static int handle_pml_full(struct kvm_vcpu *vcpu)
5122{
5123 unsigned long exit_qualification;
5124
5125 trace_kvm_pml_full(vcpu->vcpu_id);
5126
5127 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5128
5129 /*
5130 * PML buffer FULL happened while executing iret from NMI,
5131 * "blocked by NMI" bit has to be set before next VM entry.
5132 */
5133 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005134 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005135 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5136 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5137 GUEST_INTR_STATE_NMI);
5138
5139 /*
5140 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5141 * here.., and there's no userspace involvement needed for PML.
5142 */
5143 return 1;
5144}
5145
Yunhong Jiang64672c92016-06-13 14:19:59 -07005146static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5147{
Sean Christophersond264ee02018-08-27 15:21:12 -07005148 if (!to_vmx(vcpu)->req_immediate_exit)
5149 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005150 return 1;
5151}
5152
Sean Christophersone4027cf2018-12-03 13:53:12 -08005153/*
5154 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5155 * are overwritten by nested_vmx_setup() when nested=1.
5156 */
5157static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5158{
5159 kvm_queue_exception(vcpu, UD_VECTOR);
5160 return 1;
5161}
5162
Sean Christopherson0b665d32018-08-14 09:33:34 -07005163static int handle_encls(struct kvm_vcpu *vcpu)
5164{
5165 /*
5166 * SGX virtualization is not yet supported. There is no software
5167 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5168 * to prevent the guest from executing ENCLS.
5169 */
5170 kvm_queue_exception(vcpu, UD_VECTOR);
5171 return 1;
5172}
5173
Nadav Har'El0140cae2011-05-25 23:06:28 +03005174/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005175 * The exit handlers return 1 if the exit was handled fully and guest execution
5176 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5177 * to be done to userspace and return 0.
5178 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005179static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005180 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5181 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005182 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005183 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005184 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005185 [EXIT_REASON_CR_ACCESS] = handle_cr,
5186 [EXIT_REASON_DR_ACCESS] = handle_dr,
5187 [EXIT_REASON_CPUID] = handle_cpuid,
5188 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5189 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5190 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5191 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005192 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005193 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005194 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005195 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005196 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5197 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5198 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5199 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5200 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5201 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5202 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5203 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5204 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005205 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5206 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005207 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005208 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005209 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005210 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005211 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005212 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005213 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5214 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005215 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5216 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005217 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005218 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005219 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005220 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005221 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5222 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005223 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005224 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08005225 [EXIT_REASON_XSAVES] = handle_xsaves,
5226 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08005227 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005228 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005229 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005230 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005231 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005232};
5233
5234static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005235 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005236
Avi Kivity586f9602010-11-18 13:09:54 +02005237static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5238{
5239 *info1 = vmcs_readl(EXIT_QUALIFICATION);
5240 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
5241}
5242
Kai Huanga3eaa862015-11-04 13:46:05 +08005243static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005244{
Kai Huanga3eaa862015-11-04 13:46:05 +08005245 if (vmx->pml_pg) {
5246 __free_page(vmx->pml_pg);
5247 vmx->pml_pg = NULL;
5248 }
Kai Huang843e4332015-01-28 10:54:28 +08005249}
5250
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005251static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005252{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005253 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005254 u64 *pml_buf;
5255 u16 pml_idx;
5256
5257 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5258
5259 /* Do nothing if PML buffer is empty */
5260 if (pml_idx == (PML_ENTITY_NUM - 1))
5261 return;
5262
5263 /* PML index always points to next available PML buffer entity */
5264 if (pml_idx >= PML_ENTITY_NUM)
5265 pml_idx = 0;
5266 else
5267 pml_idx++;
5268
5269 pml_buf = page_address(vmx->pml_pg);
5270 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5271 u64 gpa;
5272
5273 gpa = pml_buf[pml_idx];
5274 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005275 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005276 }
5277
5278 /* reset PML index */
5279 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5280}
5281
5282/*
5283 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5284 * Called before reporting dirty_bitmap to userspace.
5285 */
5286static void kvm_flush_pml_buffers(struct kvm *kvm)
5287{
5288 int i;
5289 struct kvm_vcpu *vcpu;
5290 /*
5291 * We only need to kick vcpu out of guest mode here, as PML buffer
5292 * is flushed at beginning of all VMEXITs, and it's obvious that only
5293 * vcpus running in guest are possible to have unflushed GPAs in PML
5294 * buffer.
5295 */
5296 kvm_for_each_vcpu(i, vcpu, kvm)
5297 kvm_vcpu_kick(vcpu);
5298}
5299
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005300static void vmx_dump_sel(char *name, uint32_t sel)
5301{
5302 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005303 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005304 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5305 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5306 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5307}
5308
5309static void vmx_dump_dtsel(char *name, uint32_t limit)
5310{
5311 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5312 name, vmcs_read32(limit),
5313 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5314}
5315
5316static void dump_vmcs(void)
5317{
5318 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5319 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5320 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5321 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5322 u32 secondary_exec_control = 0;
5323 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005324 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005325 int i, n;
5326
5327 if (cpu_has_secondary_exec_ctrls())
5328 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5329
5330 pr_err("*** Guest State ***\n");
5331 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5332 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5333 vmcs_readl(CR0_GUEST_HOST_MASK));
5334 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5335 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5336 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5337 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5338 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5339 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005340 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5341 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5342 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5343 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005344 }
5345 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5346 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5347 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5348 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5349 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5350 vmcs_readl(GUEST_SYSENTER_ESP),
5351 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5352 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5353 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5354 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5355 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5356 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5357 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5358 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5359 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5360 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5361 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5362 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5363 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005364 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5365 efer, vmcs_read64(GUEST_IA32_PAT));
5366 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5367 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005368 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005369 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005370 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005371 pr_err("PerfGlobCtl = 0x%016llx\n",
5372 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005373 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005374 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005375 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5376 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5377 vmcs_read32(GUEST_ACTIVITY_STATE));
5378 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5379 pr_err("InterruptStatus = %04x\n",
5380 vmcs_read16(GUEST_INTR_STATUS));
5381
5382 pr_err("*** Host State ***\n");
5383 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5384 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5385 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5386 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5387 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5388 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5389 vmcs_read16(HOST_TR_SELECTOR));
5390 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5391 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5392 vmcs_readl(HOST_TR_BASE));
5393 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5394 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5395 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5396 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5397 vmcs_readl(HOST_CR4));
5398 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5399 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5400 vmcs_read32(HOST_IA32_SYSENTER_CS),
5401 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5402 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005403 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5404 vmcs_read64(HOST_IA32_EFER),
5405 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005406 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005407 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005408 pr_err("PerfGlobCtl = 0x%016llx\n",
5409 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005410
5411 pr_err("*** Control State ***\n");
5412 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5413 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5414 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5415 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5416 vmcs_read32(EXCEPTION_BITMAP),
5417 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5418 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5419 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5420 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5421 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5422 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5423 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5424 vmcs_read32(VM_EXIT_INTR_INFO),
5425 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5426 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5427 pr_err(" reason=%08x qualification=%016lx\n",
5428 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5429 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5430 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5431 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005432 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005433 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005434 pr_err("TSC Multiplier = 0x%016llx\n",
5435 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005436 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
5437 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
5438 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5439 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5440 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005441 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005442 n = vmcs_read32(CR3_TARGET_COUNT);
5443 for (i = 0; i + 1 < n; i += 4)
5444 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
5445 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
5446 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
5447 if (i < n)
5448 pr_err("CR3 target%u=%016lx\n",
5449 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
5450 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5451 pr_err("PLE Gap=%08x Window=%08x\n",
5452 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5453 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5454 pr_err("Virtual processor ID = 0x%04x\n",
5455 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5456}
5457
Avi Kivity6aa8b732006-12-10 02:21:36 -08005458/*
5459 * The guest has exited. See if we can fix it or if we need userspace
5460 * assistance.
5461 */
Avi Kivity851ba692009-08-24 11:10:17 +03005462static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005463{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005464 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005465 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005466 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005467
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01005468 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
5469
Kai Huang843e4332015-01-28 10:54:28 +08005470 /*
5471 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5472 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5473 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5474 * mode as if vcpus is in root mode, the PML buffer must has been
5475 * flushed already.
5476 */
5477 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005478 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005479
Mohammed Gamal80ced182009-09-01 12:48:18 +02005480 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005481 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005482 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005483
Paolo Bonzini7313c692017-07-27 10:31:25 +02005484 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
5485 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03005486
Mohammed Gamal51207022010-05-31 22:40:54 +03005487 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005488 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03005489 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5490 vcpu->run->fail_entry.hardware_entry_failure_reason
5491 = exit_reason;
5492 return 0;
5493 }
5494
Avi Kivity29bd8a72007-09-10 17:27:03 +03005495 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005496 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5497 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03005498 = vmcs_read32(VM_INSTRUCTION_ERROR);
5499 return 0;
5500 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005501
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005502 /*
5503 * Note:
5504 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
5505 * delivery event since it indicates guest is accessing MMIO.
5506 * The vm-exit can be triggered again after return to guest that
5507 * will cause infinite loop.
5508 */
Mike Dayd77c26f2007-10-08 09:02:08 -04005509 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08005510 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02005511 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00005512 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005513 exit_reason != EXIT_REASON_TASK_SWITCH)) {
5514 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5515 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005516 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005517 vcpu->run->internal.data[0] = vectoring_info;
5518 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02005519 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
5520 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
5521 vcpu->run->internal.ndata++;
5522 vcpu->run->internal.data[3] =
5523 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5524 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08005525 return 0;
5526 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005527
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005528 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005529 vmx->loaded_vmcs->soft_vnmi_blocked)) {
5530 if (vmx_interrupt_allowed(vcpu)) {
5531 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5532 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
5533 vcpu->arch.nmi_pending) {
5534 /*
5535 * This CPU don't support us in finding the end of an
5536 * NMI-blocked window if the guest runs with IRQs
5537 * disabled. So we pull the trigger after 1 s of
5538 * futile waiting, but inform the user about this.
5539 */
5540 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
5541 "state on VCPU %d after 1 s timeout\n",
5542 __func__, vcpu->vcpu_id);
5543 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
5544 }
5545 }
5546
Avi Kivity6aa8b732006-12-10 02:21:36 -08005547 if (exit_reason < kvm_vmx_max_exit_handlers
5548 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03005549 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01005551 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
5552 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03005553 kvm_queue_exception(vcpu, UD_VECTOR);
5554 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556}
5557
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005558/*
5559 * Software based L1D cache flush which is used when microcode providing
5560 * the cache control MSR is not loaded.
5561 *
5562 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
5563 * flush it is required to read in 64 KiB because the replacement algorithm
5564 * is not exactly LRU. This could be sized at runtime via topology
5565 * information but as all relevant affected CPUs have 32KiB L1D cache size
5566 * there is no point in doing so.
5567 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005568static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005569{
5570 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005571
5572 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02005573 * This code is only executed when the the flush mode is 'cond' or
5574 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005575 */
Nicolai Stange427362a2018-07-21 22:25:00 +02005576 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02005577 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005578
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005579 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02005580 * Clear the per-vcpu flush bit, it gets set again
5581 * either from vcpu_run() or from one of the unsafe
5582 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005583 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02005584 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02005585 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02005586
5587 /*
5588 * Clear the per-cpu flush bit, it gets set again from
5589 * the interrupt handlers.
5590 */
5591 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
5592 kvm_clear_cpu_l1tf_flush_l1d();
5593
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02005594 if (!flush_l1d)
5595 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02005596 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02005597
5598 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005599
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02005600 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
5601 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
5602 return;
5603 }
5604
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005605 asm volatile(
5606 /* First ensure the pages are in the TLB */
5607 "xorl %%eax, %%eax\n"
5608 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02005609 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005610 "addl $4096, %%eax\n\t"
5611 "cmpl %%eax, %[size]\n\t"
5612 "jne .Lpopulate_tlb\n\t"
5613 "xorl %%eax, %%eax\n\t"
5614 "cpuid\n\t"
5615 /* Now fill the cache */
5616 "xorl %%eax, %%eax\n"
5617 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02005618 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005619 "addl $64, %%eax\n\t"
5620 "cmpl %%eax, %[size]\n\t"
5621 "jne .Lfill_cache\n\t"
5622 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02005623 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02005624 [size] "r" (size)
5625 : "eax", "ebx", "ecx", "edx");
5626}
5627
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005628static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005629{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08005630 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5631
5632 if (is_guest_mode(vcpu) &&
5633 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
5634 return;
5635
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005636 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005637 vmcs_write32(TPR_THRESHOLD, 0);
5638 return;
5639 }
5640
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005641 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005642}
5643
Sean Christopherson97b7ead2018-12-03 13:53:16 -08005644void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005645{
5646 u32 sec_exec_control;
5647
Jim Mattson8d860bb2018-05-09 16:56:05 -04005648 if (!lapic_in_kernel(vcpu))
5649 return;
5650
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07005651 if (!flexpriority_enabled &&
5652 !cpu_has_vmx_virtualize_x2apic_mode())
5653 return;
5654
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02005655 /* Postpone execution until vmcs01 is the current VMCS. */
5656 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04005657 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02005658 return;
5659 }
5660
Yang Zhang8d146952013-01-25 10:18:50 +08005661 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04005662 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
5663 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08005664
Jim Mattson8d860bb2018-05-09 16:56:05 -04005665 switch (kvm_get_apic_mode(vcpu)) {
5666 case LAPIC_MODE_INVALID:
5667 WARN_ONCE(true, "Invalid local APIC state");
5668 case LAPIC_MODE_DISABLED:
5669 break;
5670 case LAPIC_MODE_XAPIC:
5671 if (flexpriority_enabled) {
5672 sec_exec_control |=
5673 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5674 vmx_flush_tlb(vcpu, true);
5675 }
5676 break;
5677 case LAPIC_MODE_X2APIC:
5678 if (cpu_has_vmx_virtualize_x2apic_mode())
5679 sec_exec_control |=
5680 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
5681 break;
Yang Zhang8d146952013-01-25 10:18:50 +08005682 }
5683 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
5684
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005685 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08005686}
5687
Tang Chen38b99172014-09-24 15:57:54 +08005688static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
5689{
Jim Mattsonab5df312018-05-09 17:02:03 -04005690 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08005691 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07005692 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07005693 }
Tang Chen38b99172014-09-24 15:57:54 +08005694}
5695
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005696static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005697{
5698 u16 status;
5699 u8 old;
5700
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005701 if (max_isr == -1)
5702 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08005703
5704 status = vmcs_read16(GUEST_INTR_STATUS);
5705 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005706 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005707 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005708 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08005709 vmcs_write16(GUEST_INTR_STATUS, status);
5710 }
5711}
5712
5713static void vmx_set_rvi(int vector)
5714{
5715 u16 status;
5716 u8 old;
5717
Wei Wang4114c272014-11-05 10:53:43 +08005718 if (vector == -1)
5719 vector = 0;
5720
Yang Zhangc7c9c562013-01-25 10:18:51 +08005721 status = vmcs_read16(GUEST_INTR_STATUS);
5722 old = (u8)status & 0xff;
5723 if ((u8)vector != old) {
5724 status &= ~0xff;
5725 status |= (u8)vector;
5726 vmcs_write16(GUEST_INTR_STATUS, status);
5727 }
5728}
5729
5730static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5731{
Liran Alon851c1a182017-12-24 18:12:56 +02005732 /*
5733 * When running L2, updating RVI is only relevant when
5734 * vmcs12 virtual-interrupt-delivery enabled.
5735 * However, it can be enabled only when L1 also
5736 * intercepts external-interrupts and in that case
5737 * we should not update vmcs02 RVI but instead intercept
5738 * interrupt. Therefore, do nothing when running L2.
5739 */
5740 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08005741 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005742}
5743
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005744static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005745{
5746 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005747 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02005748 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005749
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005750 WARN_ON(!vcpu->arch.apicv_active);
5751 if (pi_test_on(&vmx->pi_desc)) {
5752 pi_clear_on(&vmx->pi_desc);
5753 /*
5754 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
5755 * But on x86 this is just a compiler barrier anyway.
5756 */
5757 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02005758 max_irr_updated =
5759 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
5760
5761 /*
5762 * If we are running L2 and L1 has a new pending interrupt
5763 * which can be injected, we should re-evaluate
5764 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02005765 * If L1 intercepts external-interrupts, we should
5766 * exit from L2 to L1. Otherwise, interrupt should be
5767 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02005768 */
Liran Alon851c1a182017-12-24 18:12:56 +02005769 if (is_guest_mode(vcpu) && max_irr_updated) {
5770 if (nested_exit_on_intr(vcpu))
5771 kvm_vcpu_exiting_guest_mode(vcpu);
5772 else
5773 kvm_make_request(KVM_REQ_EVENT, vcpu);
5774 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01005775 } else {
5776 max_irr = kvm_lapic_find_highest_irr(vcpu);
5777 }
5778 vmx_hwapic_irr_update(vcpu, max_irr);
5779 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01005780}
5781
Andrey Smetanin63086302015-11-10 15:36:32 +03005782static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005783{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005784 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08005785 return;
5786
Yang Zhangc7c9c562013-01-25 10:18:51 +08005787 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
5788 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
5789 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
5790 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
5791}
5792
Paolo Bonzini967235d2016-12-19 14:03:45 +01005793static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
5794{
5795 struct vcpu_vmx *vmx = to_vmx(vcpu);
5796
5797 pi_clear_on(&vmx->pi_desc);
5798 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
5799}
5800
Avi Kivity51aa01d2010-07-20 14:31:20 +03005801static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03005802{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005803 u32 exit_intr_info = 0;
5804 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02005805
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005806 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
5807 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02005808 return;
5809
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005810 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
5811 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5812 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08005813
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005814 /* if exit due to PF check for async PF */
5815 if (is_page_fault(exit_intr_info))
5816 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
5817
Andi Kleena0861c02009-06-08 17:37:09 +08005818 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07005819 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
5820 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08005821 kvm_machine_check();
5822
Gleb Natapov20f65982009-05-11 13:35:55 +03005823 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08005824 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07005825 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03005826 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07005827 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005828 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03005829}
Gleb Natapov20f65982009-05-11 13:35:55 +03005830
Yang Zhanga547c6d2013-04-11 19:25:10 +08005831static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
5832{
5833 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5834
Yang Zhanga547c6d2013-04-11 19:25:10 +08005835 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
5836 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
5837 unsigned int vector;
5838 unsigned long entry;
5839 gate_desc *desc;
5840 struct vcpu_vmx *vmx = to_vmx(vcpu);
5841#ifdef CONFIG_X86_64
5842 unsigned long tmp;
5843#endif
5844
5845 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
5846 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02005847 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08005848 asm volatile(
5849#ifdef CONFIG_X86_64
5850 "mov %%" _ASM_SP ", %[sp]\n\t"
5851 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
5852 "push $%c[ss]\n\t"
5853 "push %[sp]\n\t"
5854#endif
5855 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08005856 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01005857 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08005858 :
5859#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06005860 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08005861#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05005862 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08005863 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01005864 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08005865 [ss]"i"(__KERNEL_DS),
5866 [cs]"i"(__KERNEL_CS)
5867 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005868 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08005869}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005870STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08005871
Tom Lendackybc226f02018-05-10 22:06:39 +02005872static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005873{
Tom Lendackybc226f02018-05-10 22:06:39 +02005874 switch (index) {
5875 case MSR_IA32_SMBASE:
5876 /*
5877 * We cannot do SMM unless we can run the guest in big
5878 * real mode.
5879 */
5880 return enable_unrestricted_guest || emulate_invalid_guest_state;
5881 case MSR_AMD64_VIRT_SPEC_CTRL:
5882 /* This is AMD only. */
5883 return false;
5884 default:
5885 return true;
5886 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005887}
5888
Avi Kivity51aa01d2010-07-20 14:31:20 +03005889static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
5890{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02005891 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03005892 bool unblock_nmi;
5893 u8 vector;
5894 bool idtv_info_valid;
5895
5896 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03005897
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005898 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01005899 if (vmx->loaded_vmcs->nmi_known_unmasked)
5900 return;
5901 /*
5902 * Can't use vmx->exit_intr_info since we're not sure what
5903 * the exit reason is.
5904 */
5905 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5906 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
5907 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
5908 /*
5909 * SDM 3: 27.7.1.2 (September 2008)
5910 * Re-set bit "block by NMI" before VM entry if vmexit caused by
5911 * a guest IRET fault.
5912 * SDM 3: 23.2.2 (September 2008)
5913 * Bit 12 is undefined in any of the following cases:
5914 * If the VM exit sets the valid bit in the IDT-vectoring
5915 * information field.
5916 * If the VM exit is due to a double fault.
5917 */
5918 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
5919 vector != DF_VECTOR && !idtv_info_valid)
5920 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5921 GUEST_INTR_STATE_NMI);
5922 else
5923 vmx->loaded_vmcs->nmi_known_unmasked =
5924 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
5925 & GUEST_INTR_STATE_NMI);
5926 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
5927 vmx->loaded_vmcs->vnmi_blocked_time +=
5928 ktime_to_ns(ktime_sub(ktime_get(),
5929 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03005930}
5931
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005932static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03005933 u32 idt_vectoring_info,
5934 int instr_len_field,
5935 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03005936{
Avi Kivity51aa01d2010-07-20 14:31:20 +03005937 u8 vector;
5938 int type;
5939 bool idtv_info_valid;
5940
5941 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03005942
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005943 vcpu->arch.nmi_injected = false;
5944 kvm_clear_exception_queue(vcpu);
5945 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005946
5947 if (!idtv_info_valid)
5948 return;
5949
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005950 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03005951
Avi Kivity668f6122008-07-02 09:28:55 +03005952 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
5953 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03005954
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005955 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03005956 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005957 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03005958 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03005959 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03005960 * Clear bit "block by NMI" before VM entry if a NMI
5961 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03005962 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005963 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005964 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03005965 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005966 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005967 /* fall through */
5968 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03005969 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03005970 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03005971 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03005972 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03005973 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005974 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005975 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005976 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005977 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03005978 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005979 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03005980 break;
5981 default:
5982 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03005983 }
Avi Kivitycf393f72008-07-01 16:20:21 +03005984}
5985
Avi Kivity83422e12010-07-20 14:43:23 +03005986static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
5987{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005988 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03005989 VM_EXIT_INSTRUCTION_LEN,
5990 IDT_VECTORING_ERROR_CODE);
5991}
5992
Avi Kivityb463a6f2010-07-20 15:06:17 +03005993static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
5994{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01005995 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005996 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5997 VM_ENTRY_INSTRUCTION_LEN,
5998 VM_ENTRY_EXCEPTION_ERROR_CODE);
5999
6000 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6001}
6002
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006003static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6004{
6005 int i, nr_msrs;
6006 struct perf_guest_switch_msr *msrs;
6007
6008 msrs = perf_guest_get_msrs(&nr_msrs);
6009
6010 if (!msrs)
6011 return;
6012
6013 for (i = 0; i < nr_msrs; i++)
6014 if (msrs[i].host == msrs[i].guest)
6015 clear_atomic_switch_msr(vmx, msrs[i].msr);
6016 else
6017 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006018 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006019}
6020
Sean Christophersonf459a702018-08-27 15:21:11 -07006021static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
6022{
6023 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
6024 if (!vmx->loaded_vmcs->hv_timer_armed)
6025 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
6026 PIN_BASED_VMX_PREEMPTION_TIMER);
6027 vmx->loaded_vmcs->hv_timer_armed = true;
6028}
6029
6030static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006031{
6032 struct vcpu_vmx *vmx = to_vmx(vcpu);
6033 u64 tscl;
6034 u32 delta_tsc;
6035
Sean Christophersond264ee02018-08-27 15:21:12 -07006036 if (vmx->req_immediate_exit) {
6037 vmx_arm_hv_timer(vmx, 0);
6038 return;
6039 }
6040
Sean Christophersonf459a702018-08-27 15:21:11 -07006041 if (vmx->hv_deadline_tsc != -1) {
6042 tscl = rdtsc();
6043 if (vmx->hv_deadline_tsc > tscl)
6044 /* set_hv_timer ensures the delta fits in 32-bits */
6045 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6046 cpu_preemption_timer_multi);
6047 else
6048 delta_tsc = 0;
6049
6050 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -07006051 return;
Sean Christophersonf459a702018-08-27 15:21:11 -07006052 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006053
Sean Christophersonf459a702018-08-27 15:21:11 -07006054 if (vmx->loaded_vmcs->hv_timer_armed)
6055 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
6056 PIN_BASED_VMX_PREEMPTION_TIMER);
6057 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006058}
6059
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08006060static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006061{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006062 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006063 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02006064
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006065 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006066 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006067 vmx->loaded_vmcs->soft_vnmi_blocked))
6068 vmx->loaded_vmcs->entry_time = ktime_get();
6069
Avi Kivity104f2262010-11-18 13:12:52 +02006070 /* Don't enter VMX if guest state is invalid, let the exit handler
6071 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02006072 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02006073 return;
6074
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006075 if (vmx->ple_window_dirty) {
6076 vmx->ple_window_dirty = false;
6077 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6078 }
6079
Sean Christophersonff241482018-12-03 13:53:14 -08006080 if (vmx->nested.need_vmcs12_sync)
6081 nested_sync_from_vmcs12(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03006082
Avi Kivity104f2262010-11-18 13:12:52 +02006083 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6084 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6085 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6086 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6087
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006088 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -07006089 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006090 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -07006091 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006092 }
6093
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006094 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -07006095 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006096 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -07006097 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006098 }
6099
Avi Kivity104f2262010-11-18 13:12:52 +02006100 /* When single-stepping over STI and MOV SS, we must clear the
6101 * corresponding interruptibility bits in the guest state. Otherwise
6102 * vmentry fails as it then expects bit 14 (BS) in pending debug
6103 * exceptions being set, but that's not correct for the guest debugging
6104 * case. */
6105 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6106 vmx_set_interrupt_shadow(vcpu, 0);
6107
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02006108 if (static_cpu_has(X86_FEATURE_PKU) &&
6109 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
6110 vcpu->arch.pkru != vmx->host_pkru)
6111 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006112
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006113 atomic_switch_perf_msrs(vmx);
6114
Sean Christophersonf459a702018-08-27 15:21:11 -07006115 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07006116
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006117 /*
6118 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6119 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6120 * is no need to worry about the conditional branch over the wrmsr
6121 * being speculatively taken.
6122 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006123 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006124
Nadav Har'Eld462b812011-05-24 15:26:10 +03006125 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006126
6127 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
6128 (unsigned long)&current_evmcs->host_rsp : 0;
6129
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006130 if (static_branch_unlikely(&vmx_l1d_should_flush))
6131 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006132
Avi Kivity104f2262010-11-18 13:12:52 +02006133 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08006134 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006135 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6136 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6137 "push %%" _ASM_CX " \n\t"
6138 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006139 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006140 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006141 /* Avoid VMWRITE when Enlightened VMCS is in use */
6142 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
6143 "jz 2f \n\t"
6144 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
6145 "jmp 1f \n\t"
6146 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006147 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006148 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006149 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006150 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6151 "mov %%cr2, %%" _ASM_DX " \n\t"
6152 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006153 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006154 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006155 "3: \n\t"
Uros Bizjak00df9182018-10-23 00:09:11 +02006156 /* Check if vmlaunch or vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02006157 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006158 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006159 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6160 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6161 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6162 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6163 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6164 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006165#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006166 "mov %c[r8](%0), %%r8 \n\t"
6167 "mov %c[r9](%0), %%r9 \n\t"
6168 "mov %c[r10](%0), %%r10 \n\t"
6169 "mov %c[r11](%0), %%r11 \n\t"
6170 "mov %c[r12](%0), %%r12 \n\t"
6171 "mov %c[r13](%0), %%r13 \n\t"
6172 "mov %c[r14](%0), %%r14 \n\t"
6173 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006174#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006175 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03006176
Avi Kivity6aa8b732006-12-10 02:21:36 -08006177 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03006178 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006179 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006180 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +02006181 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006182 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08006183 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006184 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02006185 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08006186 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006187 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6188 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6189 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6190 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6191 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6192 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6193 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006194#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006195 "mov %%r8, %c[r8](%0) \n\t"
6196 "mov %%r9, %c[r9](%0) \n\t"
6197 "mov %%r10, %c[r10](%0) \n\t"
6198 "mov %%r11, %c[r11](%0) \n\t"
6199 "mov %%r12, %c[r12](%0) \n\t"
6200 "mov %%r13, %c[r13](%0) \n\t"
6201 "mov %%r14, %c[r14](%0) \n\t"
6202 "mov %%r15, %c[r15](%0) \n\t"
Uros Bizjak43ce76c2018-10-17 16:46:57 +02006203 /*
6204 * Clear host registers marked as clobbered to prevent
6205 * speculative use.
6206 */
Jim Mattson0cb5b302018-01-03 14:31:38 -08006207 "xor %%r8d, %%r8d \n\t"
6208 "xor %%r9d, %%r9d \n\t"
6209 "xor %%r10d, %%r10d \n\t"
6210 "xor %%r11d, %%r11d \n\t"
6211 "xor %%r12d, %%r12d \n\t"
6212 "xor %%r13d, %%r13d \n\t"
6213 "xor %%r14d, %%r14d \n\t"
6214 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006215#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006216 "mov %%cr2, %%" _ASM_AX " \n\t"
6217 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006218
Jim Mattson0cb5b302018-01-03 14:31:38 -08006219 "xor %%eax, %%eax \n\t"
6220 "xor %%ebx, %%ebx \n\t"
6221 "xor %%esi, %%esi \n\t"
6222 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006223 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006224 ".pushsection .rodata \n\t"
6225 ".global vmx_return \n\t"
6226 "vmx_return: " _ASM_PTR " 2b \n\t"
6227 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006228 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03006229 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02006230 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03006231 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006232 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6233 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6234 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6235 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6236 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6237 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6238 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006239#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006240 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6241 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6242 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6243 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6244 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6245 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6246 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6247 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08006248#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02006249 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6250 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02006251 : "cc", "memory"
6252#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006253 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006254 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006255#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006256 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006257#endif
6258 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08006259
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006260 /*
6261 * We do not use IBRS in the kernel. If this vCPU has used the
6262 * SPEC_CTRL MSR it may have left it on; save the value and
6263 * turn it off. This is much more efficient than blindly adding
6264 * it to the atomic save/restore list. Especially as the former
6265 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6266 *
6267 * For non-nested case:
6268 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6269 * save it.
6270 *
6271 * For nested case:
6272 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6273 * save it.
6274 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006275 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006276 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006277
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006278 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006279
David Woodhouse117cc7a2018-01-12 11:11:27 +00006280 /* Eliminate branch target predictions from guest mode */
6281 vmexit_fill_RSB();
6282
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006283 /* All fields are clean at this point */
6284 if (static_branch_unlikely(&enable_evmcs))
6285 current_evmcs->hv_clean_fields |=
6286 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6287
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006288 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006289 if (vmx->host_debugctlmsr)
6290 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006291
Avi Kivityaa67f602012-08-01 16:48:03 +03006292#ifndef CONFIG_X86_64
6293 /*
6294 * The sysexit path does not restore ds/es, so we must set them to
6295 * a reasonable value ourselves.
6296 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006297 * We can't defer this to vmx_prepare_switch_to_host() since that
6298 * function may be executed in interrupt context, which saves and
6299 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006300 */
6301 loadsegment(ds, __USER_DS);
6302 loadsegment(es, __USER_DS);
6303#endif
6304
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006305 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02006306 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006307 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03006308 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006309 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006310 vcpu->arch.regs_dirty = 0;
6311
Gleb Natapove0b890d2013-09-25 12:51:33 +03006312 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006313 * eager fpu is enabled if PKEY is supported and CR4 is switched
6314 * back on host, so it is safe to read guest PKRU from current
6315 * XSAVE.
6316 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02006317 if (static_cpu_has(X86_FEATURE_PKU) &&
6318 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
6319 vcpu->arch.pkru = __read_pkru();
6320 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006321 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08006322 }
6323
Gleb Natapove0b890d2013-09-25 12:51:33 +03006324 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006325 vmx->idt_vectoring_info = 0;
6326
6327 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
6328 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
6329 return;
6330
6331 vmx->loaded_vmcs->launched = 1;
6332 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006333
Avi Kivity51aa01d2010-07-20 14:31:20 +03006334 vmx_complete_atomic_exit(vmx);
6335 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006336 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006337}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05006338STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339
Sean Christopherson434a1e92018-03-20 12:17:18 -07006340static struct kvm *vmx_vm_alloc(void)
6341{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07006342 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006343 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07006344}
6345
6346static void vmx_vm_free(struct kvm *kvm)
6347{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07006348 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07006349}
6350
Avi Kivity6aa8b732006-12-10 02:21:36 -08006351static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6352{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006353 struct vcpu_vmx *vmx = to_vmx(vcpu);
6354
Kai Huang843e4332015-01-28 10:54:28 +08006355 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006356 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006357 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006358 leave_guest_mode(vcpu);
Sean Christopherson55d23752018-12-03 13:53:18 -08006359 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006360 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006361 kfree(vmx->guest_msrs);
6362 kvm_vcpu_uninit(vcpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08006363 kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
Rusty Russella4770342007-08-01 14:46:11 +10006364 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006365}
6366
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006367static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006368{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006369 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10006370 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006371 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03006372 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006373
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006374 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006375 return ERR_PTR(-ENOMEM);
6376
Marc Orrb666a4b2018-11-06 14:53:56 -08006377 vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, GFP_KERNEL);
6378 if (!vmx->vcpu.arch.guest_fpu) {
6379 printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
6380 err = -ENOMEM;
6381 goto free_partial_vcpu;
6382 }
6383
Wanpeng Li991e7a02015-09-16 17:30:05 +08006384 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08006385
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006386 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6387 if (err)
6388 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006389
Peter Feiner4e595162016-07-07 14:49:58 -07006390 err = -ENOMEM;
6391
6392 /*
6393 * If PML is turned on, failure on enabling PML just results in failure
6394 * of creating the vcpu, therefore we can simplify PML logic (by
6395 * avoiding dealing with cases, such as enabling PML partially on vcpus
6396 * for the guest, etc.
6397 */
6398 if (enable_pml) {
6399 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
6400 if (!vmx->pml_pg)
6401 goto uninit_vcpu;
6402 }
6403
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006404 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02006405 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
6406 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03006407
Peter Feiner4e595162016-07-07 14:49:58 -07006408 if (!vmx->guest_msrs)
6409 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006410
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006411 err = alloc_loaded_vmcs(&vmx->vmcs01);
6412 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006413 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006414
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006415 msr_bitmap = vmx->vmcs01.msr_bitmap;
Jim Mattson788fc1e2018-11-09 09:35:11 -08006416 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006417 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
6418 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
6419 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6420 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6421 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6422 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
6423 vmx->msr_bitmap_mode = 0;
6424
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006425 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006426 cpu = get_cpu();
6427 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10006428 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +02006429 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006430 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006431 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +02006432 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006433 err = alloc_apic_access_page(kvm);
6434 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006435 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006436 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006437
Sean Christophersone90008d2018-03-05 12:04:37 -08006438 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +08006439 err = init_rmode_identity_map(kvm);
6440 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006441 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006442 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006443
Roman Kagan63aff652018-07-19 21:59:07 +03006444 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006445 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
Sean Christopherson7caaa712018-12-03 13:53:01 -08006446 vmx_capability.ept,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006447 kvm_vcpu_apicv_active(&vmx->vcpu));
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006448 else
6449 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006450
Wincy Van705699a2015-02-03 23:58:17 +08006451 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006452 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006453
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006454 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
6455
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02006456 /*
6457 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
6458 * or POSTED_INTR_WAKEUP_VECTOR.
6459 */
6460 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
6461 vmx->pi_desc.sn = 1;
6462
Lan Tianyu53963a72018-12-06 15:34:36 +08006463 vmx->ept_pointer = INVALID_PAGE;
6464
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006465 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006466
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006467free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006468 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006469free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006470 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07006471free_pml:
6472 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006473uninit_vcpu:
6474 kvm_vcpu_uninit(&vmx->vcpu);
6475free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08006476 free_vpid(vmx->vpid);
Marc Orrb666a4b2018-11-06 14:53:56 -08006477 kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
6478free_partial_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10006479 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006480 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006481}
6482
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006483#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"
6484#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 -04006485
Wanpeng Lib31c1142018-03-12 04:53:04 -07006486static int vmx_vm_init(struct kvm *kvm)
6487{
Tianyu Lan877ad952018-07-19 08:40:23 +00006488 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
6489
Wanpeng Lib31c1142018-03-12 04:53:04 -07006490 if (!ple_gap)
6491 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006492
Jiri Kosinad90a7a02018-07-13 16:23:25 +02006493 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
6494 switch (l1tf_mitigation) {
6495 case L1TF_MITIGATION_OFF:
6496 case L1TF_MITIGATION_FLUSH_NOWARN:
6497 /* 'I explicitly don't care' is set */
6498 break;
6499 case L1TF_MITIGATION_FLUSH:
6500 case L1TF_MITIGATION_FLUSH_NOSMT:
6501 case L1TF_MITIGATION_FULL:
6502 /*
6503 * Warn upon starting the first VM in a potentially
6504 * insecure environment.
6505 */
6506 if (cpu_smt_control == CPU_SMT_ENABLED)
6507 pr_warn_once(L1TF_MSG_SMT);
6508 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
6509 pr_warn_once(L1TF_MSG_L1D);
6510 break;
6511 case L1TF_MITIGATION_FULL_FORCE:
6512 /* Flush is enforced */
6513 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006514 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04006515 }
Wanpeng Lib31c1142018-03-12 04:53:04 -07006516 return 0;
6517}
6518
Yang, Sheng002c7f72007-07-31 14:23:01 +03006519static void __init vmx_check_processor_compat(void *rtn)
6520{
6521 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006522 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03006523
6524 *(int *)rtn = 0;
Sean Christopherson7caaa712018-12-03 13:53:01 -08006525 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03006526 *(int *)rtn = -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006527 if (nested)
6528 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept,
6529 enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +03006530 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6531 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6532 smp_processor_id());
6533 *(int *)rtn = -EIO;
6534 }
6535}
6536
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006537static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006538{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006539 u8 cache;
6540 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006541
Sheng Yang522c68c2009-04-27 20:35:43 +08006542 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02006543 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08006544 * 2. EPT with VT-d:
6545 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02006546 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08006547 * b. VT-d with snooping control feature: snooping control feature of
6548 * VT-d engine can guarantee the cache correctness. Just set it
6549 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08006550 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08006551 * consistent with host MTRR
6552 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02006553 if (is_mmio) {
6554 cache = MTRR_TYPE_UNCACHABLE;
6555 goto exit;
6556 }
6557
6558 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006559 ipat = VMX_EPT_IPAT_BIT;
6560 cache = MTRR_TYPE_WRBACK;
6561 goto exit;
6562 }
6563
6564 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
6565 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02006566 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08006567 cache = MTRR_TYPE_WRBACK;
6568 else
6569 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006570 goto exit;
6571 }
6572
Xiao Guangrongff536042015-06-15 16:55:22 +08006573 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08006574
6575exit:
6576 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08006577}
6578
Sheng Yang17cc3932010-01-05 19:02:27 +08006579static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02006580{
Sheng Yang878403b2010-01-05 19:02:29 +08006581 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6582 return PT_DIRECTORY_LEVEL;
6583 else
6584 /* For shadow and EPT supported 1GB page */
6585 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02006586}
6587
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006588static void vmcs_set_secondary_exec_control(u32 new_ctl)
6589{
6590 /*
6591 * These bits in the secondary execution controls field
6592 * are dynamic, the others are mostly based on the hypervisor
6593 * architecture and the guest's CPUID. Do not touch the
6594 * dynamic bits.
6595 */
6596 u32 mask =
6597 SECONDARY_EXEC_SHADOW_VMCS |
6598 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02006599 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6600 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08006601
6602 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6603
6604 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6605 (new_ctl & ~mask) | (cur_ctl & mask));
6606}
6607
David Matlack8322ebb2016-11-29 18:14:09 -08006608/*
6609 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
6610 * (indicating "allowed-1") if they are supported in the guest's CPUID.
6611 */
6612static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
6613{
6614 struct vcpu_vmx *vmx = to_vmx(vcpu);
6615 struct kvm_cpuid_entry2 *entry;
6616
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006617 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
6618 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08006619
6620#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
6621 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006622 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08006623} while (0)
6624
6625 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
6626 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
6627 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
6628 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
6629 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
6630 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
6631 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
6632 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
6633 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
6634 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
6635 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
6636 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
6637 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
6638 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
6639 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
6640
6641 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
6642 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
6643 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
6644 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
6645 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +01006646 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -08006647
6648#undef cr4_fixed1_update
6649}
6650
Liran Alon5f76f6f2018-09-14 03:25:52 +03006651static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
6652{
6653 struct vcpu_vmx *vmx = to_vmx(vcpu);
6654
6655 if (kvm_mpx_supported()) {
6656 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
6657
6658 if (mpx_enabled) {
6659 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
6660 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
6661 } else {
6662 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
6663 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
6664 }
6665 }
6666}
6667
Sheng Yang0e851882009-12-18 16:48:46 +08006668static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
6669{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006670 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006671
Paolo Bonzini80154d72017-08-24 13:55:35 +02006672 if (cpu_has_secondary_exec_ctrls()) {
6673 vmx_compute_secondary_exec_control(vmx);
6674 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006675 }
Mao, Junjiead756a12012-07-02 01:18:48 +00006676
Haozhong Zhang37e4c992016-06-22 14:59:55 +08006677 if (nested_vmx_allowed(vcpu))
6678 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
6679 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
6680 else
6681 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
6682 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08006683
Liran Alon5f76f6f2018-09-14 03:25:52 +03006684 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08006685 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03006686 nested_vmx_entry_exit_ctls_update(vcpu);
6687 }
Sheng Yang0e851882009-12-18 16:48:46 +08006688}
6689
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006690static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
6691{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03006692 if (func == 1 && nested)
6693 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006694}
6695
Sean Christophersond264ee02018-08-27 15:21:12 -07006696static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
6697{
6698 to_vmx(vcpu)->req_immediate_exit = true;
6699}
6700
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006701static int vmx_check_intercept(struct kvm_vcpu *vcpu,
6702 struct x86_instruction_info *info,
6703 enum x86_intercept_stage stage)
6704{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02006705 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6706 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
6707
6708 /*
6709 * RDPID causes #UD if disabled through secondary execution controls.
6710 * Because it is marked as EmulateOnUD, we need to intercept it here.
6711 */
6712 if (info->intercept == x86_intercept_rdtscp &&
6713 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
6714 ctxt->exception.vector = UD_VECTOR;
6715 ctxt->exception.error_code_valid = false;
6716 return X86EMUL_PROPAGATE_FAULT;
6717 }
6718
6719 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006720 return X86EMUL_CONTINUE;
6721}
6722
Yunhong Jiang64672c92016-06-13 14:19:59 -07006723#ifdef CONFIG_X86_64
6724/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
6725static inline int u64_shl_div_u64(u64 a, unsigned int shift,
6726 u64 divisor, u64 *result)
6727{
6728 u64 low = a << shift, high = a >> (64 - shift);
6729
6730 /* To avoid the overflow on divq */
6731 if (high >= divisor)
6732 return 1;
6733
6734 /* Low hold the result, high hold rem which is discarded */
6735 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
6736 "rm" (divisor), "0" (low), "1" (high));
6737 *result = low;
6738
6739 return 0;
6740}
6741
6742static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
6743{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02006744 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08006745 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02006746
6747 if (kvm_mwait_in_guest(vcpu->kvm))
6748 return -EOPNOTSUPP;
6749
6750 vmx = to_vmx(vcpu);
6751 tscl = rdtsc();
6752 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
6753 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08006754 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
6755
6756 if (delta_tsc > lapic_timer_advance_cycles)
6757 delta_tsc -= lapic_timer_advance_cycles;
6758 else
6759 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006760
6761 /* Convert to host delta tsc if tsc scaling is enabled */
6762 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
6763 u64_shl_div_u64(delta_tsc,
6764 kvm_tsc_scaling_ratio_frac_bits,
6765 vcpu->arch.tsc_scaling_ratio,
6766 &delta_tsc))
6767 return -ERANGE;
6768
6769 /*
6770 * If the delta tsc can't fit in the 32 bit after the multi shift,
6771 * we can't use the preemption timer.
6772 * It's possible that it fits on later vmentries, but checking
6773 * on every vmentry is costly so we just use an hrtimer.
6774 */
6775 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
6776 return -ERANGE;
6777
6778 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -07006779 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006780}
6781
6782static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
6783{
Sean Christophersonf459a702018-08-27 15:21:11 -07006784 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07006785}
6786#endif
6787
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006788static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006789{
Wanpeng Lib31c1142018-03-12 04:53:04 -07006790 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006791 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006792}
6793
Kai Huang843e4332015-01-28 10:54:28 +08006794static void vmx_slot_enable_log_dirty(struct kvm *kvm,
6795 struct kvm_memory_slot *slot)
6796{
6797 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
6798 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
6799}
6800
6801static void vmx_slot_disable_log_dirty(struct kvm *kvm,
6802 struct kvm_memory_slot *slot)
6803{
6804 kvm_mmu_slot_set_dirty(kvm, slot);
6805}
6806
6807static void vmx_flush_log_dirty(struct kvm *kvm)
6808{
6809 kvm_flush_pml_buffers(kvm);
6810}
6811
Bandan Dasc5f983f2017-05-05 15:25:14 -04006812static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
6813{
6814 struct vmcs12 *vmcs12;
6815 struct vcpu_vmx *vmx = to_vmx(vcpu);
6816 gpa_t gpa;
6817 struct page *page = NULL;
6818 u64 *pml_address;
6819
6820 if (is_guest_mode(vcpu)) {
6821 WARN_ON_ONCE(vmx->nested.pml_full);
6822
6823 /*
6824 * Check if PML is enabled for the nested guest.
6825 * Whether eptp bit 6 is set is already checked
6826 * as part of A/D emulation.
6827 */
6828 vmcs12 = get_vmcs12(vcpu);
6829 if (!nested_cpu_has_pml(vmcs12))
6830 return 0;
6831
Dan Carpenter47698862017-05-10 22:43:17 +03006832 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -04006833 vmx->nested.pml_full = true;
6834 return 1;
6835 }
6836
6837 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
6838
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02006839 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
6840 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -04006841 return 0;
6842
6843 pml_address = kmap(page);
6844 pml_address[vmcs12->guest_pml_index--] = gpa;
6845 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02006846 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -04006847 }
6848
6849 return 0;
6850}
6851
Kai Huang843e4332015-01-28 10:54:28 +08006852static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
6853 struct kvm_memory_slot *memslot,
6854 gfn_t offset, unsigned long mask)
6855{
6856 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
6857}
6858
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006859static void __pi_post_block(struct kvm_vcpu *vcpu)
6860{
6861 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6862 struct pi_desc old, new;
6863 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006864
6865 do {
6866 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006867 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
6868 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006869
6870 dest = cpu_physical_id(vcpu->cpu);
6871
6872 if (x2apic_enabled())
6873 new.ndst = dest;
6874 else
6875 new.ndst = (dest << 8) & 0xFF00;
6876
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006877 /* set 'NV' to 'notification vector' */
6878 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02006879 } while (cmpxchg64(&pi_desc->control, old.control,
6880 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006881
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006882 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
6883 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006884 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006885 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006886 vcpu->pre_pcpu = -1;
6887 }
6888}
6889
Feng Wuefc64402015-09-18 22:29:51 +08006890/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08006891 * This routine does the following things for vCPU which is going
6892 * to be blocked if VT-d PI is enabled.
6893 * - Store the vCPU to the wakeup list, so when interrupts happen
6894 * we can find the right vCPU to wake up.
6895 * - Change the Posted-interrupt descriptor as below:
6896 * 'NDST' <-- vcpu->pre_pcpu
6897 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
6898 * - If 'ON' is set during this process, which means at least one
6899 * interrupt is posted for this vCPU, we cannot block it, in
6900 * this case, return 1, otherwise, return 0.
6901 *
6902 */
Yunhong Jiangbc225122016-06-13 14:19:58 -07006903static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006904{
Feng Wubf9f6ac2015-09-18 22:29:55 +08006905 unsigned int dest;
6906 struct pi_desc old, new;
6907 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6908
6909 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08006910 !irq_remapping_cap(IRQ_POSTING_CAP) ||
6911 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +08006912 return 0;
6913
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006914 WARN_ON(irqs_disabled());
6915 local_irq_disable();
6916 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
6917 vcpu->pre_pcpu = vcpu->cpu;
6918 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
6919 list_add_tail(&vcpu->blocked_vcpu_list,
6920 &per_cpu(blocked_vcpu_on_cpu,
6921 vcpu->pre_pcpu));
6922 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
6923 }
Feng Wubf9f6ac2015-09-18 22:29:55 +08006924
6925 do {
6926 old.control = new.control = pi_desc->control;
6927
Feng Wubf9f6ac2015-09-18 22:29:55 +08006928 WARN((pi_desc->sn == 1),
6929 "Warning: SN field of posted-interrupts "
6930 "is set before blocking\n");
6931
6932 /*
6933 * Since vCPU can be preempted during this process,
6934 * vcpu->cpu could be different with pre_pcpu, we
6935 * need to set pre_pcpu as the destination of wakeup
6936 * notification event, then we can find the right vCPU
6937 * to wakeup in wakeup handler if interrupts happen
6938 * when the vCPU is in blocked state.
6939 */
6940 dest = cpu_physical_id(vcpu->pre_pcpu);
6941
6942 if (x2apic_enabled())
6943 new.ndst = dest;
6944 else
6945 new.ndst = (dest << 8) & 0xFF00;
6946
6947 /* set 'NV' to 'wakeup vector' */
6948 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02006949 } while (cmpxchg64(&pi_desc->control, old.control,
6950 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006951
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006952 /* We should not block the vCPU if an interrupt is posted for it. */
6953 if (pi_test_on(pi_desc) == 1)
6954 __pi_post_block(vcpu);
6955
6956 local_irq_enable();
6957 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +08006958}
6959
Yunhong Jiangbc225122016-06-13 14:19:58 -07006960static int vmx_pre_block(struct kvm_vcpu *vcpu)
6961{
6962 if (pi_pre_block(vcpu))
6963 return 1;
6964
Yunhong Jiang64672c92016-06-13 14:19:59 -07006965 if (kvm_lapic_hv_timer_in_use(vcpu))
6966 kvm_lapic_switch_to_sw_timer(vcpu);
6967
Yunhong Jiangbc225122016-06-13 14:19:58 -07006968 return 0;
6969}
6970
6971static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006972{
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006973 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +08006974 return;
6975
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006976 WARN_ON(irqs_disabled());
6977 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +02006978 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02006979 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +08006980}
6981
Yunhong Jiangbc225122016-06-13 14:19:58 -07006982static void vmx_post_block(struct kvm_vcpu *vcpu)
6983{
Yunhong Jiang64672c92016-06-13 14:19:59 -07006984 if (kvm_x86_ops->set_hv_timer)
6985 kvm_lapic_switch_to_hv_timer(vcpu);
6986
Yunhong Jiangbc225122016-06-13 14:19:58 -07006987 pi_post_block(vcpu);
6988}
6989
Feng Wubf9f6ac2015-09-18 22:29:55 +08006990/*
Feng Wuefc64402015-09-18 22:29:51 +08006991 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
6992 *
6993 * @kvm: kvm
6994 * @host_irq: host irq of the interrupt
6995 * @guest_irq: gsi of the interrupt
6996 * @set: set or unset PI
6997 * returns 0 on success, < 0 on failure
6998 */
6999static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
7000 uint32_t guest_irq, bool set)
7001{
7002 struct kvm_kernel_irq_routing_entry *e;
7003 struct kvm_irq_routing_table *irq_rt;
7004 struct kvm_lapic_irq irq;
7005 struct kvm_vcpu *vcpu;
7006 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007007 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +08007008
7009 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08007010 !irq_remapping_cap(IRQ_POSTING_CAP) ||
7011 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +08007012 return 0;
7013
7014 idx = srcu_read_lock(&kvm->irq_srcu);
7015 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007016 if (guest_irq >= irq_rt->nr_rt_entries ||
7017 hlist_empty(&irq_rt->map[guest_irq])) {
7018 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
7019 guest_irq, irq_rt->nr_rt_entries);
7020 goto out;
7021 }
Feng Wuefc64402015-09-18 22:29:51 +08007022
7023 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
7024 if (e->type != KVM_IRQ_ROUTING_MSI)
7025 continue;
7026 /*
7027 * VT-d PI cannot support posting multicast/broadcast
7028 * interrupts to a vCPU, we still use interrupt remapping
7029 * for these kind of interrupts.
7030 *
7031 * For lowest-priority interrupts, we only support
7032 * those with single CPU as the destination, e.g. user
7033 * configures the interrupts via /proc/irq or uses
7034 * irqbalance to make the interrupts single-CPU.
7035 *
7036 * We will support full lowest-priority interrupt later.
7037 */
7038
Radim Krčmář371313132016-07-12 22:09:27 +02007039 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +08007040 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
7041 /*
7042 * Make sure the IRTE is in remapped mode if
7043 * we don't handle it in posted mode.
7044 */
7045 ret = irq_set_vcpu_affinity(host_irq, NULL);
7046 if (ret < 0) {
7047 printk(KERN_INFO
7048 "failed to back to remapped mode, irq: %u\n",
7049 host_irq);
7050 goto out;
7051 }
7052
Feng Wuefc64402015-09-18 22:29:51 +08007053 continue;
Feng Wu23a1c252016-01-25 16:53:32 +08007054 }
Feng Wuefc64402015-09-18 22:29:51 +08007055
7056 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
7057 vcpu_info.vector = irq.vector;
7058
hu huajun2698d822018-04-11 15:16:40 +08007059 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +08007060 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
7061
7062 if (set)
7063 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +08007064 else
Feng Wuefc64402015-09-18 22:29:51 +08007065 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +08007066
7067 if (ret < 0) {
7068 printk(KERN_INFO "%s: failed to update PI IRTE\n",
7069 __func__);
7070 goto out;
7071 }
7072 }
7073
7074 ret = 0;
7075out:
7076 srcu_read_unlock(&kvm->irq_srcu, idx);
7077 return ret;
7078}
7079
Ashok Rajc45dcc72016-06-22 14:59:56 +08007080static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7081{
7082 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7083 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
7084 FEATURE_CONTROL_LMCE;
7085 else
7086 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
7087 ~FEATURE_CONTROL_LMCE;
7088}
7089
Ladi Prosek72d7b372017-10-11 16:54:41 +02007090static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
7091{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007092 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7093 if (to_vmx(vcpu)->nested.nested_run_pending)
7094 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +02007095 return 1;
7096}
7097
Ladi Prosek0234bf82017-10-11 16:54:40 +02007098static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7099{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007100 struct vcpu_vmx *vmx = to_vmx(vcpu);
7101
7102 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7103 if (vmx->nested.smm.guest_mode)
7104 nested_vmx_vmexit(vcpu, -1, 0, 0);
7105
7106 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7107 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007108 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007109 return 0;
7110}
7111
7112static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
7113{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007114 struct vcpu_vmx *vmx = to_vmx(vcpu);
7115 int ret;
7116
7117 if (vmx->nested.smm.vmxon) {
7118 vmx->nested.vmxon = true;
7119 vmx->nested.smm.vmxon = false;
7120 }
7121
7122 if (vmx->nested.smm.guest_mode) {
7123 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -07007124 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007125 vcpu->arch.hflags |= HF_SMM_MASK;
7126 if (ret)
7127 return ret;
7128
7129 vmx->nested.smm.guest_mode = false;
7130 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007131 return 0;
7132}
7133
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007134static int enable_smi_window(struct kvm_vcpu *vcpu)
7135{
7136 return 0;
7137}
7138
Sean Christophersona3203382018-12-03 13:53:11 -08007139static __init int hardware_setup(void)
7140{
7141 unsigned long host_bndcfgs;
7142 int r, i;
7143
7144 rdmsrl_safe(MSR_EFER, &host_efer);
7145
7146 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7147 kvm_define_shared_msr(i, vmx_msr_index[i]);
7148
7149 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
7150 return -EIO;
7151
7152 if (boot_cpu_has(X86_FEATURE_NX))
7153 kvm_enable_efer_bits(EFER_NX);
7154
7155 if (boot_cpu_has(X86_FEATURE_MPX)) {
7156 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7157 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7158 }
7159
7160 if (boot_cpu_has(X86_FEATURE_XSAVES))
7161 rdmsrl(MSR_IA32_XSS, host_xss);
7162
7163 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7164 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
7165 enable_vpid = 0;
7166
7167 if (!cpu_has_vmx_ept() ||
7168 !cpu_has_vmx_ept_4levels() ||
7169 !cpu_has_vmx_ept_mt_wb() ||
7170 !cpu_has_vmx_invept_global())
7171 enable_ept = 0;
7172
7173 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
7174 enable_ept_ad_bits = 0;
7175
7176 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
7177 enable_unrestricted_guest = 0;
7178
7179 if (!cpu_has_vmx_flexpriority())
7180 flexpriority_enabled = 0;
7181
7182 if (!cpu_has_virtual_nmis())
7183 enable_vnmi = 0;
7184
7185 /*
7186 * set_apic_access_page_addr() is used to reload apic access
7187 * page upon invalidation. No need to do anything if not
7188 * using the APIC_ACCESS_ADDR VMCS field.
7189 */
7190 if (!flexpriority_enabled)
7191 kvm_x86_ops->set_apic_access_page_addr = NULL;
7192
7193 if (!cpu_has_vmx_tpr_shadow())
7194 kvm_x86_ops->update_cr8_intercept = NULL;
7195
7196 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7197 kvm_disable_largepages();
7198
7199#if IS_ENABLED(CONFIG_HYPERV)
7200 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7201 && enable_ept)
7202 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7203#endif
7204
7205 if (!cpu_has_vmx_ple()) {
7206 ple_gap = 0;
7207 ple_window = 0;
7208 ple_window_grow = 0;
7209 ple_window_max = 0;
7210 ple_window_shrink = 0;
7211 }
7212
7213 if (!cpu_has_vmx_apicv()) {
7214 enable_apicv = 0;
7215 kvm_x86_ops->sync_pir_to_irr = NULL;
7216 }
7217
7218 if (cpu_has_vmx_tsc_scaling()) {
7219 kvm_has_tsc_control = true;
7220 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7221 kvm_tsc_scaling_ratio_frac_bits = 48;
7222 }
7223
7224 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7225
7226 if (enable_ept)
7227 vmx_enable_tdp();
7228 else
7229 kvm_disable_tdp();
7230
Sean Christophersona3203382018-12-03 13:53:11 -08007231 /*
7232 * Only enable PML when hardware supports PML feature, and both EPT
7233 * and EPT A/D bit features are enabled -- PML depends on them to work.
7234 */
7235 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7236 enable_pml = 0;
7237
7238 if (!enable_pml) {
7239 kvm_x86_ops->slot_enable_log_dirty = NULL;
7240 kvm_x86_ops->slot_disable_log_dirty = NULL;
7241 kvm_x86_ops->flush_log_dirty = NULL;
7242 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7243 }
7244
7245 if (!cpu_has_vmx_preemption_timer())
7246 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7247
7248 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7249 u64 vmx_msr;
7250
7251 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7252 cpu_preemption_timer_multi =
7253 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7254 } else {
7255 kvm_x86_ops->set_hv_timer = NULL;
7256 kvm_x86_ops->cancel_hv_timer = NULL;
7257 }
7258
Sean Christophersona3203382018-12-03 13:53:11 -08007259 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Sean Christophersona3203382018-12-03 13:53:11 -08007260
7261 kvm_mce_cap_supported |= MCG_LMCE_P;
7262
Chao Pengf99e3da2018-10-24 16:05:10 +08007263 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
7264 return -EINVAL;
7265 if (!enable_ept || !cpu_has_vmx_intel_pt())
7266 pt_mode = PT_MODE_SYSTEM;
7267
Sean Christophersona3203382018-12-03 13:53:11 -08007268 if (nested) {
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007269 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
7270 vmx_capability.ept, enable_apicv);
7271
Sean Christophersone4027cf2018-12-03 13:53:12 -08007272 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Sean Christophersona3203382018-12-03 13:53:11 -08007273 if (r)
7274 return r;
7275 }
7276
7277 r = alloc_kvm_area();
7278 if (r)
7279 nested_vmx_hardware_unsetup();
7280 return r;
7281}
7282
7283static __exit void hardware_unsetup(void)
7284{
7285 if (nested)
7286 nested_vmx_hardware_unsetup();
7287
7288 free_kvm_area();
7289}
7290
Kees Cook404f6aa2016-08-08 16:29:06 -07007291static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292 .cpu_has_kvm_support = cpu_has_kvm_support,
7293 .disabled_by_bios = vmx_disabled_by_bios,
7294 .hardware_setup = hardware_setup,
7295 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007296 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007297 .hardware_enable = hardware_enable,
7298 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007299 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007300 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007301
Wanpeng Lib31c1142018-03-12 04:53:04 -07007302 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -07007303 .vm_alloc = vmx_vm_alloc,
7304 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -07007305
Avi Kivity6aa8b732006-12-10 02:21:36 -08007306 .vcpu_create = vmx_create_vcpu,
7307 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007308 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007309
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007310 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007311 .vcpu_load = vmx_vcpu_load,
7312 .vcpu_put = vmx_vcpu_put,
7313
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007314 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007315 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007316 .get_msr = vmx_get_msr,
7317 .set_msr = vmx_set_msr,
7318 .get_segment_base = vmx_get_segment_base,
7319 .get_segment = vmx_get_segment,
7320 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007321 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007322 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007323 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007324 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007325 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007326 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007327 .set_cr3 = vmx_set_cr3,
7328 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007329 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007330 .get_idt = vmx_get_idt,
7331 .set_idt = vmx_set_idt,
7332 .get_gdt = vmx_get_gdt,
7333 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007334 .get_dr6 = vmx_get_dr6,
7335 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007336 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007337 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007338 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007339 .get_rflags = vmx_get_rflags,
7340 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007341
Avi Kivity6aa8b732006-12-10 02:21:36 -08007342 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007343 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007344
Avi Kivity6aa8b732006-12-10 02:21:36 -08007345 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007346 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007347 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007348 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7349 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007350 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007351 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007352 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007353 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007354 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007355 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007356 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007357 .get_nmi_mask = vmx_get_nmi_mask,
7358 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007359 .enable_nmi_window = enable_nmi_window,
7360 .enable_irq_window = enable_irq_window,
7361 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007362 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007363 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007364 .get_enable_apicv = vmx_get_enable_apicv,
7365 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007366 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007367 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007368 .hwapic_irr_update = vmx_hwapic_irr_update,
7369 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007370 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007371 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7372 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007373
Izik Eiduscbc94022007-10-25 00:29:55 +02007374 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007375 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007376 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007377 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007378
Avi Kivity586f9602010-11-18 13:09:54 +02007379 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007380
Sheng Yang17cc3932010-01-05 19:02:27 +08007381 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007382
7383 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007384
7385 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007386 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007387
7388 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007389
7390 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007391
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02007392 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +02007393 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007394
7395 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007396
7397 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08007398 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007399 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007400 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007401 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007402
Sean Christophersond264ee02018-08-27 15:21:12 -07007403 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007404
7405 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007406
7407 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7408 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7409 .flush_log_dirty = vmx_flush_log_dirty,
7410 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -04007411 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +02007412
Feng Wubf9f6ac2015-09-18 22:29:55 +08007413 .pre_block = vmx_pre_block,
7414 .post_block = vmx_post_block,
7415
Wei Huang25462f72015-06-19 15:45:05 +02007416 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007417
7418 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007419
7420#ifdef CONFIG_X86_64
7421 .set_hv_timer = vmx_set_hv_timer,
7422 .cancel_hv_timer = vmx_cancel_hv_timer,
7423#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007424
7425 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007426
Ladi Prosek72d7b372017-10-11 16:54:41 +02007427 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007428 .pre_enter_smm = vmx_pre_enter_smm,
7429 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007430 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007431
Sean Christophersone4027cf2018-12-03 13:53:12 -08007432 .check_nested_events = NULL,
7433 .get_nested_state = NULL,
7434 .set_nested_state = NULL,
7435 .get_vmcs12_pages = NULL,
7436 .nested_enable_evmcs = NULL,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007437};
7438
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +02007439static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02007440{
7441 if (vmx_l1d_flush_pages) {
7442 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
7443 vmx_l1d_flush_pages = NULL;
7444 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +02007445 /* Restore state so sysfs ignores VMX */
7446 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +02007447}
7448
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007449static void vmx_exit(void)
7450{
7451#ifdef CONFIG_KEXEC_CORE
7452 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
7453 synchronize_rcu();
7454#endif
7455
7456 kvm_exit();
7457
7458#if IS_ENABLED(CONFIG_HYPERV)
7459 if (static_branch_unlikely(&enable_evmcs)) {
7460 int cpu;
7461 struct hv_vp_assist_page *vp_ap;
7462 /*
7463 * Reset everything to support using non-enlightened VMCS
7464 * access later (e.g. when we reload the module with
7465 * enlightened_vmcs=0)
7466 */
7467 for_each_online_cpu(cpu) {
7468 vp_ap = hv_get_vp_assist_page(cpu);
7469
7470 if (!vp_ap)
7471 continue;
7472
7473 vp_ap->current_nested_vmcs = 0;
7474 vp_ap->enlighten_vmentry = 0;
7475 }
7476
7477 static_branch_disable(&enable_evmcs);
7478 }
7479#endif
7480 vmx_cleanup_l1d_flush();
7481}
7482module_exit(vmx_exit);
7483
Avi Kivity6aa8b732006-12-10 02:21:36 -08007484static int __init vmx_init(void)
7485{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01007486 int r;
7487
7488#if IS_ENABLED(CONFIG_HYPERV)
7489 /*
7490 * Enlightened VMCS usage should be recommended and the host needs
7491 * to support eVMCS v1 or above. We can also disable eVMCS support
7492 * with module parameter.
7493 */
7494 if (enlightened_vmcs &&
7495 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
7496 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
7497 KVM_EVMCS_VERSION) {
7498 int cpu;
7499
7500 /* Check that we have assist pages on all online CPUs */
7501 for_each_online_cpu(cpu) {
7502 if (!hv_get_vp_assist_page(cpu)) {
7503 enlightened_vmcs = false;
7504 break;
7505 }
7506 }
7507
7508 if (enlightened_vmcs) {
7509 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
7510 static_branch_enable(&enable_evmcs);
7511 }
7512 } else {
7513 enlightened_vmcs = false;
7514 }
7515#endif
7516
7517 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007518 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007519 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007520 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +08007521
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007522 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +02007523 * Must be called after kvm_init() so enable_ept is properly set
7524 * up. Hand the parameter mitigation value in which was stored in
7525 * the pre module init parser. If no parameter was given, it will
7526 * contain 'auto' which will be turned into the default 'cond'
7527 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007528 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +02007529 if (boot_cpu_has(X86_BUG_L1TF)) {
7530 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
7531 if (r) {
7532 vmx_exit();
7533 return r;
7534 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02007535 }
7536
Dave Young2965faa2015-09-09 15:38:55 -07007537#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007538 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7539 crash_vmclear_local_loaded_vmcss);
7540#endif
Jim Mattson21ebf532018-05-01 15:40:28 -07007541 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007542
He, Qingfdef3ad2007-04-30 09:45:24 +03007543 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007544}
Thomas Gleixnera7b90202018-07-13 16:23:18 +02007545module_init(vmx_init);