blob: be6f13f1c25f7b2029542a5f2bc522c7dc35d580 [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
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +020023#include "hyperv.h"
Avi Kivitye4956062007-06-28 14:15:57 -040024
Avi Kivityedf88412007-12-16 11:02:48 +020025#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020027#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#include <linux/mm.h>
29#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040030#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020031#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070032#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040033#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040035#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010036#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050037#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080038#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030039#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030040#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040041
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020042#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080043#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080045#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020046#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020047#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080048#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020049#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020050#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010051#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080052#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010053#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080054#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070055#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020056#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010057#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080058
Marcelo Tosatti229456f2009-06-17 09:22:14 -030059#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020060#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010061#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030062
Avi Kivity4ecac3f2008-05-13 13:23:38 +030063#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040064#define __ex_clear(x, reg) \
Uros Bizjak43ce76c2018-10-17 16:46:57 +020065 ____kvm_handle_fault_on_reboot(x, "xor " reg ", " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030066
Avi Kivity6aa8b732006-12-10 02:21:36 -080067MODULE_AUTHOR("Qumranet");
68MODULE_LICENSE("GPL");
69
Josh Triplette9bda3b2012-03-20 23:33:51 -070070static const struct x86_cpu_id vmx_cpu_id[] = {
71 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
75
Rusty Russell476bc002012-01-13 09:32:18 +103076static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020077module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080078
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010079static bool __read_mostly enable_vnmi = 1;
80module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
81
Rusty Russell476bc002012-01-13 09:32:18 +103082static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020083module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020084
Rusty Russell476bc002012-01-13 09:32:18 +103085static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020086module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080087
Rusty Russell476bc002012-01-13 09:32:18 +103088static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070089module_param_named(unrestricted_guest,
90 enable_unrestricted_guest, bool, S_IRUGO);
91
Xudong Hao83c3a332012-05-28 19:33:35 +080092static bool __read_mostly enable_ept_ad_bits = 1;
93module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
94
Avi Kivitya27685c2012-06-12 20:30:18 +030095static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020096module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030097
Rusty Russell476bc002012-01-13 09:32:18 +103098static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030099module_param(fasteoi, bool, S_IRUGO);
100
Yang Zhang5a717852013-04-11 19:25:16 +0800101static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800102module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800103
Abel Gordonabc4fc52013-04-18 14:35:25 +0300104static bool __read_mostly enable_shadow_vmcs = 1;
105module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300106/*
107 * If nested=1, nested virtualization is supported, i.e., guests may use
108 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
109 * use VMX instructions.
110 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200111static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300112module_param(nested, bool, S_IRUGO);
113
Sean Christopherson52017602018-09-26 09:23:57 -0700114static bool __read_mostly nested_early_check = 0;
115module_param(nested_early_check, bool, S_IRUGO);
116
Wanpeng Li20300092014-12-02 19:14:59 +0800117static u64 __read_mostly host_xss;
118
Kai Huang843e4332015-01-28 10:54:28 +0800119static bool __read_mostly enable_pml = 1;
120module_param_named(pml, enable_pml, bool, S_IRUGO);
121
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100122#define MSR_TYPE_R 1
123#define MSR_TYPE_W 2
124#define MSR_TYPE_RW 3
125
126#define MSR_BITMAP_MODE_X2APIC 1
127#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100128
Haozhong Zhang64903d62015-10-20 15:39:09 +0800129#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
130
Yunhong Jiang64672c92016-06-13 14:19:59 -0700131/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
132static int __read_mostly cpu_preemption_timer_multi;
133static bool __read_mostly enable_preemption_timer = 1;
134#ifdef CONFIG_X86_64
135module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
136#endif
137
Sean Christopherson3de63472018-07-13 08:42:30 -0700138#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800139#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
140#define KVM_VM_CR0_ALWAYS_ON \
141 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
142 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200143#define KVM_CR4_GUEST_OWNED_BITS \
144 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800145 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200146
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800147#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200148#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
149#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
150
Avi Kivity78ac8b42010-04-08 18:19:35 +0300151#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
152
Jan Kiszkaf41245002014-03-07 20:03:13 +0100153#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
154
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800155/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300156 * Hyper-V requires all of these, so mark them as supported even though
157 * they are just treated the same as all-context.
158 */
159#define VMX_VPID_EXTENT_SUPPORTED_MASK \
160 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
161 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
163 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
164
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800165/*
166 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
167 * ple_gap: upper bound on the amount of time between two successive
168 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500169 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800170 * ple_window: upper bound on the amount of time a guest is allowed to execute
171 * in a PAUSE loop. Tests indicate that most spinlocks are held for
172 * less than 2^12 cycles
173 * Time is measured based on a counter that runs at the same rate as the TSC,
174 * refer SDM volume 3b section 21.6.13 & 22.1.3.
175 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400176static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500177module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
180module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800181
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200182/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400187static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400188module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
190/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400191static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
192module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200193
Avi Kivity83287ea422012-09-16 15:10:57 +0300194extern const ulong vmx_return;
Sean Christopherson52017602018-09-26 09:23:57 -0700195extern const ulong vmx_early_consistency_check_return;
Avi Kivity83287ea422012-09-16 15:10:57 +0300196
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200197static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200198static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200199static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200200
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200201/* Storage for pre module init parameter parsing */
202static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200203
204static const struct {
205 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200206 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200207} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200208 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
209 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
210 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
211 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
212 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
213 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200214};
215
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200216#define L1D_CACHE_ORDER 4
217static void *vmx_l1d_flush_pages;
218
219static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
220{
221 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200222 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200223
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200224 if (!enable_ept) {
225 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
226 return 0;
227 }
228
Yi Wangd806afa2018-08-16 13:42:39 +0800229 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
230 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200231
Yi Wangd806afa2018-08-16 13:42:39 +0800232 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
233 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
234 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
235 return 0;
236 }
237 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200238
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200239 /* If set to auto use the default l1tf mitigation method */
240 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
241 switch (l1tf_mitigation) {
242 case L1TF_MITIGATION_OFF:
243 l1tf = VMENTER_L1D_FLUSH_NEVER;
244 break;
245 case L1TF_MITIGATION_FLUSH_NOWARN:
246 case L1TF_MITIGATION_FLUSH:
247 case L1TF_MITIGATION_FLUSH_NOSMT:
248 l1tf = VMENTER_L1D_FLUSH_COND;
249 break;
250 case L1TF_MITIGATION_FULL:
251 case L1TF_MITIGATION_FULL_FORCE:
252 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
253 break;
254 }
255 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
256 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
257 }
258
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200259 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
260 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
261 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
262 if (!page)
263 return -ENOMEM;
264 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200265
266 /*
267 * Initialize each page with a different pattern in
268 * order to protect against KSM in the nested
269 * virtualization case.
270 */
271 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
272 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
273 PAGE_SIZE);
274 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200275 }
276
277 l1tf_vmx_mitigation = l1tf;
278
Thomas Gleixner895ae472018-07-13 16:23:22 +0200279 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
280 static_branch_enable(&vmx_l1d_should_flush);
281 else
282 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200283
Nicolai Stange427362a2018-07-21 22:25:00 +0200284 if (l1tf == VMENTER_L1D_FLUSH_COND)
285 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200286 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200287 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200288 return 0;
289}
290
291static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200292{
293 unsigned int i;
294
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200295 if (s) {
296 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200297 if (vmentry_l1d_param[i].for_parse &&
298 sysfs_streq(s, vmentry_l1d_param[i].option))
299 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200300 }
301 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200302 return -EINVAL;
303}
304
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200305static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
306{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200307 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200308
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200309 l1tf = vmentry_l1d_flush_parse(s);
310 if (l1tf < 0)
311 return l1tf;
312
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200313 if (!boot_cpu_has(X86_BUG_L1TF))
314 return 0;
315
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200316 /*
317 * Has vmx_init() run already? If not then this is the pre init
318 * parameter parsing. In that case just store the value and let
319 * vmx_init() do the proper setup after enable_ept has been
320 * established.
321 */
322 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
323 vmentry_l1d_flush_param = l1tf;
324 return 0;
325 }
326
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200327 mutex_lock(&vmx_l1d_flush_mutex);
328 ret = vmx_setup_l1d_flush(l1tf);
329 mutex_unlock(&vmx_l1d_flush_mutex);
330 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200331}
332
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200333static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
334{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200335 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
336 return sprintf(s, "???\n");
337
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200338 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200339}
340
341static const struct kernel_param_ops vmentry_l1d_flush_ops = {
342 .set = vmentry_l1d_flush_set,
343 .get = vmentry_l1d_flush_get,
344};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200345module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200346
Tianyu Lan877ad952018-07-19 08:40:23 +0000347enum ept_pointers_status {
348 EPT_POINTERS_CHECK = 0,
349 EPT_POINTERS_MATCH = 1,
350 EPT_POINTERS_MISMATCH = 2
351};
352
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700353struct kvm_vmx {
354 struct kvm kvm;
355
356 unsigned int tss_addr;
357 bool ept_identity_pagetable_done;
358 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000359
360 enum ept_pointers_status ept_pointers_match;
361 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700362};
363
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200364#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300365
Liran Alon392b2f22018-06-23 02:35:01 +0300366struct vmcs_hdr {
367 u32 revision_id:31;
368 u32 shadow_vmcs:1;
369};
370
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400371struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300372 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400373 u32 abort;
374 char data[0];
375};
376
Nadav Har'Eld462b812011-05-24 15:26:10 +0300377/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700378 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
379 * and whose values change infrequently, but are not constant. I.e. this is
380 * used as a write-through cache of the corresponding VMCS fields.
381 */
382struct vmcs_host_state {
383 unsigned long cr3; /* May not match real cr3 */
384 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700385 unsigned long gs_base;
386 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700387
388 u16 fs_sel, gs_sel, ldt_sel;
389#ifdef CONFIG_X86_64
390 u16 ds_sel, es_sel;
391#endif
392};
393
394/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300395 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
396 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
397 * loaded on this CPU (so we can clear them if the CPU goes down).
398 */
399struct loaded_vmcs {
400 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700401 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300402 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200403 bool launched;
404 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700405 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100406 /* Support for vnmi-less CPUs */
407 int soft_vnmi_blocked;
408 ktime_t entry_time;
409 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100410 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300411 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700412 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300413};
414
Avi Kivity26bb0982009-09-07 11:14:12 +0300415struct shared_msr_entry {
416 unsigned index;
417 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200418 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300419};
420
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300421/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300422 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
423 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
424 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
425 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
426 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
427 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600428 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300429 * underlying hardware which will be used to run L2.
430 * This structure is packed to ensure that its layout is identical across
431 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700432 *
433 * IMPORTANT: Changing the layout of existing fields in this structure
434 * will break save/restore compatibility with older kvm releases. When
435 * adding new fields, either use space in the reserved padding* arrays
436 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300437 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300438typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300439struct __packed vmcs12 {
440 /* According to the Intel spec, a VMCS region must start with the
441 * following two fields. Then follow implementation-specific data.
442 */
Liran Alon392b2f22018-06-23 02:35:01 +0300443 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300444 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300445
Nadav Har'El27d6c862011-05-25 23:06:59 +0300446 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
447 u32 padding[7]; /* room for future expansion */
448
Nadav Har'El22bd0352011-05-25 23:05:57 +0300449 u64 io_bitmap_a;
450 u64 io_bitmap_b;
451 u64 msr_bitmap;
452 u64 vm_exit_msr_store_addr;
453 u64 vm_exit_msr_load_addr;
454 u64 vm_entry_msr_load_addr;
455 u64 tsc_offset;
456 u64 virtual_apic_page_addr;
457 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800458 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300459 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800460 u64 eoi_exit_bitmap0;
461 u64 eoi_exit_bitmap1;
462 u64 eoi_exit_bitmap2;
463 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800464 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300465 u64 guest_physical_address;
466 u64 vmcs_link_pointer;
467 u64 guest_ia32_debugctl;
468 u64 guest_ia32_pat;
469 u64 guest_ia32_efer;
470 u64 guest_ia32_perf_global_ctrl;
471 u64 guest_pdptr0;
472 u64 guest_pdptr1;
473 u64 guest_pdptr2;
474 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100475 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300476 u64 host_ia32_pat;
477 u64 host_ia32_efer;
478 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700479 u64 vmread_bitmap;
480 u64 vmwrite_bitmap;
481 u64 vm_function_control;
482 u64 eptp_list_address;
483 u64 pml_address;
484 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300485 /*
486 * To allow migration of L1 (complete with its L2 guests) between
487 * machines of different natural widths (32 or 64 bit), we cannot have
488 * unsigned long fields with no explict size. We use u64 (aliased
489 * natural_width) instead. Luckily, x86 is little-endian.
490 */
491 natural_width cr0_guest_host_mask;
492 natural_width cr4_guest_host_mask;
493 natural_width cr0_read_shadow;
494 natural_width cr4_read_shadow;
495 natural_width cr3_target_value0;
496 natural_width cr3_target_value1;
497 natural_width cr3_target_value2;
498 natural_width cr3_target_value3;
499 natural_width exit_qualification;
500 natural_width guest_linear_address;
501 natural_width guest_cr0;
502 natural_width guest_cr3;
503 natural_width guest_cr4;
504 natural_width guest_es_base;
505 natural_width guest_cs_base;
506 natural_width guest_ss_base;
507 natural_width guest_ds_base;
508 natural_width guest_fs_base;
509 natural_width guest_gs_base;
510 natural_width guest_ldtr_base;
511 natural_width guest_tr_base;
512 natural_width guest_gdtr_base;
513 natural_width guest_idtr_base;
514 natural_width guest_dr7;
515 natural_width guest_rsp;
516 natural_width guest_rip;
517 natural_width guest_rflags;
518 natural_width guest_pending_dbg_exceptions;
519 natural_width guest_sysenter_esp;
520 natural_width guest_sysenter_eip;
521 natural_width host_cr0;
522 natural_width host_cr3;
523 natural_width host_cr4;
524 natural_width host_fs_base;
525 natural_width host_gs_base;
526 natural_width host_tr_base;
527 natural_width host_gdtr_base;
528 natural_width host_idtr_base;
529 natural_width host_ia32_sysenter_esp;
530 natural_width host_ia32_sysenter_eip;
531 natural_width host_rsp;
532 natural_width host_rip;
533 natural_width paddingl[8]; /* room for future expansion */
534 u32 pin_based_vm_exec_control;
535 u32 cpu_based_vm_exec_control;
536 u32 exception_bitmap;
537 u32 page_fault_error_code_mask;
538 u32 page_fault_error_code_match;
539 u32 cr3_target_count;
540 u32 vm_exit_controls;
541 u32 vm_exit_msr_store_count;
542 u32 vm_exit_msr_load_count;
543 u32 vm_entry_controls;
544 u32 vm_entry_msr_load_count;
545 u32 vm_entry_intr_info_field;
546 u32 vm_entry_exception_error_code;
547 u32 vm_entry_instruction_len;
548 u32 tpr_threshold;
549 u32 secondary_vm_exec_control;
550 u32 vm_instruction_error;
551 u32 vm_exit_reason;
552 u32 vm_exit_intr_info;
553 u32 vm_exit_intr_error_code;
554 u32 idt_vectoring_info_field;
555 u32 idt_vectoring_error_code;
556 u32 vm_exit_instruction_len;
557 u32 vmx_instruction_info;
558 u32 guest_es_limit;
559 u32 guest_cs_limit;
560 u32 guest_ss_limit;
561 u32 guest_ds_limit;
562 u32 guest_fs_limit;
563 u32 guest_gs_limit;
564 u32 guest_ldtr_limit;
565 u32 guest_tr_limit;
566 u32 guest_gdtr_limit;
567 u32 guest_idtr_limit;
568 u32 guest_es_ar_bytes;
569 u32 guest_cs_ar_bytes;
570 u32 guest_ss_ar_bytes;
571 u32 guest_ds_ar_bytes;
572 u32 guest_fs_ar_bytes;
573 u32 guest_gs_ar_bytes;
574 u32 guest_ldtr_ar_bytes;
575 u32 guest_tr_ar_bytes;
576 u32 guest_interruptibility_info;
577 u32 guest_activity_state;
578 u32 guest_sysenter_cs;
579 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100580 u32 vmx_preemption_timer_value;
581 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300582 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800583 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300584 u16 guest_es_selector;
585 u16 guest_cs_selector;
586 u16 guest_ss_selector;
587 u16 guest_ds_selector;
588 u16 guest_fs_selector;
589 u16 guest_gs_selector;
590 u16 guest_ldtr_selector;
591 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800592 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300593 u16 host_es_selector;
594 u16 host_cs_selector;
595 u16 host_ss_selector;
596 u16 host_ds_selector;
597 u16 host_fs_selector;
598 u16 host_gs_selector;
599 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700600 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300601};
602
603/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700604 * For save/restore compatibility, the vmcs12 field offsets must not change.
605 */
606#define CHECK_OFFSET(field, loc) \
607 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
608 "Offset of " #field " in struct vmcs12 has changed.")
609
610static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300611 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700612 CHECK_OFFSET(abort, 4);
613 CHECK_OFFSET(launch_state, 8);
614 CHECK_OFFSET(io_bitmap_a, 40);
615 CHECK_OFFSET(io_bitmap_b, 48);
616 CHECK_OFFSET(msr_bitmap, 56);
617 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
618 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
619 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
620 CHECK_OFFSET(tsc_offset, 88);
621 CHECK_OFFSET(virtual_apic_page_addr, 96);
622 CHECK_OFFSET(apic_access_addr, 104);
623 CHECK_OFFSET(posted_intr_desc_addr, 112);
624 CHECK_OFFSET(ept_pointer, 120);
625 CHECK_OFFSET(eoi_exit_bitmap0, 128);
626 CHECK_OFFSET(eoi_exit_bitmap1, 136);
627 CHECK_OFFSET(eoi_exit_bitmap2, 144);
628 CHECK_OFFSET(eoi_exit_bitmap3, 152);
629 CHECK_OFFSET(xss_exit_bitmap, 160);
630 CHECK_OFFSET(guest_physical_address, 168);
631 CHECK_OFFSET(vmcs_link_pointer, 176);
632 CHECK_OFFSET(guest_ia32_debugctl, 184);
633 CHECK_OFFSET(guest_ia32_pat, 192);
634 CHECK_OFFSET(guest_ia32_efer, 200);
635 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
636 CHECK_OFFSET(guest_pdptr0, 216);
637 CHECK_OFFSET(guest_pdptr1, 224);
638 CHECK_OFFSET(guest_pdptr2, 232);
639 CHECK_OFFSET(guest_pdptr3, 240);
640 CHECK_OFFSET(guest_bndcfgs, 248);
641 CHECK_OFFSET(host_ia32_pat, 256);
642 CHECK_OFFSET(host_ia32_efer, 264);
643 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
644 CHECK_OFFSET(vmread_bitmap, 280);
645 CHECK_OFFSET(vmwrite_bitmap, 288);
646 CHECK_OFFSET(vm_function_control, 296);
647 CHECK_OFFSET(eptp_list_address, 304);
648 CHECK_OFFSET(pml_address, 312);
649 CHECK_OFFSET(cr0_guest_host_mask, 344);
650 CHECK_OFFSET(cr4_guest_host_mask, 352);
651 CHECK_OFFSET(cr0_read_shadow, 360);
652 CHECK_OFFSET(cr4_read_shadow, 368);
653 CHECK_OFFSET(cr3_target_value0, 376);
654 CHECK_OFFSET(cr3_target_value1, 384);
655 CHECK_OFFSET(cr3_target_value2, 392);
656 CHECK_OFFSET(cr3_target_value3, 400);
657 CHECK_OFFSET(exit_qualification, 408);
658 CHECK_OFFSET(guest_linear_address, 416);
659 CHECK_OFFSET(guest_cr0, 424);
660 CHECK_OFFSET(guest_cr3, 432);
661 CHECK_OFFSET(guest_cr4, 440);
662 CHECK_OFFSET(guest_es_base, 448);
663 CHECK_OFFSET(guest_cs_base, 456);
664 CHECK_OFFSET(guest_ss_base, 464);
665 CHECK_OFFSET(guest_ds_base, 472);
666 CHECK_OFFSET(guest_fs_base, 480);
667 CHECK_OFFSET(guest_gs_base, 488);
668 CHECK_OFFSET(guest_ldtr_base, 496);
669 CHECK_OFFSET(guest_tr_base, 504);
670 CHECK_OFFSET(guest_gdtr_base, 512);
671 CHECK_OFFSET(guest_idtr_base, 520);
672 CHECK_OFFSET(guest_dr7, 528);
673 CHECK_OFFSET(guest_rsp, 536);
674 CHECK_OFFSET(guest_rip, 544);
675 CHECK_OFFSET(guest_rflags, 552);
676 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
677 CHECK_OFFSET(guest_sysenter_esp, 568);
678 CHECK_OFFSET(guest_sysenter_eip, 576);
679 CHECK_OFFSET(host_cr0, 584);
680 CHECK_OFFSET(host_cr3, 592);
681 CHECK_OFFSET(host_cr4, 600);
682 CHECK_OFFSET(host_fs_base, 608);
683 CHECK_OFFSET(host_gs_base, 616);
684 CHECK_OFFSET(host_tr_base, 624);
685 CHECK_OFFSET(host_gdtr_base, 632);
686 CHECK_OFFSET(host_idtr_base, 640);
687 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
688 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
689 CHECK_OFFSET(host_rsp, 664);
690 CHECK_OFFSET(host_rip, 672);
691 CHECK_OFFSET(pin_based_vm_exec_control, 744);
692 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
693 CHECK_OFFSET(exception_bitmap, 752);
694 CHECK_OFFSET(page_fault_error_code_mask, 756);
695 CHECK_OFFSET(page_fault_error_code_match, 760);
696 CHECK_OFFSET(cr3_target_count, 764);
697 CHECK_OFFSET(vm_exit_controls, 768);
698 CHECK_OFFSET(vm_exit_msr_store_count, 772);
699 CHECK_OFFSET(vm_exit_msr_load_count, 776);
700 CHECK_OFFSET(vm_entry_controls, 780);
701 CHECK_OFFSET(vm_entry_msr_load_count, 784);
702 CHECK_OFFSET(vm_entry_intr_info_field, 788);
703 CHECK_OFFSET(vm_entry_exception_error_code, 792);
704 CHECK_OFFSET(vm_entry_instruction_len, 796);
705 CHECK_OFFSET(tpr_threshold, 800);
706 CHECK_OFFSET(secondary_vm_exec_control, 804);
707 CHECK_OFFSET(vm_instruction_error, 808);
708 CHECK_OFFSET(vm_exit_reason, 812);
709 CHECK_OFFSET(vm_exit_intr_info, 816);
710 CHECK_OFFSET(vm_exit_intr_error_code, 820);
711 CHECK_OFFSET(idt_vectoring_info_field, 824);
712 CHECK_OFFSET(idt_vectoring_error_code, 828);
713 CHECK_OFFSET(vm_exit_instruction_len, 832);
714 CHECK_OFFSET(vmx_instruction_info, 836);
715 CHECK_OFFSET(guest_es_limit, 840);
716 CHECK_OFFSET(guest_cs_limit, 844);
717 CHECK_OFFSET(guest_ss_limit, 848);
718 CHECK_OFFSET(guest_ds_limit, 852);
719 CHECK_OFFSET(guest_fs_limit, 856);
720 CHECK_OFFSET(guest_gs_limit, 860);
721 CHECK_OFFSET(guest_ldtr_limit, 864);
722 CHECK_OFFSET(guest_tr_limit, 868);
723 CHECK_OFFSET(guest_gdtr_limit, 872);
724 CHECK_OFFSET(guest_idtr_limit, 876);
725 CHECK_OFFSET(guest_es_ar_bytes, 880);
726 CHECK_OFFSET(guest_cs_ar_bytes, 884);
727 CHECK_OFFSET(guest_ss_ar_bytes, 888);
728 CHECK_OFFSET(guest_ds_ar_bytes, 892);
729 CHECK_OFFSET(guest_fs_ar_bytes, 896);
730 CHECK_OFFSET(guest_gs_ar_bytes, 900);
731 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
732 CHECK_OFFSET(guest_tr_ar_bytes, 908);
733 CHECK_OFFSET(guest_interruptibility_info, 912);
734 CHECK_OFFSET(guest_activity_state, 916);
735 CHECK_OFFSET(guest_sysenter_cs, 920);
736 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
737 CHECK_OFFSET(vmx_preemption_timer_value, 928);
738 CHECK_OFFSET(virtual_processor_id, 960);
739 CHECK_OFFSET(posted_intr_nv, 962);
740 CHECK_OFFSET(guest_es_selector, 964);
741 CHECK_OFFSET(guest_cs_selector, 966);
742 CHECK_OFFSET(guest_ss_selector, 968);
743 CHECK_OFFSET(guest_ds_selector, 970);
744 CHECK_OFFSET(guest_fs_selector, 972);
745 CHECK_OFFSET(guest_gs_selector, 974);
746 CHECK_OFFSET(guest_ldtr_selector, 976);
747 CHECK_OFFSET(guest_tr_selector, 978);
748 CHECK_OFFSET(guest_intr_status, 980);
749 CHECK_OFFSET(host_es_selector, 982);
750 CHECK_OFFSET(host_cs_selector, 984);
751 CHECK_OFFSET(host_ss_selector, 986);
752 CHECK_OFFSET(host_ds_selector, 988);
753 CHECK_OFFSET(host_fs_selector, 990);
754 CHECK_OFFSET(host_gs_selector, 992);
755 CHECK_OFFSET(host_tr_selector, 994);
756 CHECK_OFFSET(guest_pml_index, 996);
757}
758
759/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300760 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
761 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
762 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700763 *
764 * IMPORTANT: Changing this value will break save/restore compatibility with
765 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300766 */
767#define VMCS12_REVISION 0x11e57ed0
768
769/*
770 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
771 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
772 * current implementation, 4K are reserved to avoid future complications.
773 */
774#define VMCS12_SIZE 0x1000
775
776/*
Jim Mattson5b157062017-12-22 12:11:12 -0800777 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
778 * supported VMCS12 field encoding.
779 */
780#define VMCS12_MAX_FIELD_INDEX 0x17
781
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100782struct nested_vmx_msrs {
783 /*
784 * We only store the "true" versions of the VMX capability MSRs. We
785 * generate the "non-true" versions by setting the must-be-1 bits
786 * according to the SDM.
787 */
788 u32 procbased_ctls_low;
789 u32 procbased_ctls_high;
790 u32 secondary_ctls_low;
791 u32 secondary_ctls_high;
792 u32 pinbased_ctls_low;
793 u32 pinbased_ctls_high;
794 u32 exit_ctls_low;
795 u32 exit_ctls_high;
796 u32 entry_ctls_low;
797 u32 entry_ctls_high;
798 u32 misc_low;
799 u32 misc_high;
800 u32 ept_caps;
801 u32 vpid_caps;
802 u64 basic;
803 u64 cr0_fixed0;
804 u64 cr0_fixed1;
805 u64 cr4_fixed0;
806 u64 cr4_fixed1;
807 u64 vmcs_enum;
808 u64 vmfunc_controls;
809};
810
Jim Mattson5b157062017-12-22 12:11:12 -0800811/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300812 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
813 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
814 */
815struct nested_vmx {
816 /* Has the level1 guest done vmxon? */
817 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400818 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400819 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300820
821 /* The guest-physical address of the current VMCS L1 keeps for L2 */
822 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700823 /*
824 * Cache of the guest's VMCS, existing outside of guest memory.
825 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700826 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700827 */
828 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300829 /*
Liran Alon61ada742018-06-23 02:35:08 +0300830 * Cache of the guest's shadow VMCS, existing outside of guest
831 * memory. Loaded from guest memory during VM entry. Flushed
832 * to guest memory during VM exit.
833 */
834 struct vmcs12 *cached_shadow_vmcs12;
835 /*
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200836 * Indicates if the shadow vmcs or enlightened vmcs must be updated
837 * with the data held by struct vmcs12.
Abel Gordon012f83c2013-04-18 14:39:25 +0300838 */
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200839 bool need_vmcs12_sync;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100840 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300841
Sean Christopherson9d6105b2018-09-26 09:23:51 -0700842 /*
843 * vmcs02 has been initialized, i.e. state that is constant for
844 * vmcs02 has been written to the backing VMCS. Initialization
845 * is delayed until L1 actually attempts to run a nested VM.
846 */
847 bool vmcs02_initialized;
848
Jim Mattson8d860bb2018-05-09 16:56:05 -0400849 bool change_vmcs01_virtual_apic_mode;
850
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +0200851 /*
852 * Enlightened VMCS has been enabled. It does not mean that L1 has to
853 * use it. However, VMX features available to L1 will be limited based
854 * on what the enlightened VMCS supports.
855 */
856 bool enlightened_vmcs_enabled;
857
Nadav Har'El644d7112011-05-25 23:12:35 +0300858 /* L2 must run next, and mustn't decide to exit to L1. */
859 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600860
861 struct loaded_vmcs vmcs02;
862
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300863 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600864 * Guest pages referred to in the vmcs02 with host-physical
865 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300866 */
867 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800868 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800869 struct page *pi_desc_page;
870 struct pi_desc *pi_desc;
871 bool pi_pending;
872 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100873
874 struct hrtimer preemption_timer;
875 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200876
877 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
878 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300879 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800880
Wanpeng Li5c614b32015-10-13 09:18:36 -0700881 u16 vpid02;
882 u16 last_vpid;
883
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100884 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200885
886 /* SMM related state */
887 struct {
888 /* in VMX operation on SMM entry? */
889 bool vmxon;
890 /* in guest mode on SMM entry? */
891 bool guest_mode;
892 } smm;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200893
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +0200894 gpa_t hv_evmcs_vmptr;
895 struct page *hv_evmcs_page;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +0200896 struct hv_enlightened_vmcs *hv_evmcs;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300897};
898
Yang Zhang01e439b2013-04-11 19:25:12 +0800899#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800900#define POSTED_INTR_SN 1
901
Yang Zhang01e439b2013-04-11 19:25:12 +0800902/* Posted-Interrupt Descriptor */
903struct pi_desc {
904 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800905 union {
906 struct {
907 /* bit 256 - Outstanding Notification */
908 u16 on : 1,
909 /* bit 257 - Suppress Notification */
910 sn : 1,
911 /* bit 271:258 - Reserved */
912 rsvd_1 : 14;
913 /* bit 279:272 - Notification Vector */
914 u8 nv;
915 /* bit 287:280 - Reserved */
916 u8 rsvd_2;
917 /* bit 319:288 - Notification Destination */
918 u32 ndst;
919 };
920 u64 control;
921 };
922 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800923} __aligned(64);
924
Yang Zhanga20ed542013-04-11 19:25:15 +0800925static bool pi_test_and_set_on(struct pi_desc *pi_desc)
926{
927 return test_and_set_bit(POSTED_INTR_ON,
928 (unsigned long *)&pi_desc->control);
929}
930
931static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
932{
933 return test_and_clear_bit(POSTED_INTR_ON,
934 (unsigned long *)&pi_desc->control);
935}
936
937static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
938{
939 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
940}
941
Feng Wuebbfc762015-09-18 22:29:46 +0800942static inline void pi_clear_sn(struct pi_desc *pi_desc)
943{
944 return clear_bit(POSTED_INTR_SN,
945 (unsigned long *)&pi_desc->control);
946}
947
948static inline void pi_set_sn(struct pi_desc *pi_desc)
949{
950 return set_bit(POSTED_INTR_SN,
951 (unsigned long *)&pi_desc->control);
952}
953
Paolo Bonziniad361092016-09-20 16:15:05 +0200954static inline void pi_clear_on(struct pi_desc *pi_desc)
955{
956 clear_bit(POSTED_INTR_ON,
957 (unsigned long *)&pi_desc->control);
958}
959
Feng Wuebbfc762015-09-18 22:29:46 +0800960static inline int pi_test_on(struct pi_desc *pi_desc)
961{
962 return test_bit(POSTED_INTR_ON,
963 (unsigned long *)&pi_desc->control);
964}
965
966static inline int pi_test_sn(struct pi_desc *pi_desc)
967{
968 return test_bit(POSTED_INTR_SN,
969 (unsigned long *)&pi_desc->control);
970}
971
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400972struct vmx_msrs {
973 unsigned int nr;
974 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
975};
976
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000978 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300979 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300980 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100981 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300982 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200983 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200984 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300985 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986 int nmsrs;
987 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800988 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400989#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300990 u64 msr_host_kernel_gs_base;
991 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400992#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100993
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100994 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100995 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100996
Gleb Natapov2961e8762013-11-25 15:37:13 +0200997 u32 vm_entry_controls_shadow;
998 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200999 u32 secondary_exec_control;
1000
Nadav Har'Eld462b812011-05-24 15:26:10 +03001001 /*
1002 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
1003 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001004 * guest (L2), it points to a different VMCS. loaded_cpu_state points
1005 * to the VMCS whose state is loaded into the CPU registers that only
1006 * need to be switched when transitioning to/from the kernel; a NULL
1007 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +03001008 */
1009 struct loaded_vmcs vmcs01;
1010 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001011 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +03001012 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +03001013 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001014 struct vmx_msrs guest;
1015 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001016 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001017
Avi Kivity9c8cba32007-11-22 11:42:59 +02001018 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001019 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001020 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001021 struct kvm_segment segs[8];
1022 } rmode;
1023 struct {
1024 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001025 struct kvm_save_segment {
1026 u16 selector;
1027 unsigned long base;
1028 u32 limit;
1029 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001030 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001031 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001032 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001033 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001034
Andi Kleena0861c02009-06-08 17:37:09 +08001035 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001036
Yang Zhang01e439b2013-04-11 19:25:12 +08001037 /* Posted interrupt descriptor */
1038 struct pi_desc pi_desc;
1039
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001040 /* Support for a guest hypervisor (nested VMX) */
1041 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001042
1043 /* Dynamic PLE window. */
1044 int ple_window;
1045 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001046
Sean Christophersond264ee02018-08-27 15:21:12 -07001047 bool req_immediate_exit;
1048
Kai Huang843e4332015-01-28 10:54:28 +08001049 /* Support for PML */
1050#define PML_ENTITY_NUM 512
1051 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001052
Yunhong Jiang64672c92016-06-13 14:19:59 -07001053 /* apic deadline value in host tsc */
1054 u64 hv_deadline_tsc;
1055
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001056 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001057
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001058 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001059
Wanpeng Li74c55932017-11-29 01:31:20 -08001060 unsigned long host_debugctlmsr;
1061
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001062 /*
1063 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1064 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1065 * in msr_ia32_feature_control_valid_bits.
1066 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001067 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001068 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001069 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001070};
1071
Avi Kivity2fb92db2011-04-27 19:42:18 +03001072enum segment_cache_field {
1073 SEG_FIELD_SEL = 0,
1074 SEG_FIELD_BASE = 1,
1075 SEG_FIELD_LIMIT = 2,
1076 SEG_FIELD_AR = 3,
1077
1078 SEG_FIELD_NR = 4
1079};
1080
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001081static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1082{
1083 return container_of(kvm, struct kvm_vmx, kvm);
1084}
1085
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001086static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1087{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001088 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001089}
1090
Feng Wuefc64402015-09-18 22:29:51 +08001091static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1092{
1093 return &(to_vmx(vcpu)->pi_desc);
1094}
1095
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001096#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001097#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001098#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1099#define FIELD64(number, name) \
1100 FIELD(number, name), \
1101 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001102
Abel Gordon4607c2d2013-04-18 14:35:55 +03001103
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001104static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001105#define SHADOW_FIELD_RO(x) x,
1106#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001107};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001108static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001109 ARRAY_SIZE(shadow_read_only_fields);
1110
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001111static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001112#define SHADOW_FIELD_RW(x) x,
1113#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001114};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001115static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001116 ARRAY_SIZE(shadow_read_write_fields);
1117
Mathias Krause772e0312012-08-30 01:30:19 +02001118static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001119 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001120 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001121 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1122 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1123 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1124 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1125 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1126 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1127 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1128 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001129 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001130 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001131 FIELD(HOST_ES_SELECTOR, host_es_selector),
1132 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1133 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1134 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1135 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1136 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1137 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1138 FIELD64(IO_BITMAP_A, io_bitmap_a),
1139 FIELD64(IO_BITMAP_B, io_bitmap_b),
1140 FIELD64(MSR_BITMAP, msr_bitmap),
1141 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1142 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1143 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001144 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001145 FIELD64(TSC_OFFSET, tsc_offset),
1146 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1147 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001148 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001149 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001150 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001151 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1152 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1153 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1154 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001155 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001156 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1157 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001158 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001159 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1160 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1161 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1162 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1163 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1164 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1165 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1166 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1167 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1168 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001169 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001170 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1171 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1172 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1173 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1174 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1175 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1176 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1177 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1178 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1179 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1180 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1181 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1182 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1183 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1184 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1185 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1186 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1187 FIELD(TPR_THRESHOLD, tpr_threshold),
1188 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1189 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1190 FIELD(VM_EXIT_REASON, vm_exit_reason),
1191 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1192 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1193 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1194 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1195 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1196 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1197 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1198 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1199 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1200 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1201 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1202 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1203 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1204 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1205 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1206 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1207 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1208 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1209 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1210 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1211 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1212 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1213 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1214 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1215 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1216 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1217 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1218 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001219 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001220 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1221 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1222 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1223 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1224 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1225 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1226 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1227 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1228 FIELD(EXIT_QUALIFICATION, exit_qualification),
1229 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1230 FIELD(GUEST_CR0, guest_cr0),
1231 FIELD(GUEST_CR3, guest_cr3),
1232 FIELD(GUEST_CR4, guest_cr4),
1233 FIELD(GUEST_ES_BASE, guest_es_base),
1234 FIELD(GUEST_CS_BASE, guest_cs_base),
1235 FIELD(GUEST_SS_BASE, guest_ss_base),
1236 FIELD(GUEST_DS_BASE, guest_ds_base),
1237 FIELD(GUEST_FS_BASE, guest_fs_base),
1238 FIELD(GUEST_GS_BASE, guest_gs_base),
1239 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1240 FIELD(GUEST_TR_BASE, guest_tr_base),
1241 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1242 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1243 FIELD(GUEST_DR7, guest_dr7),
1244 FIELD(GUEST_RSP, guest_rsp),
1245 FIELD(GUEST_RIP, guest_rip),
1246 FIELD(GUEST_RFLAGS, guest_rflags),
1247 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1248 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1249 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1250 FIELD(HOST_CR0, host_cr0),
1251 FIELD(HOST_CR3, host_cr3),
1252 FIELD(HOST_CR4, host_cr4),
1253 FIELD(HOST_FS_BASE, host_fs_base),
1254 FIELD(HOST_GS_BASE, host_gs_base),
1255 FIELD(HOST_TR_BASE, host_tr_base),
1256 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1257 FIELD(HOST_IDTR_BASE, host_idtr_base),
1258 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1259 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1260 FIELD(HOST_RSP, host_rsp),
1261 FIELD(HOST_RIP, host_rip),
1262};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001263
1264static inline short vmcs_field_to_offset(unsigned long field)
1265{
Dan Williams085331d2018-01-31 17:47:03 -08001266 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1267 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001268 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001269
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001270 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001271 return -ENOENT;
1272
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001273 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001274 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001275 return -ENOENT;
1276
Linus Torvalds15303ba2018-02-10 13:16:35 -08001277 index = array_index_nospec(index, size);
1278 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001279 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001280 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001281 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001282}
1283
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001284static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1285{
David Matlack4f2777b2016-07-13 17:16:37 -07001286 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001287}
1288
Liran Alon61ada742018-06-23 02:35:08 +03001289static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1290{
1291 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1292}
1293
Peter Feiner995f00a2017-06-30 17:26:32 -07001294static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001295static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001296static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001297static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001298static void vmx_set_segment(struct kvm_vcpu *vcpu,
1299 struct kvm_segment *var, int seg);
1300static void vmx_get_segment(struct kvm_vcpu *vcpu,
1301 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001302static bool guest_state_valid(struct kvm_vcpu *vcpu);
1303static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001304static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001305static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1306static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1307static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1308 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001309static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001310static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1311 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001312
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1314static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001315/*
1316 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1317 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1318 */
1319static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320
Feng Wubf9f6ac2015-09-18 22:29:55 +08001321/*
1322 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1323 * can find which vCPU should be waken up.
1324 */
1325static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1326static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1327
Radim Krčmář23611332016-09-29 22:41:33 +02001328enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001329 VMX_VMREAD_BITMAP,
1330 VMX_VMWRITE_BITMAP,
1331 VMX_BITMAP_NR
1332};
1333
1334static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1335
Radim Krčmář23611332016-09-29 22:41:33 +02001336#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1337#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001338
Avi Kivity110312c2010-12-21 12:54:20 +02001339static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001340static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001341
Sheng Yang2384d2b2008-01-17 15:14:33 +08001342static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1343static DEFINE_SPINLOCK(vmx_vpid_lock);
1344
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001345static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 int size;
1347 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001348 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001350 u32 pin_based_exec_ctrl;
1351 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001352 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001353 u32 vmexit_ctrl;
1354 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001355 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001356} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357
Hannes Ederefff9e52008-11-28 17:02:06 +01001358static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001359 u32 ept;
1360 u32 vpid;
1361} vmx_capability;
1362
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363#define VMX_SEGMENT_FIELD(seg) \
1364 [VCPU_SREG_##seg] = { \
1365 .selector = GUEST_##seg##_SELECTOR, \
1366 .base = GUEST_##seg##_BASE, \
1367 .limit = GUEST_##seg##_LIMIT, \
1368 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1369 }
1370
Mathias Krause772e0312012-08-30 01:30:19 +02001371static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 unsigned selector;
1373 unsigned base;
1374 unsigned limit;
1375 unsigned ar_bytes;
1376} kvm_vmx_segment_fields[] = {
1377 VMX_SEGMENT_FIELD(CS),
1378 VMX_SEGMENT_FIELD(DS),
1379 VMX_SEGMENT_FIELD(ES),
1380 VMX_SEGMENT_FIELD(FS),
1381 VMX_SEGMENT_FIELD(GS),
1382 VMX_SEGMENT_FIELD(SS),
1383 VMX_SEGMENT_FIELD(TR),
1384 VMX_SEGMENT_FIELD(LDTR),
1385};
1386
Avi Kivity26bb0982009-09-07 11:14:12 +03001387static u64 host_efer;
1388
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001389static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1390
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001391/*
Brian Gerst8c065852010-07-17 09:03:26 -04001392 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001393 * away by decrementing the array size.
1394 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001396#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001397 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001399 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001402DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1403
1404#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1405
1406#define KVM_EVMCS_VERSION 1
1407
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001408/*
1409 * Enlightened VMCSv1 doesn't support these:
1410 *
1411 * POSTED_INTR_NV = 0x00000002,
1412 * GUEST_INTR_STATUS = 0x00000810,
1413 * APIC_ACCESS_ADDR = 0x00002014,
1414 * POSTED_INTR_DESC_ADDR = 0x00002016,
1415 * EOI_EXIT_BITMAP0 = 0x0000201c,
1416 * EOI_EXIT_BITMAP1 = 0x0000201e,
1417 * EOI_EXIT_BITMAP2 = 0x00002020,
1418 * EOI_EXIT_BITMAP3 = 0x00002022,
1419 * GUEST_PML_INDEX = 0x00000812,
1420 * PML_ADDRESS = 0x0000200e,
1421 * VM_FUNCTION_CONTROL = 0x00002018,
1422 * EPTP_LIST_ADDRESS = 0x00002024,
1423 * VMREAD_BITMAP = 0x00002026,
1424 * VMWRITE_BITMAP = 0x00002028,
1425 *
1426 * TSC_MULTIPLIER = 0x00002032,
1427 * PLE_GAP = 0x00004020,
1428 * PLE_WINDOW = 0x00004022,
1429 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1430 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1431 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1432 *
1433 * Currently unsupported in KVM:
1434 * GUEST_IA32_RTIT_CTL = 0x00002814,
1435 */
1436#define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
1437 PIN_BASED_VMX_PREEMPTION_TIMER)
1438#define EVMCS1_UNSUPPORTED_2NDEXEC \
1439 (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
1440 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
1441 SECONDARY_EXEC_APIC_REGISTER_VIRT | \
1442 SECONDARY_EXEC_ENABLE_PML | \
1443 SECONDARY_EXEC_ENABLE_VMFUNC | \
1444 SECONDARY_EXEC_SHADOW_VMCS | \
1445 SECONDARY_EXEC_TSC_SCALING | \
1446 SECONDARY_EXEC_PAUSE_LOOP_EXITING)
1447#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
1448#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
1449#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
1450
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001451#if IS_ENABLED(CONFIG_HYPERV)
1452static bool __read_mostly enlightened_vmcs = true;
1453module_param(enlightened_vmcs, bool, 0444);
1454
1455static inline void evmcs_write64(unsigned long field, u64 value)
1456{
1457 u16 clean_field;
1458 int offset = get_evmcs_offset(field, &clean_field);
1459
1460 if (offset < 0)
1461 return;
1462
1463 *(u64 *)((char *)current_evmcs + offset) = value;
1464
1465 current_evmcs->hv_clean_fields &= ~clean_field;
1466}
1467
1468static inline void evmcs_write32(unsigned long field, u32 value)
1469{
1470 u16 clean_field;
1471 int offset = get_evmcs_offset(field, &clean_field);
1472
1473 if (offset < 0)
1474 return;
1475
1476 *(u32 *)((char *)current_evmcs + offset) = value;
1477 current_evmcs->hv_clean_fields &= ~clean_field;
1478}
1479
1480static inline void evmcs_write16(unsigned long field, u16 value)
1481{
1482 u16 clean_field;
1483 int offset = get_evmcs_offset(field, &clean_field);
1484
1485 if (offset < 0)
1486 return;
1487
1488 *(u16 *)((char *)current_evmcs + offset) = value;
1489 current_evmcs->hv_clean_fields &= ~clean_field;
1490}
1491
1492static inline u64 evmcs_read64(unsigned long field)
1493{
1494 int offset = get_evmcs_offset(field, NULL);
1495
1496 if (offset < 0)
1497 return 0;
1498
1499 return *(u64 *)((char *)current_evmcs + offset);
1500}
1501
1502static inline u32 evmcs_read32(unsigned long field)
1503{
1504 int offset = get_evmcs_offset(field, NULL);
1505
1506 if (offset < 0)
1507 return 0;
1508
1509 return *(u32 *)((char *)current_evmcs + offset);
1510}
1511
1512static inline u16 evmcs_read16(unsigned long field)
1513{
1514 int offset = get_evmcs_offset(field, NULL);
1515
1516 if (offset < 0)
1517 return 0;
1518
1519 return *(u16 *)((char *)current_evmcs + offset);
1520}
1521
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001522static inline void evmcs_touch_msr_bitmap(void)
1523{
1524 if (unlikely(!current_evmcs))
1525 return;
1526
1527 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1528 current_evmcs->hv_clean_fields &=
1529 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1530}
1531
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001532static void evmcs_load(u64 phys_addr)
1533{
1534 struct hv_vp_assist_page *vp_ap =
1535 hv_get_vp_assist_page(smp_processor_id());
1536
1537 vp_ap->current_nested_vmcs = phys_addr;
1538 vp_ap->enlighten_vmentry = 1;
1539}
1540
1541static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1542{
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001543 vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1544 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001545
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001546 vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1547 vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001548
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001549}
Tianyu Lan877ad952018-07-19 08:40:23 +00001550
1551/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1552static void check_ept_pointer_match(struct kvm *kvm)
1553{
1554 struct kvm_vcpu *vcpu;
1555 u64 tmp_eptp = INVALID_PAGE;
1556 int i;
1557
1558 kvm_for_each_vcpu(i, vcpu, kvm) {
1559 if (!VALID_PAGE(tmp_eptp)) {
1560 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1561 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1562 to_kvm_vmx(kvm)->ept_pointers_match
1563 = EPT_POINTERS_MISMATCH;
1564 return;
1565 }
1566 }
1567
1568 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1569}
1570
1571static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1572{
Lan Tianyua5c214d2018-10-13 22:54:05 +08001573 struct kvm_vcpu *vcpu;
1574 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +00001575
1576 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1577
1578 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1579 check_ept_pointer_match(kvm);
1580
Vitaly Kuznetsov5f8bb002018-10-11 12:03:12 +02001581 /*
1582 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
1583 * base of EPT PML4 table, strip off EPT configuration information.
1584 */
Tianyu Lan877ad952018-07-19 08:40:23 +00001585 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +08001586 kvm_for_each_vcpu(i, vcpu, kvm)
1587 ret |= hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -07001588 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
Lan Tianyua5c214d2018-10-13 22:54:05 +08001589 } else {
1590 ret = hyperv_flush_guest_mapping(
Linus Torvalds0d1e8b82018-10-25 17:57:35 -07001591 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
Tianyu Lan877ad952018-07-19 08:40:23 +00001592 }
Tianyu Lan877ad952018-07-19 08:40:23 +00001593
Tianyu Lan877ad952018-07-19 08:40:23 +00001594 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1595 return ret;
1596}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001597#else /* !IS_ENABLED(CONFIG_HYPERV) */
1598static inline void evmcs_write64(unsigned long field, u64 value) {}
1599static inline void evmcs_write32(unsigned long field, u32 value) {}
1600static inline void evmcs_write16(unsigned long field, u16 value) {}
1601static inline u64 evmcs_read64(unsigned long field) { return 0; }
1602static inline u32 evmcs_read32(unsigned long field) { return 0; }
1603static inline u16 evmcs_read16(unsigned long field) { return 0; }
1604static inline void evmcs_load(u64 phys_addr) {}
1605static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001606static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001607#endif /* IS_ENABLED(CONFIG_HYPERV) */
1608
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001609static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
1610 uint16_t *vmcs_version)
1611{
1612 struct vcpu_vmx *vmx = to_vmx(vcpu);
1613
1614 /* We don't support disabling the feature for simplicity. */
1615 if (vmx->nested.enlightened_vmcs_enabled)
1616 return 0;
1617
1618 vmx->nested.enlightened_vmcs_enabled = true;
1619
1620 /*
1621 * vmcs_version represents the range of supported Enlightened VMCS
1622 * versions: lower 8 bits is the minimal version, higher 8 bits is the
1623 * maximum supported version. KVM supports versions from 1 to
1624 * KVM_EVMCS_VERSION.
1625 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02001626 if (vmcs_version)
1627 *vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001628
1629 vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1630 vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
1631 vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1632 vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
1633 vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC;
1634
1635 return 0;
1636}
1637
Jan Kiszka5bb16012016-02-09 20:14:21 +01001638static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639{
1640 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1641 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001642 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1643}
1644
Jan Kiszka6f054852016-02-09 20:15:18 +01001645static inline bool is_debug(u32 intr_info)
1646{
1647 return is_exception_n(intr_info, DB_VECTOR);
1648}
1649
1650static inline bool is_breakpoint(u32 intr_info)
1651{
1652 return is_exception_n(intr_info, BP_VECTOR);
1653}
1654
Jan Kiszka5bb16012016-02-09 20:14:21 +01001655static inline bool is_page_fault(u32 intr_info)
1656{
1657 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658}
1659
Gui Jianfeng31299942010-03-15 17:29:09 +08001660static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001661{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001662 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001663}
1664
Liran Alon9e869482018-03-12 13:12:51 +02001665static inline bool is_gp_fault(u32 intr_info)
1666{
1667 return is_exception_n(intr_info, GP_VECTOR);
1668}
1669
Gui Jianfeng31299942010-03-15 17:29:09 +08001670static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001671{
1672 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1673 INTR_INFO_VALID_MASK)) ==
1674 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1675}
1676
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001677/* Undocumented: icebp/int1 */
1678static inline bool is_icebp(u32 intr_info)
1679{
1680 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1681 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1682}
1683
Gui Jianfeng31299942010-03-15 17:29:09 +08001684static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001685{
Sheng Yang04547152009-04-01 15:52:31 +08001686 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001687}
1688
Gui Jianfeng31299942010-03-15 17:29:09 +08001689static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001690{
Sheng Yang04547152009-04-01 15:52:31 +08001691 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001692}
1693
Paolo Bonzini35754c92015-07-29 12:05:37 +02001694static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001695{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001696 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001697}
1698
Gui Jianfeng31299942010-03-15 17:29:09 +08001699static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001700{
Sheng Yang04547152009-04-01 15:52:31 +08001701 return vmcs_config.cpu_based_exec_ctrl &
1702 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001703}
1704
Avi Kivity774ead32007-12-26 13:57:04 +02001705static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001706{
Sheng Yang04547152009-04-01 15:52:31 +08001707 return vmcs_config.cpu_based_2nd_exec_ctrl &
1708 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1709}
1710
Yang Zhang8d146952013-01-25 10:18:50 +08001711static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1712{
1713 return vmcs_config.cpu_based_2nd_exec_ctrl &
1714 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1715}
1716
Yang Zhang83d4c282013-01-25 10:18:49 +08001717static inline bool cpu_has_vmx_apic_register_virt(void)
1718{
1719 return vmcs_config.cpu_based_2nd_exec_ctrl &
1720 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1721}
1722
Yang Zhangc7c9c562013-01-25 10:18:51 +08001723static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1724{
1725 return vmcs_config.cpu_based_2nd_exec_ctrl &
1726 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1727}
1728
Sean Christopherson0b665d32018-08-14 09:33:34 -07001729static inline bool cpu_has_vmx_encls_vmexit(void)
1730{
1731 return vmcs_config.cpu_based_2nd_exec_ctrl &
1732 SECONDARY_EXEC_ENCLS_EXITING;
1733}
1734
Yunhong Jiang64672c92016-06-13 14:19:59 -07001735/*
1736 * Comment's format: document - errata name - stepping - processor name.
1737 * Refer from
1738 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1739 */
1740static u32 vmx_preemption_cpu_tfms[] = {
1741/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17420x000206E6,
1743/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1744/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1745/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17460x00020652,
1747/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17480x00020655,
1749/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1750/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1751/*
1752 * 320767.pdf - AAP86 - B1 -
1753 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1754 */
17550x000106E5,
1756/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17570x000106A0,
1758/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17590x000106A1,
1760/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17610x000106A4,
1762 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1763 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1764 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17650x000106A5,
1766};
1767
1768static inline bool cpu_has_broken_vmx_preemption_timer(void)
1769{
1770 u32 eax = cpuid_eax(0x00000001), i;
1771
1772 /* Clear the reserved bits */
1773 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001774 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001775 if (eax == vmx_preemption_cpu_tfms[i])
1776 return true;
1777
1778 return false;
1779}
1780
1781static inline bool cpu_has_vmx_preemption_timer(void)
1782{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001783 return vmcs_config.pin_based_exec_ctrl &
1784 PIN_BASED_VMX_PREEMPTION_TIMER;
1785}
1786
Yang Zhang01e439b2013-04-11 19:25:12 +08001787static inline bool cpu_has_vmx_posted_intr(void)
1788{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001789 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1790 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001791}
1792
1793static inline bool cpu_has_vmx_apicv(void)
1794{
1795 return cpu_has_vmx_apic_register_virt() &&
1796 cpu_has_vmx_virtual_intr_delivery() &&
1797 cpu_has_vmx_posted_intr();
1798}
1799
Sheng Yang04547152009-04-01 15:52:31 +08001800static inline bool cpu_has_vmx_flexpriority(void)
1801{
1802 return cpu_has_vmx_tpr_shadow() &&
1803 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001804}
1805
Marcelo Tosattie7997942009-06-11 12:07:40 -03001806static inline bool cpu_has_vmx_ept_execute_only(void)
1807{
Gui Jianfeng31299942010-03-15 17:29:09 +08001808 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001809}
1810
Marcelo Tosattie7997942009-06-11 12:07:40 -03001811static inline bool cpu_has_vmx_ept_2m_page(void)
1812{
Gui Jianfeng31299942010-03-15 17:29:09 +08001813 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001814}
1815
Sheng Yang878403b2010-01-05 19:02:29 +08001816static inline bool cpu_has_vmx_ept_1g_page(void)
1817{
Gui Jianfeng31299942010-03-15 17:29:09 +08001818 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001819}
1820
Sheng Yang4bc9b982010-06-02 14:05:24 +08001821static inline bool cpu_has_vmx_ept_4levels(void)
1822{
1823 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1824}
1825
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001826static inline bool cpu_has_vmx_ept_mt_wb(void)
1827{
1828 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1829}
1830
Yu Zhang855feb62017-08-24 20:27:55 +08001831static inline bool cpu_has_vmx_ept_5levels(void)
1832{
1833 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1834}
1835
Xudong Hao83c3a332012-05-28 19:33:35 +08001836static inline bool cpu_has_vmx_ept_ad_bits(void)
1837{
1838 return vmx_capability.ept & VMX_EPT_AD_BIT;
1839}
1840
Gui Jianfeng31299942010-03-15 17:29:09 +08001841static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001842{
Gui Jianfeng31299942010-03-15 17:29:09 +08001843 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001844}
1845
Gui Jianfeng31299942010-03-15 17:29:09 +08001846static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001847{
Gui Jianfeng31299942010-03-15 17:29:09 +08001848 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001849}
1850
Liran Aloncd9a4912018-05-22 17:16:15 +03001851static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1852{
1853 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1854}
1855
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001856static inline bool cpu_has_vmx_invvpid_single(void)
1857{
1858 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1859}
1860
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001861static inline bool cpu_has_vmx_invvpid_global(void)
1862{
1863 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1864}
1865
Wanpeng Li08d839c2017-03-23 05:30:08 -07001866static inline bool cpu_has_vmx_invvpid(void)
1867{
1868 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1869}
1870
Gui Jianfeng31299942010-03-15 17:29:09 +08001871static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001872{
Sheng Yang04547152009-04-01 15:52:31 +08001873 return vmcs_config.cpu_based_2nd_exec_ctrl &
1874 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001875}
1876
Gui Jianfeng31299942010-03-15 17:29:09 +08001877static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001878{
1879 return vmcs_config.cpu_based_2nd_exec_ctrl &
1880 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1881}
1882
Gui Jianfeng31299942010-03-15 17:29:09 +08001883static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001884{
1885 return vmcs_config.cpu_based_2nd_exec_ctrl &
1886 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1887}
1888
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001889static inline bool cpu_has_vmx_basic_inout(void)
1890{
1891 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1892}
1893
Paolo Bonzini35754c92015-07-29 12:05:37 +02001894static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001895{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001896 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001897}
1898
Gui Jianfeng31299942010-03-15 17:29:09 +08001899static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001900{
Sheng Yang04547152009-04-01 15:52:31 +08001901 return vmcs_config.cpu_based_2nd_exec_ctrl &
1902 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001903}
1904
Gui Jianfeng31299942010-03-15 17:29:09 +08001905static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001906{
1907 return vmcs_config.cpu_based_2nd_exec_ctrl &
1908 SECONDARY_EXEC_RDTSCP;
1909}
1910
Mao, Junjiead756a12012-07-02 01:18:48 +00001911static inline bool cpu_has_vmx_invpcid(void)
1912{
1913 return vmcs_config.cpu_based_2nd_exec_ctrl &
1914 SECONDARY_EXEC_ENABLE_INVPCID;
1915}
1916
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001917static inline bool cpu_has_virtual_nmis(void)
1918{
1919 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1920}
1921
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001922static inline bool cpu_has_vmx_wbinvd_exit(void)
1923{
1924 return vmcs_config.cpu_based_2nd_exec_ctrl &
1925 SECONDARY_EXEC_WBINVD_EXITING;
1926}
1927
Abel Gordonabc4fc52013-04-18 14:35:25 +03001928static inline bool cpu_has_vmx_shadow_vmcs(void)
1929{
1930 u64 vmx_msr;
1931 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1932 /* check if the cpu supports writing r/o exit information fields */
1933 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1934 return false;
1935
1936 return vmcs_config.cpu_based_2nd_exec_ctrl &
1937 SECONDARY_EXEC_SHADOW_VMCS;
1938}
1939
Kai Huang843e4332015-01-28 10:54:28 +08001940static inline bool cpu_has_vmx_pml(void)
1941{
1942 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1943}
1944
Haozhong Zhang64903d62015-10-20 15:39:09 +08001945static inline bool cpu_has_vmx_tsc_scaling(void)
1946{
1947 return vmcs_config.cpu_based_2nd_exec_ctrl &
1948 SECONDARY_EXEC_TSC_SCALING;
1949}
1950
Bandan Das2a499e42017-08-03 15:54:41 -04001951static inline bool cpu_has_vmx_vmfunc(void)
1952{
1953 return vmcs_config.cpu_based_2nd_exec_ctrl &
1954 SECONDARY_EXEC_ENABLE_VMFUNC;
1955}
1956
Sean Christopherson64f7a112018-04-30 10:01:06 -07001957static bool vmx_umip_emulated(void)
1958{
1959 return vmcs_config.cpu_based_2nd_exec_ctrl &
1960 SECONDARY_EXEC_DESC;
1961}
1962
Sheng Yang04547152009-04-01 15:52:31 +08001963static inline bool report_flexpriority(void)
1964{
1965 return flexpriority_enabled;
1966}
1967
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001968static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1969{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001970 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001971}
1972
Jim Mattsonf4160e42018-05-29 09:11:33 -07001973/*
1974 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1975 * to modify any valid field of the VMCS, or are the VM-exit
1976 * information fields read-only?
1977 */
1978static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1979{
1980 return to_vmx(vcpu)->nested.msrs.misc_low &
1981 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1982}
1983
Marc Orr04473782018-06-20 17:21:29 -07001984static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1985{
1986 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1987}
1988
1989static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1990{
1991 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1992 CPU_BASED_MONITOR_TRAP_FLAG;
1993}
1994
Liran Alonfa97d7d2018-07-18 14:07:59 +02001995static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1996{
1997 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1998 SECONDARY_EXEC_SHADOW_VMCS;
1999}
2000
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002001static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
2002{
2003 return vmcs12->cpu_based_vm_exec_control & bit;
2004}
2005
2006static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
2007{
2008 return (vmcs12->cpu_based_vm_exec_control &
2009 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2010 (vmcs12->secondary_vm_exec_control & bit);
2011}
2012
Jan Kiszkaf41245002014-03-07 20:03:13 +01002013static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
2014{
2015 return vmcs12->pin_based_vm_exec_control &
2016 PIN_BASED_VMX_PREEMPTION_TIMER;
2017}
2018
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05002019static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
2020{
2021 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
2022}
2023
2024static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
2025{
2026 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
2027}
2028
Nadav Har'El155a97a2013-08-05 11:07:16 +03002029static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
2030{
2031 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
2032}
2033
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002034static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2035{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002036 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002037}
2038
Bandan Dasc5f983f2017-05-05 15:25:14 -04002039static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2040{
2041 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2042}
2043
Wincy Vanf2b93282015-02-03 23:56:03 +08002044static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2045{
2046 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2047}
2048
Wanpeng Li5c614b32015-10-13 09:18:36 -07002049static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2050{
2051 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2052}
2053
Wincy Van82f0dd42015-02-03 23:57:18 +08002054static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2055{
2056 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2057}
2058
Wincy Van608406e2015-02-03 23:57:51 +08002059static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2060{
2061 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2062}
2063
Wincy Van705699a2015-02-03 23:58:17 +08002064static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2065{
2066 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2067}
2068
Bandan Das27c42a12017-08-03 15:54:42 -04002069static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2070{
2071 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2072}
2073
Bandan Das41ab9372017-08-03 15:54:43 -04002074static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2075{
2076 return nested_cpu_has_vmfunc(vmcs12) &&
2077 (vmcs12->vm_function_control &
2078 VMX_VMFUNC_EPTP_SWITCHING);
2079}
2080
Liran Alonf792d272018-06-23 02:35:05 +03002081static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2082{
2083 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2084}
2085
Jim Mattsonef85b672016-12-12 11:01:37 -08002086static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002087{
2088 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002089 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002090}
2091
Jan Kiszka533558b2014-01-04 18:47:20 +01002092static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2093 u32 exit_intr_info,
2094 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002095
Rusty Russell8b9cf982007-07-30 16:31:43 +10002096static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002097{
2098 int i;
2099
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002100 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002101 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002102 return i;
2103 return -1;
2104}
2105
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002106static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002107{
2108 struct {
2109 u64 vpid : 16;
2110 u64 rsvd : 48;
2111 u64 gva;
2112 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002113 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002114
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002115 asm volatile (__ex("invvpid %2, %1") CC_SET(na)
2116 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002117 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002118}
2119
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002120static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08002121{
2122 struct {
2123 u64 eptp, gpa;
2124 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002125 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002126
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002127 asm volatile (__ex("invept %2, %1") CC_SET(na)
2128 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002129 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002130}
2131
Avi Kivity26bb0982009-09-07 11:14:12 +03002132static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002133{
2134 int i;
2135
Rusty Russell8b9cf982007-07-30 16:31:43 +10002136 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002137 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002138 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002139 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002140}
2141
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142static void vmcs_clear(struct vmcs *vmcs)
2143{
2144 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002145 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002147 asm volatile (__ex("vmclear %1") CC_SET(na)
2148 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002149 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2151 vmcs, phys_addr);
2152}
2153
Nadav Har'Eld462b812011-05-24 15:26:10 +03002154static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2155{
2156 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002157 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2158 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002159 loaded_vmcs->cpu = -1;
2160 loaded_vmcs->launched = 0;
2161}
2162
Dongxiao Xu7725b892010-05-11 18:29:38 +08002163static void vmcs_load(struct vmcs *vmcs)
2164{
2165 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002166 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002167
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002168 if (static_branch_unlikely(&enable_evmcs))
2169 return evmcs_load(phys_addr);
2170
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002171 asm volatile (__ex("vmptrld %1") CC_SET(na)
2172 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002173 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002174 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002175 vmcs, phys_addr);
2176}
2177
Dave Young2965faa2015-09-09 15:38:55 -07002178#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002179/*
2180 * This bitmap is used to indicate whether the vmclear
2181 * operation is enabled on all cpus. All disabled by
2182 * default.
2183 */
2184static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2185
2186static inline void crash_enable_local_vmclear(int cpu)
2187{
2188 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2189}
2190
2191static inline void crash_disable_local_vmclear(int cpu)
2192{
2193 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2194}
2195
2196static inline int crash_local_vmclear_enabled(int cpu)
2197{
2198 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2199}
2200
2201static void crash_vmclear_local_loaded_vmcss(void)
2202{
2203 int cpu = raw_smp_processor_id();
2204 struct loaded_vmcs *v;
2205
2206 if (!crash_local_vmclear_enabled(cpu))
2207 return;
2208
2209 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2210 loaded_vmcss_on_cpu_link)
2211 vmcs_clear(v->vmcs);
2212}
2213#else
2214static inline void crash_enable_local_vmclear(int cpu) { }
2215static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002216#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002217
Nadav Har'Eld462b812011-05-24 15:26:10 +03002218static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002220 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002221 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222
Nadav Har'Eld462b812011-05-24 15:26:10 +03002223 if (loaded_vmcs->cpu != cpu)
2224 return; /* vcpu migration can race with cpu offline */
2225 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002227 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002228 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002229
2230 /*
2231 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2232 * is before setting loaded_vmcs->vcpu to -1 which is done in
2233 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2234 * then adds the vmcs into percpu list before it is deleted.
2235 */
2236 smp_wmb();
2237
Nadav Har'Eld462b812011-05-24 15:26:10 +03002238 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002239 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240}
2241
Nadav Har'Eld462b812011-05-24 15:26:10 +03002242static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002243{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002244 int cpu = loaded_vmcs->cpu;
2245
2246 if (cpu != -1)
2247 smp_call_function_single(cpu,
2248 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002249}
2250
Junaid Shahidfaff8752018-06-29 13:10:05 -07002251static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2252{
2253 if (vpid == 0)
2254 return true;
2255
2256 if (cpu_has_vmx_invvpid_individual_addr()) {
2257 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2258 return true;
2259 }
2260
2261 return false;
2262}
2263
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002264static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002265{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002266 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002267 return;
2268
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002269 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002270 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002271}
2272
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002273static inline void vpid_sync_vcpu_global(void)
2274{
2275 if (cpu_has_vmx_invvpid_global())
2276 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2277}
2278
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002279static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002280{
2281 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002282 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002283 else
2284 vpid_sync_vcpu_global();
2285}
2286
Sheng Yang14394422008-04-28 12:24:45 +08002287static inline void ept_sync_global(void)
2288{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002289 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002290}
2291
2292static inline void ept_sync_context(u64 eptp)
2293{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002294 if (cpu_has_vmx_invept_context())
2295 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2296 else
2297 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002298}
2299
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002300static __always_inline void vmcs_check16(unsigned long field)
2301{
2302 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2303 "16-bit accessor invalid for 64-bit field");
2304 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2305 "16-bit accessor invalid for 64-bit high field");
2306 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2307 "16-bit accessor invalid for 32-bit high field");
2308 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2309 "16-bit accessor invalid for natural width field");
2310}
2311
2312static __always_inline void vmcs_check32(unsigned long field)
2313{
2314 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2315 "32-bit accessor invalid for 16-bit field");
2316 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2317 "32-bit accessor invalid for natural width field");
2318}
2319
2320static __always_inline void vmcs_check64(unsigned long field)
2321{
2322 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2323 "64-bit accessor invalid for 16-bit field");
2324 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2325 "64-bit accessor invalid for 64-bit high field");
2326 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2327 "64-bit accessor invalid for 32-bit field");
2328 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2329 "64-bit accessor invalid for natural width field");
2330}
2331
2332static __always_inline void vmcs_checkl(unsigned long field)
2333{
2334 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2335 "Natural width accessor invalid for 16-bit field");
2336 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2337 "Natural width accessor invalid for 64-bit field");
2338 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2339 "Natural width accessor invalid for 64-bit high field");
2340 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2341 "Natural width accessor invalid for 32-bit field");
2342}
2343
2344static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345{
Avi Kivity5e520e62011-05-15 10:13:12 -04002346 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347
Uros Bizjak44c2d662018-10-11 19:40:45 +02002348 asm volatile (__ex_clear("vmread %1, %0", "%k0")
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002349 : "=r"(value) : "r"(field));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350 return value;
2351}
2352
Avi Kivity96304212011-05-15 10:13:13 -04002353static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002355 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002356 if (static_branch_unlikely(&enable_evmcs))
2357 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002358 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359}
2360
Avi Kivity96304212011-05-15 10:13:13 -04002361static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002363 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002364 if (static_branch_unlikely(&enable_evmcs))
2365 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002366 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367}
2368
Avi Kivity96304212011-05-15 10:13:13 -04002369static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002371 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002372 if (static_branch_unlikely(&enable_evmcs))
2373 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002374#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002375 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002377 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378#endif
2379}
2380
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002381static __always_inline unsigned long vmcs_readl(unsigned long field)
2382{
2383 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002384 if (static_branch_unlikely(&enable_evmcs))
2385 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386 return __vmcs_readl(field);
2387}
2388
Avi Kivitye52de1b2007-01-05 16:36:56 -08002389static noinline void vmwrite_error(unsigned long field, unsigned long value)
2390{
2391 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2392 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2393 dump_stack();
2394}
2395
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002396static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002398 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002400 asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
2401 : CC_OUT(na) (error) : "r"(field), "rm"(value));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002402 if (unlikely(error))
2403 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404}
2405
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002406static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002408 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002409 if (static_branch_unlikely(&enable_evmcs))
2410 return evmcs_write16(field, value);
2411
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002412 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413}
2414
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002415static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002417 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002418 if (static_branch_unlikely(&enable_evmcs))
2419 return evmcs_write32(field, value);
2420
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002421 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422}
2423
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002424static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002426 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002427 if (static_branch_unlikely(&enable_evmcs))
2428 return evmcs_write64(field, value);
2429
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002430 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002431#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002433 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434#endif
2435}
2436
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002437static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002438{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002439 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002440 if (static_branch_unlikely(&enable_evmcs))
2441 return evmcs_write64(field, value);
2442
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002443 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002444}
2445
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002446static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002447{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002448 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2449 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002450 if (static_branch_unlikely(&enable_evmcs))
2451 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2452
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002453 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2454}
2455
2456static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2457{
2458 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2459 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002460 if (static_branch_unlikely(&enable_evmcs))
2461 return evmcs_write32(field, evmcs_read32(field) | mask);
2462
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002463 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002464}
2465
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002466static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2467{
2468 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2469}
2470
Gleb Natapov2961e8762013-11-25 15:37:13 +02002471static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2472{
2473 vmcs_write32(VM_ENTRY_CONTROLS, val);
2474 vmx->vm_entry_controls_shadow = val;
2475}
2476
2477static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2478{
2479 if (vmx->vm_entry_controls_shadow != val)
2480 vm_entry_controls_init(vmx, val);
2481}
2482
2483static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2484{
2485 return vmx->vm_entry_controls_shadow;
2486}
2487
2488
2489static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2490{
2491 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2492}
2493
2494static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2495{
2496 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2497}
2498
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002499static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2500{
2501 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2502}
2503
Gleb Natapov2961e8762013-11-25 15:37:13 +02002504static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2505{
2506 vmcs_write32(VM_EXIT_CONTROLS, val);
2507 vmx->vm_exit_controls_shadow = val;
2508}
2509
2510static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2511{
2512 if (vmx->vm_exit_controls_shadow != val)
2513 vm_exit_controls_init(vmx, val);
2514}
2515
2516static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2517{
2518 return vmx->vm_exit_controls_shadow;
2519}
2520
2521
2522static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2523{
2524 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2525}
2526
2527static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2528{
2529 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2530}
2531
Avi Kivity2fb92db2011-04-27 19:42:18 +03002532static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2533{
2534 vmx->segment_cache.bitmask = 0;
2535}
2536
2537static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2538 unsigned field)
2539{
2540 bool ret;
2541 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2542
2543 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2544 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2545 vmx->segment_cache.bitmask = 0;
2546 }
2547 ret = vmx->segment_cache.bitmask & mask;
2548 vmx->segment_cache.bitmask |= mask;
2549 return ret;
2550}
2551
2552static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2553{
2554 u16 *p = &vmx->segment_cache.seg[seg].selector;
2555
2556 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2557 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2558 return *p;
2559}
2560
2561static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2562{
2563 ulong *p = &vmx->segment_cache.seg[seg].base;
2564
2565 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2566 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2567 return *p;
2568}
2569
2570static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2571{
2572 u32 *p = &vmx->segment_cache.seg[seg].limit;
2573
2574 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2575 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2576 return *p;
2577}
2578
2579static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2580{
2581 u32 *p = &vmx->segment_cache.seg[seg].ar;
2582
2583 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2584 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2585 return *p;
2586}
2587
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002588static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2589{
2590 u32 eb;
2591
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002592 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002593 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002594 /*
2595 * Guest access to VMware backdoor ports could legitimately
2596 * trigger #GP because of TSS I/O permission bitmap.
2597 * We intercept those #GP and allow access to them anyway
2598 * as VMware does.
2599 */
2600 if (enable_vmware_backdoor)
2601 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002602 if ((vcpu->guest_debug &
2603 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2604 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2605 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002606 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002607 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002608 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002609 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002610
2611 /* When we are running a nested L2 guest and L1 specified for it a
2612 * certain exception bitmap, we must trap the same exceptions and pass
2613 * them to L1. When running L2, we will only handle the exceptions
2614 * specified above if L1 did not want them.
2615 */
2616 if (is_guest_mode(vcpu))
2617 eb |= get_vmcs12(vcpu)->exception_bitmap;
2618
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002619 vmcs_write32(EXCEPTION_BITMAP, eb);
2620}
2621
Ashok Raj15d45072018-02-01 22:59:43 +01002622/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002623 * Check if MSR is intercepted for currently loaded MSR bitmap.
2624 */
2625static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2626{
2627 unsigned long *msr_bitmap;
2628 int f = sizeof(unsigned long);
2629
2630 if (!cpu_has_vmx_msr_bitmap())
2631 return true;
2632
2633 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2634
2635 if (msr <= 0x1fff) {
2636 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2637 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2638 msr &= 0x1fff;
2639 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2640 }
2641
2642 return true;
2643}
2644
2645/*
Ashok Raj15d45072018-02-01 22:59:43 +01002646 * Check if MSR is intercepted for L01 MSR bitmap.
2647 */
2648static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2649{
2650 unsigned long *msr_bitmap;
2651 int f = sizeof(unsigned long);
2652
2653 if (!cpu_has_vmx_msr_bitmap())
2654 return true;
2655
2656 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2657
2658 if (msr <= 0x1fff) {
2659 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2660 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2661 msr &= 0x1fff;
2662 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2663 }
2664
2665 return true;
2666}
2667
Gleb Natapov2961e8762013-11-25 15:37:13 +02002668static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2669 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002670{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002671 vm_entry_controls_clearbit(vmx, entry);
2672 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002673}
2674
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002675static int find_msr(struct vmx_msrs *m, unsigned int msr)
2676{
2677 unsigned int i;
2678
2679 for (i = 0; i < m->nr; ++i) {
2680 if (m->val[i].index == msr)
2681 return i;
2682 }
2683 return -ENOENT;
2684}
2685
Avi Kivity61d2ef22010-04-28 16:40:38 +03002686static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2687{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002688 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002689 struct msr_autoload *m = &vmx->msr_autoload;
2690
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002691 switch (msr) {
2692 case MSR_EFER:
2693 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002694 clear_atomic_switch_msr_special(vmx,
2695 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002696 VM_EXIT_LOAD_IA32_EFER);
2697 return;
2698 }
2699 break;
2700 case MSR_CORE_PERF_GLOBAL_CTRL:
2701 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002702 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002703 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2704 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2705 return;
2706 }
2707 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002708 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002709 i = find_msr(&m->guest, msr);
2710 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002711 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002712 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002713 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002714 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002715
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002716skip_guest:
2717 i = find_msr(&m->host, msr);
2718 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002719 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002720
2721 --m->host.nr;
2722 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002723 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002724}
2725
Gleb Natapov2961e8762013-11-25 15:37:13 +02002726static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2727 unsigned long entry, unsigned long exit,
2728 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2729 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002730{
2731 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07002732 if (host_val_vmcs != HOST_IA32_EFER)
2733 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002734 vm_entry_controls_setbit(vmx, entry);
2735 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002736}
2737
Avi Kivity61d2ef22010-04-28 16:40:38 +03002738static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002739 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002740{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002741 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002742 struct msr_autoload *m = &vmx->msr_autoload;
2743
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002744 switch (msr) {
2745 case MSR_EFER:
2746 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002747 add_atomic_switch_msr_special(vmx,
2748 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002749 VM_EXIT_LOAD_IA32_EFER,
2750 GUEST_IA32_EFER,
2751 HOST_IA32_EFER,
2752 guest_val, host_val);
2753 return;
2754 }
2755 break;
2756 case MSR_CORE_PERF_GLOBAL_CTRL:
2757 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002758 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002759 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2760 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2761 GUEST_IA32_PERF_GLOBAL_CTRL,
2762 HOST_IA32_PERF_GLOBAL_CTRL,
2763 guest_val, host_val);
2764 return;
2765 }
2766 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002767 case MSR_IA32_PEBS_ENABLE:
2768 /* PEBS needs a quiescent period after being disabled (to write
2769 * a record). Disabling PEBS through VMX MSR swapping doesn't
2770 * provide that period, so a CPU could write host's record into
2771 * guest's memory.
2772 */
2773 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002774 }
2775
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002776 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002777 if (!entry_only)
2778 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002779
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002780 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002781 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002782 "Can't add msr %x\n", msr);
2783 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002784 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002785 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002786 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002787 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002788 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002789 m->guest.val[i].index = msr;
2790 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002791
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002792 if (entry_only)
2793 return;
2794
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002795 if (j < 0) {
2796 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002797 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002798 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002799 m->host.val[j].index = msr;
2800 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002801}
2802
Avi Kivity92c0d902009-10-29 11:00:16 +02002803static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002804{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002805 u64 guest_efer = vmx->vcpu.arch.efer;
2806 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002807
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002808 if (!enable_ept) {
2809 /*
2810 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2811 * host CPUID is more efficient than testing guest CPUID
2812 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2813 */
2814 if (boot_cpu_has(X86_FEATURE_SMEP))
2815 guest_efer |= EFER_NX;
2816 else if (!(guest_efer & EFER_NX))
2817 ignore_bits |= EFER_NX;
2818 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002819
Avi Kivity51c6cf62007-08-29 03:48:05 +03002820 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002821 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002822 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002823 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002824#ifdef CONFIG_X86_64
2825 ignore_bits |= EFER_LMA | EFER_LME;
2826 /* SCE is meaningful only in long mode on Intel */
2827 if (guest_efer & EFER_LMA)
2828 ignore_bits &= ~(u64)EFER_SCE;
2829#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002830
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002831 /*
2832 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2833 * On CPUs that support "load IA32_EFER", always switch EFER
2834 * atomically, since it's faster than switching it manually.
2835 */
2836 if (cpu_has_load_ia32_efer ||
2837 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002838 if (!(guest_efer & EFER_LMA))
2839 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002840 if (guest_efer != host_efer)
2841 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002842 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002843 else
2844 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002845 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002846 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002847 clear_atomic_switch_msr(vmx, MSR_EFER);
2848
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002849 guest_efer &= ~ignore_bits;
2850 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002851
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002852 vmx->guest_msrs[efer_offset].data = guest_efer;
2853 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2854
2855 return true;
2856 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002857}
2858
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002859#ifdef CONFIG_X86_32
2860/*
2861 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2862 * VMCS rather than the segment table. KVM uses this helper to figure
2863 * out the current bases to poke them into the VMCS before entry.
2864 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002865static unsigned long segment_base(u16 selector)
2866{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002867 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002868 unsigned long v;
2869
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002870 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002871 return 0;
2872
Thomas Garnier45fc8752017-03-14 10:05:08 -07002873 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002874
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002875 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002876 u16 ldt_selector = kvm_read_ldt();
2877
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002878 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002879 return 0;
2880
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002881 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002882 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002883 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002884 return v;
2885}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002886#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002887
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002888static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002889{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002890 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002891 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002892#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002893 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002894#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002895 unsigned long fs_base, gs_base;
2896 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002897 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002898
Sean Christophersond264ee02018-08-27 15:21:12 -07002899 vmx->req_immediate_exit = false;
2900
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002901 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002902 return;
2903
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002904 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002905 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002906
Avi Kivity33ed6322007-05-02 16:54:03 +03002907 /*
2908 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2909 * allow segment selectors with cpl > 0 or ti == 1.
2910 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002911 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002912
2913#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002914 savesegment(ds, host_state->ds_sel);
2915 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002916
2917 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002918 if (likely(is_64bit_mm(current->mm))) {
2919 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002920 fs_sel = current->thread.fsindex;
2921 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002922 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002923 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002924 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002925 savesegment(fs, fs_sel);
2926 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002927 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002928 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002929 }
2930
Paolo Bonzini4679b612018-09-24 17:23:01 +02002931 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002932#else
Sean Christophersone368b872018-07-23 12:32:41 -07002933 savesegment(fs, fs_sel);
2934 savesegment(gs, gs_sel);
2935 fs_base = segment_base(fs_sel);
2936 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002937#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002938
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002939 if (unlikely(fs_sel != host_state->fs_sel)) {
2940 if (!(fs_sel & 7))
2941 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2942 else
2943 vmcs_write16(HOST_FS_SELECTOR, 0);
2944 host_state->fs_sel = fs_sel;
2945 }
2946 if (unlikely(gs_sel != host_state->gs_sel)) {
2947 if (!(gs_sel & 7))
2948 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2949 else
2950 vmcs_write16(HOST_GS_SELECTOR, 0);
2951 host_state->gs_sel = gs_sel;
2952 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002953 if (unlikely(fs_base != host_state->fs_base)) {
2954 vmcs_writel(HOST_FS_BASE, fs_base);
2955 host_state->fs_base = fs_base;
2956 }
2957 if (unlikely(gs_base != host_state->gs_base)) {
2958 vmcs_writel(HOST_GS_BASE, gs_base);
2959 host_state->gs_base = gs_base;
2960 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002961
Avi Kivity26bb0982009-09-07 11:14:12 +03002962 for (i = 0; i < vmx->save_nmsrs; ++i)
2963 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002964 vmx->guest_msrs[i].data,
2965 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002966}
2967
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002968static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002969{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002970 struct vmcs_host_state *host_state;
2971
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002972 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002973 return;
2974
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002975 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002976 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002977
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002978 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002979 vmx->loaded_cpu_state = NULL;
2980
Avi Kivityc8770e72010-11-11 12:37:26 +02002981#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002982 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002983#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002984 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2985 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002986#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002987 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002988#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002989 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002990#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002991 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002992 if (host_state->fs_sel & 7)
2993 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002994#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002995 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2996 loadsegment(ds, host_state->ds_sel);
2997 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002998 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002999#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003000 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03003001#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02003002 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03003003#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07003004 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03003005}
3006
Sean Christopherson678e3152018-07-23 12:32:43 -07003007#ifdef CONFIG_X86_64
3008static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03003009{
Paolo Bonzini4679b612018-09-24 17:23:01 +02003010 preempt_disable();
3011 if (vmx->loaded_cpu_state)
3012 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
3013 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003014 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03003015}
3016
Sean Christopherson678e3152018-07-23 12:32:43 -07003017static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
3018{
Paolo Bonzini4679b612018-09-24 17:23:01 +02003019 preempt_disable();
3020 if (vmx->loaded_cpu_state)
3021 wrmsrl(MSR_KERNEL_GS_BASE, data);
3022 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003023 vmx->msr_guest_kernel_gs_base = data;
3024}
3025#endif
3026
Feng Wu28b835d2015-09-18 22:29:54 +08003027static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
3028{
3029 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3030 struct pi_desc old, new;
3031 unsigned int dest;
3032
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003033 /*
3034 * In case of hot-plug or hot-unplug, we may have to undo
3035 * vmx_vcpu_pi_put even if there is no assigned device. And we
3036 * always keep PI.NDST up to date for simplicity: it makes the
3037 * code easier, and CPU migration is not a fast path.
3038 */
3039 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003040 return;
3041
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003042 /*
3043 * First handle the simple case where no cmpxchg is necessary; just
3044 * allow posting non-urgent interrupts.
3045 *
3046 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3047 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3048 * expects the VCPU to be on the blocked_vcpu_list that matches
3049 * PI.NDST.
3050 */
3051 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3052 vcpu->cpu == cpu) {
3053 pi_clear_sn(pi_desc);
3054 return;
3055 }
3056
3057 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003058 do {
3059 old.control = new.control = pi_desc->control;
3060
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003061 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003062
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003063 if (x2apic_enabled())
3064 new.ndst = dest;
3065 else
3066 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003067
Feng Wu28b835d2015-09-18 22:29:54 +08003068 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003069 } while (cmpxchg64(&pi_desc->control, old.control,
3070 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003071}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003072
Peter Feinerc95ba922016-08-17 09:36:47 -07003073static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3074{
3075 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3076 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3077}
3078
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079/*
3080 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3081 * vcpu mutex is already taken.
3082 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003083static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003085 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003086 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003088 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003089 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003090 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003091 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003092
3093 /*
3094 * Read loaded_vmcs->cpu should be before fetching
3095 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3096 * See the comments in __loaded_vmcs_clear().
3097 */
3098 smp_rmb();
3099
Nadav Har'Eld462b812011-05-24 15:26:10 +03003100 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3101 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003102 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003103 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003104 }
3105
3106 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3107 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3108 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003109 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003110 }
3111
3112 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003113 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003114 unsigned long sysenter_esp;
3115
3116 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003117
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 /*
3119 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003120 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003122 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003123 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003124 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003126 /*
3127 * VM exits change the host TR limit to 0x67 after a VM
3128 * exit. This is okay, since 0x67 covers everything except
3129 * the IO bitmap and have have code to handle the IO bitmap
3130 * being lost after a VM exit.
3131 */
3132 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3133
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3135 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003136
Nadav Har'Eld462b812011-05-24 15:26:10 +03003137 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 }
Feng Wu28b835d2015-09-18 22:29:54 +08003139
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003140 /* Setup TSC multiplier */
3141 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003142 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3143 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003144
Feng Wu28b835d2015-09-18 22:29:54 +08003145 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003146 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003147 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003148}
3149
3150static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3151{
3152 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3153
3154 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003155 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3156 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003157 return;
3158
3159 /* Set SN when the vCPU is preempted */
3160 if (vcpu->preempted)
3161 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162}
3163
3164static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3165{
Feng Wu28b835d2015-09-18 22:29:54 +08003166 vmx_vcpu_pi_put(vcpu);
3167
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003168 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169}
3170
Wanpeng Lif244dee2017-07-20 01:11:54 -07003171static bool emulation_required(struct kvm_vcpu *vcpu)
3172{
3173 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3174}
3175
Avi Kivityedcafe32009-12-30 18:07:40 +02003176static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3177
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003178/*
3179 * Return the cr0 value that a nested guest would read. This is a combination
3180 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3181 * its hypervisor (cr0_read_shadow).
3182 */
3183static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3184{
3185 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3186 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3187}
3188static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3189{
3190 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3191 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3192}
3193
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3195{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003196 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003197
Avi Kivity6de12732011-03-07 12:51:22 +02003198 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3199 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3200 rflags = vmcs_readl(GUEST_RFLAGS);
3201 if (to_vmx(vcpu)->rmode.vm86_active) {
3202 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3203 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3204 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3205 }
3206 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003207 }
Avi Kivity6de12732011-03-07 12:51:22 +02003208 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209}
3210
3211static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3212{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003213 unsigned long old_rflags = vmx_get_rflags(vcpu);
3214
Avi Kivity6de12732011-03-07 12:51:22 +02003215 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3216 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003217 if (to_vmx(vcpu)->rmode.vm86_active) {
3218 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003219 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003220 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003222
3223 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3224 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225}
3226
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003227static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003228{
3229 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3230 int ret = 0;
3231
3232 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003233 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003234 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003235 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003236
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003237 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003238}
3239
3240static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3241{
3242 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3243 u32 interruptibility = interruptibility_old;
3244
3245 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3246
Jan Kiszka48005f62010-02-19 19:38:07 +01003247 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003248 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003249 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003250 interruptibility |= GUEST_INTR_STATE_STI;
3251
3252 if ((interruptibility != interruptibility_old))
3253 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3254}
3255
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3257{
3258 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003260 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003262 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263
Glauber Costa2809f5d2009-05-12 16:21:05 -04003264 /* skipping an emulated instruction also counts */
3265 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266}
3267
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003268static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3269 unsigned long exit_qual)
3270{
3271 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3272 unsigned int nr = vcpu->arch.exception.nr;
3273 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3274
3275 if (vcpu->arch.exception.has_error_code) {
3276 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3277 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3278 }
3279
3280 if (kvm_exception_is_soft(nr))
3281 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3282 else
3283 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3284
3285 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3286 vmx_get_nmi_mask(vcpu))
3287 intr_info |= INTR_INFO_UNBLOCK_NMI;
3288
3289 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3290}
3291
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003292/*
3293 * KVM wants to inject page-faults which it got to the guest. This function
3294 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003295 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003296static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003297{
3298 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003299 unsigned int nr = vcpu->arch.exception.nr;
Jim Mattsonda998b42018-10-16 14:29:22 -07003300 bool has_payload = vcpu->arch.exception.has_payload;
3301 unsigned long payload = vcpu->arch.exception.payload;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003302
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003303 if (nr == PF_VECTOR) {
3304 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003305 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003306 return 1;
3307 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003308 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3309 vcpu->arch.exception.error_code)) {
Jim Mattsonda998b42018-10-16 14:29:22 -07003310 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003311 return 1;
3312 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07003313 } else if (vmcs12->exception_bitmap & (1u << nr)) {
3314 if (nr == DB_VECTOR) {
3315 if (!has_payload) {
3316 payload = vcpu->arch.dr6;
3317 payload &= ~(DR6_FIXED_1 | DR6_BT);
3318 payload ^= DR6_RTM;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003319 }
Jim Mattsonf10c7292018-10-16 14:29:23 -07003320 *exit_qual = payload;
3321 } else
3322 *exit_qual = 0;
3323 return 1;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003324 }
3325
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003326 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003327}
3328
Wanpeng Licaa057a2018-03-12 04:53:03 -07003329static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3330{
3331 /*
3332 * Ensure that we clear the HLT state in the VMCS. We don't need to
3333 * explicitly skip the instruction because if the HLT state is set,
3334 * then the instruction is already executing and RIP has already been
3335 * advanced.
3336 */
3337 if (kvm_hlt_in_guest(vcpu->kvm) &&
3338 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3339 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3340}
3341
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003342static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003343{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003344 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003345 unsigned nr = vcpu->arch.exception.nr;
3346 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003347 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003348 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003349
Jim Mattsonda998b42018-10-16 14:29:22 -07003350 kvm_deliver_exception_payload(vcpu);
3351
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003352 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003353 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003354 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3355 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003356
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003357 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003358 int inc_eip = 0;
3359 if (kvm_exception_is_soft(nr))
3360 inc_eip = vcpu->arch.event_exit_inst_len;
3361 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003362 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003363 return;
3364 }
3365
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003366 WARN_ON_ONCE(vmx->emulation_required);
3367
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003368 if (kvm_exception_is_soft(nr)) {
3369 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3370 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003371 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3372 } else
3373 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3374
3375 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003376
3377 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003378}
3379
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003380static bool vmx_rdtscp_supported(void)
3381{
3382 return cpu_has_vmx_rdtscp();
3383}
3384
Mao, Junjiead756a12012-07-02 01:18:48 +00003385static bool vmx_invpcid_supported(void)
3386{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003387 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003388}
3389
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390/*
Eddie Donga75beee2007-05-17 18:55:15 +03003391 * Swap MSR entry in host/guest MSR entry array.
3392 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003393static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003394{
Avi Kivity26bb0982009-09-07 11:14:12 +03003395 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003396
3397 tmp = vmx->guest_msrs[to];
3398 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3399 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003400}
3401
3402/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003403 * Set up the vmcs to automatically save and restore system
3404 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3405 * mode, as fiddling with msrs is very expensive.
3406 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003407static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003408{
Avi Kivity26bb0982009-09-07 11:14:12 +03003409 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003410
Eddie Donga75beee2007-05-17 18:55:15 +03003411 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003412#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003413 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003414 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003415 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003416 move_msr_up(vmx, index, save_nmsrs++);
3417 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003418 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003419 move_msr_up(vmx, index, save_nmsrs++);
3420 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003421 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003422 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003423 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003424 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003425 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003426 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003427 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003428 * if efer.sce is enabled.
3429 */
Brian Gerst8c065852010-07-17 09:03:26 -04003430 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003431 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003432 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003433 }
Eddie Donga75beee2007-05-17 18:55:15 +03003434#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003435 index = __find_msr_index(vmx, MSR_EFER);
3436 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003437 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003438
Avi Kivity26bb0982009-09-07 11:14:12 +03003439 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003440
Yang Zhang8d146952013-01-25 10:18:50 +08003441 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003442 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003443}
3444
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003445static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003447 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003449 if (is_guest_mode(vcpu) &&
3450 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3451 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3452
3453 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454}
3455
3456/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003457 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003459static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003461 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003462 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003463 * We're here if L1 chose not to trap WRMSR to TSC. According
3464 * to the spec, this should set L1's TSC; The offset that L1
3465 * set for L2 remains unchanged, and still needs to be added
3466 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003467 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003468 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003469 /* recalculate vmcs02.TSC_OFFSET: */
3470 vmcs12 = get_vmcs12(vcpu);
3471 vmcs_write64(TSC_OFFSET, offset +
3472 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3473 vmcs12->tsc_offset : 0));
3474 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003475 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3476 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003477 vmcs_write64(TSC_OFFSET, offset);
3478 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479}
3480
Nadav Har'El801d3422011-05-25 23:02:23 +03003481/*
3482 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3483 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3484 * all guests if the "nested" module option is off, and can also be disabled
3485 * for a single guest by disabling its VMX cpuid bit.
3486 */
3487static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3488{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003489 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003490}
3491
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003493 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3494 * returned for the various VMX controls MSRs when nested VMX is enabled.
3495 * The same values should also be used to verify that vmcs12 control fields are
3496 * valid during nested entry from L1 to L2.
3497 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3498 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3499 * bit in the high half is on if the corresponding bit in the control field
3500 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003501 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003502static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003503{
Paolo Bonzini13893092018-02-26 13:40:09 +01003504 if (!nested) {
3505 memset(msrs, 0, sizeof(*msrs));
3506 return;
3507 }
3508
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003509 /*
3510 * Note that as a general rule, the high half of the MSRs (bits in
3511 * the control fields which may be 1) should be initialized by the
3512 * intersection of the underlying hardware's MSR (i.e., features which
3513 * can be supported) and the list of features we want to expose -
3514 * because they are known to be properly supported in our code.
3515 * Also, usually, the low half of the MSRs (bits which must be 1) can
3516 * be set to 0, meaning that L1 may turn off any of these bits. The
3517 * reason is that if one of these bits is necessary, it will appear
3518 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3519 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003520 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003521 * These rules have exceptions below.
3522 */
3523
3524 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003525 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003526 msrs->pinbased_ctls_low,
3527 msrs->pinbased_ctls_high);
3528 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003529 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003530 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003531 PIN_BASED_EXT_INTR_MASK |
3532 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003533 PIN_BASED_VIRTUAL_NMIS |
3534 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003535 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003536 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003537 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003538
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003539 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003540 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 msrs->exit_ctls_low,
3542 msrs->exit_ctls_high);
3543 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003544 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003545
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003547#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003548 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003549#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003550 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003551 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003552 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003553 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003554 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3555
Jan Kiszka2996fca2014-06-16 13:59:43 +02003556 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003557 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003558
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003559 /* entry controls */
3560 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003561 msrs->entry_ctls_low,
3562 msrs->entry_ctls_high);
3563 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003564 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003565 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003566#ifdef CONFIG_X86_64
3567 VM_ENTRY_IA32E_MODE |
3568#endif
3569 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003570 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003571 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003572
Jan Kiszka2996fca2014-06-16 13:59:43 +02003573 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003574 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003575
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003576 /* cpu-based controls */
3577 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msrs->procbased_ctls_low,
3579 msrs->procbased_ctls_high);
3580 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003581 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003582 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003583 CPU_BASED_VIRTUAL_INTR_PENDING |
3584 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003585 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3586 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3587 CPU_BASED_CR3_STORE_EXITING |
3588#ifdef CONFIG_X86_64
3589 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3590#endif
3591 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003592 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3593 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3594 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3595 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003596 /*
3597 * We can allow some features even when not supported by the
3598 * hardware. For example, L1 can specify an MSR bitmap - and we
3599 * can use it to avoid exits to L1 - even when L0 runs L2
3600 * without MSR bitmaps.
3601 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003602 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003603 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003604 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003605
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003606 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003608 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3609
Paolo Bonzini80154d72017-08-24 13:55:35 +02003610 /*
3611 * secondary cpu-based controls. Do not include those that
3612 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3613 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003614 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 msrs->secondary_ctls_low,
3616 msrs->secondary_ctls_high);
3617 msrs->secondary_ctls_low = 0;
3618 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003619 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003620 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003621 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003622 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003623 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003624
Liran Alon32c7acf2018-06-23 02:35:11 +03003625 /*
3626 * We can emulate "VMCS shadowing," even if the hardware
3627 * doesn't support it.
3628 */
3629 msrs->secondary_ctls_high |=
3630 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003631
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003632 if (enable_ept) {
3633 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003634 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003635 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003636 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003637 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003638 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003639 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003640 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003641 msrs->ept_caps &= vmx_capability.ept;
3642 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003643 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3644 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003645 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003647 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003648 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003649 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003650 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003651
Bandan Das27c42a12017-08-03 15:54:42 -04003652 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003653 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003654 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003655 /*
3656 * Advertise EPTP switching unconditionally
3657 * since we emulate it
3658 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003659 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003660 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003661 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003662 }
3663
Paolo Bonzinief697a72016-03-18 16:58:38 +01003664 /*
3665 * Old versions of KVM use the single-context version without
3666 * checking for support, so declare that it is supported even
3667 * though it is treated as global context. The alternative is
3668 * not failing the single-context invvpid, and it is worse.
3669 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003670 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003671 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003672 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003674 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003675 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003676
Radim Krčmář0790ec12015-03-17 14:02:32 +01003677 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003679 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3680
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003681 if (flexpriority_enabled)
3682 msrs->secondary_ctls_high |=
3683 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3684
Jan Kiszkac18911a2013-03-13 16:06:41 +01003685 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003686 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003687 msrs->misc_low,
3688 msrs->misc_high);
3689 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3690 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003691 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003692 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003693 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003695
3696 /*
3697 * This MSR reports some information about VMX support. We
3698 * should return information about the VMX we emulate for the
3699 * guest, and the VMCS structure we give it - not about the
3700 * VMX support of the underlying hardware.
3701 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003703 VMCS12_REVISION |
3704 VMX_BASIC_TRUE_CTLS |
3705 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3706 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3707
3708 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003709 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003710
3711 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003712 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003713 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3714 * We picked the standard core2 setting.
3715 */
3716#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3717#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003718 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3719 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003720
3721 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003722 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3723 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003724
3725 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003726 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003727}
3728
David Matlack38991522016-11-29 18:14:08 -08003729/*
3730 * if fixed0[i] == 1: val[i] must be 1
3731 * if fixed1[i] == 0: val[i] must be 0
3732 */
3733static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3734{
3735 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736}
3737
3738static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3739{
David Matlack38991522016-11-29 18:14:08 -08003740 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003741}
3742
3743static inline u64 vmx_control_msr(u32 low, u32 high)
3744{
3745 return low | ((u64)high << 32);
3746}
3747
David Matlack62cc6b9d2016-11-29 18:14:07 -08003748static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3749{
3750 superset &= mask;
3751 subset &= mask;
3752
3753 return (superset | subset) == superset;
3754}
3755
3756static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3757{
3758 const u64 feature_and_reserved =
3759 /* feature (except bit 48; see below) */
3760 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3761 /* reserved */
3762 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003763 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003764
3765 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3766 return -EINVAL;
3767
3768 /*
3769 * KVM does not emulate a version of VMX that constrains physical
3770 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3771 */
3772 if (data & BIT_ULL(48))
3773 return -EINVAL;
3774
3775 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3776 vmx_basic_vmcs_revision_id(data))
3777 return -EINVAL;
3778
3779 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3780 return -EINVAL;
3781
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003782 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003783 return 0;
3784}
3785
3786static int
3787vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3788{
3789 u64 supported;
3790 u32 *lowp, *highp;
3791
3792 switch (msr_index) {
3793 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003794 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3795 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003796 break;
3797 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003798 lowp = &vmx->nested.msrs.procbased_ctls_low;
3799 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003800 break;
3801 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003802 lowp = &vmx->nested.msrs.exit_ctls_low;
3803 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003804 break;
3805 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 lowp = &vmx->nested.msrs.entry_ctls_low;
3807 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003808 break;
3809 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003810 lowp = &vmx->nested.msrs.secondary_ctls_low;
3811 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003812 break;
3813 default:
3814 BUG();
3815 }
3816
3817 supported = vmx_control_msr(*lowp, *highp);
3818
3819 /* Check must-be-1 bits are still 1. */
3820 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3821 return -EINVAL;
3822
3823 /* Check must-be-0 bits are still 0. */
3824 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3825 return -EINVAL;
3826
3827 *lowp = data;
3828 *highp = data >> 32;
3829 return 0;
3830}
3831
3832static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3833{
3834 const u64 feature_and_reserved_bits =
3835 /* feature */
3836 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3837 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3838 /* reserved */
3839 GENMASK_ULL(13, 9) | BIT_ULL(31);
3840 u64 vmx_misc;
3841
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003842 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3843 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003844
3845 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3846 return -EINVAL;
3847
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003848 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003849 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3850 vmx_misc_preemption_timer_rate(data) !=
3851 vmx_misc_preemption_timer_rate(vmx_misc))
3852 return -EINVAL;
3853
3854 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3855 return -EINVAL;
3856
3857 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3858 return -EINVAL;
3859
3860 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3861 return -EINVAL;
3862
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003863 vmx->nested.msrs.misc_low = data;
3864 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003865
3866 /*
3867 * If L1 has read-only VM-exit information fields, use the
3868 * less permissive vmx_vmwrite_bitmap to specify write
3869 * permissions for the shadow VMCS.
3870 */
3871 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3872 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3873
David Matlack62cc6b9d2016-11-29 18:14:07 -08003874 return 0;
3875}
3876
3877static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3878{
3879 u64 vmx_ept_vpid_cap;
3880
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003881 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3882 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003883
3884 /* Every bit is either reserved or a feature bit. */
3885 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3886 return -EINVAL;
3887
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003888 vmx->nested.msrs.ept_caps = data;
3889 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003890 return 0;
3891}
3892
3893static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3894{
3895 u64 *msr;
3896
3897 switch (msr_index) {
3898 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003899 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003900 break;
3901 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003902 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003903 break;
3904 default:
3905 BUG();
3906 }
3907
3908 /*
3909 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3910 * must be 1 in the restored value.
3911 */
3912 if (!is_bitwise_subset(data, *msr, -1ULL))
3913 return -EINVAL;
3914
3915 *msr = data;
3916 return 0;
3917}
3918
3919/*
3920 * Called when userspace is restoring VMX MSRs.
3921 *
3922 * Returns 0 on success, non-0 otherwise.
3923 */
3924static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3925{
3926 struct vcpu_vmx *vmx = to_vmx(vcpu);
3927
Jim Mattsona943ac52018-05-29 09:11:32 -07003928 /*
3929 * Don't allow changes to the VMX capability MSRs while the vCPU
3930 * is in VMX operation.
3931 */
3932 if (vmx->nested.vmxon)
3933 return -EBUSY;
3934
David Matlack62cc6b9d2016-11-29 18:14:07 -08003935 switch (msr_index) {
3936 case MSR_IA32_VMX_BASIC:
3937 return vmx_restore_vmx_basic(vmx, data);
3938 case MSR_IA32_VMX_PINBASED_CTLS:
3939 case MSR_IA32_VMX_PROCBASED_CTLS:
3940 case MSR_IA32_VMX_EXIT_CTLS:
3941 case MSR_IA32_VMX_ENTRY_CTLS:
3942 /*
3943 * The "non-true" VMX capability MSRs are generated from the
3944 * "true" MSRs, so we do not support restoring them directly.
3945 *
3946 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3947 * should restore the "true" MSRs with the must-be-1 bits
3948 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3949 * DEFAULT SETTINGS".
3950 */
3951 return -EINVAL;
3952 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3953 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3954 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3955 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3956 case MSR_IA32_VMX_PROCBASED_CTLS2:
3957 return vmx_restore_control_msr(vmx, msr_index, data);
3958 case MSR_IA32_VMX_MISC:
3959 return vmx_restore_vmx_misc(vmx, data);
3960 case MSR_IA32_VMX_CR0_FIXED0:
3961 case MSR_IA32_VMX_CR4_FIXED0:
3962 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3963 case MSR_IA32_VMX_CR0_FIXED1:
3964 case MSR_IA32_VMX_CR4_FIXED1:
3965 /*
3966 * These MSRs are generated based on the vCPU's CPUID, so we
3967 * do not support restoring them directly.
3968 */
3969 return -EINVAL;
3970 case MSR_IA32_VMX_EPT_VPID_CAP:
3971 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3972 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003973 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003974 return 0;
3975 default:
3976 /*
3977 * The rest of the VMX capability MSRs do not support restore.
3978 */
3979 return -EINVAL;
3980 }
3981}
3982
Jan Kiszkacae50132014-01-04 18:47:22 +01003983/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003984static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003985{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003986 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003987 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003988 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003989 break;
3990 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3991 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003992 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003993 msrs->pinbased_ctls_low,
3994 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003995 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3996 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3999 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004000 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 msrs->procbased_ctls_low,
4002 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004003 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
4004 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
4007 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004008 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004009 msrs->exit_ctls_low,
4010 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004011 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
4012 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004013 break;
4014 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
4015 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004016 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004017 msrs->entry_ctls_low,
4018 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004019 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
4020 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004021 break;
4022 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004023 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004024 msrs->misc_low,
4025 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004027 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004028 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004029 break;
4030 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004031 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004032 break;
4033 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004034 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004035 break;
4036 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004037 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004038 break;
4039 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004040 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004041 break;
4042 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004043 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004044 msrs->secondary_ctls_low,
4045 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004046 break;
4047 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004048 *pdata = msrs->ept_caps |
4049 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004050 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004051 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004052 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004053 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004054 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004055 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004056 }
4057
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004058 return 0;
4059}
4060
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004061static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4062 uint64_t val)
4063{
4064 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4065
4066 return !(val & ~valid_bits);
4067}
4068
Tom Lendacky801e4592018-02-21 13:39:51 -06004069static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4070{
Paolo Bonzini13893092018-02-26 13:40:09 +01004071 switch (msr->index) {
4072 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4073 if (!nested)
4074 return 1;
4075 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4076 default:
4077 return 1;
4078 }
4079
4080 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004081}
4082
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004083/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 * Reads an msr value (of 'msr_index') into 'pdata'.
4085 * Returns 0 on success, non-0 otherwise.
4086 * Assumes vcpu_load() was already called.
4087 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004088static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004090 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004091 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004093 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004094#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004096 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097 break;
4098 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004099 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004101 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004102 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004103 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004104#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004106 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004107 case MSR_IA32_SPEC_CTRL:
4108 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004109 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4110 return 1;
4111
4112 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4113 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004114 case MSR_IA32_ARCH_CAPABILITIES:
4115 if (!msr_info->host_initiated &&
4116 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4117 return 1;
4118 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4119 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004121 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122 break;
4123 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004124 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125 break;
4126 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004127 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004129 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004130 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004131 (!msr_info->host_initiated &&
4132 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004133 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004134 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004135 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004136 case MSR_IA32_MCG_EXT_CTL:
4137 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004138 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004139 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004140 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004141 msr_info->data = vcpu->arch.mcg_ext_ctl;
4142 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004143 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004144 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004145 break;
4146 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4147 if (!nested_vmx_allowed(vcpu))
4148 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004149 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4150 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004151 case MSR_IA32_XSS:
4152 if (!vmx_xsaves_supported())
4153 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004154 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004155 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004156 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004157 if (!msr_info->host_initiated &&
4158 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004159 return 1;
4160 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004162 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004163 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004164 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004165 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004167 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168 }
4169
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170 return 0;
4171}
4172
Jan Kiszkacae50132014-01-04 18:47:22 +01004173static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4174
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175/*
4176 * Writes msr value into into the appropriate "register".
4177 * Returns 0 on success, non-0 otherwise.
4178 * Assumes vcpu_load() was already called.
4179 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004180static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004182 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004183 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004184 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004185 u32 msr_index = msr_info->index;
4186 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004187
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004189 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004190 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004191 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004192#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004194 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195 vmcs_writel(GUEST_FS_BASE, data);
4196 break;
4197 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004198 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004199 vmcs_writel(GUEST_GS_BASE, data);
4200 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004201 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004202 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004203 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204#endif
4205 case MSR_IA32_SYSENTER_CS:
4206 vmcs_write32(GUEST_SYSENTER_CS, data);
4207 break;
4208 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004209 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004210 break;
4211 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004212 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004214 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004215 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004216 (!msr_info->host_initiated &&
4217 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004218 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004219 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004220 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004222 vmcs_write64(GUEST_BNDCFGS, data);
4223 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004224 case MSR_IA32_SPEC_CTRL:
4225 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004226 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4227 return 1;
4228
4229 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004230 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004231 return 1;
4232
4233 vmx->spec_ctrl = data;
4234
4235 if (!data)
4236 break;
4237
4238 /*
4239 * For non-nested:
4240 * When it's written (to non-zero) for the first time, pass
4241 * it through.
4242 *
4243 * For nested:
4244 * The handling of the MSR bitmap for L2 guests is done in
4245 * nested_vmx_merge_msr_bitmap. We should not touch the
4246 * vmcs02.msr_bitmap here since it gets completely overwritten
4247 * in the merging. We update the vmcs01 here for L1 as well
4248 * since it will end up touching the MSR anyway now.
4249 */
4250 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4251 MSR_IA32_SPEC_CTRL,
4252 MSR_TYPE_RW);
4253 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004254 case MSR_IA32_PRED_CMD:
4255 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004256 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4257 return 1;
4258
4259 if (data & ~PRED_CMD_IBPB)
4260 return 1;
4261
4262 if (!data)
4263 break;
4264
4265 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4266
4267 /*
4268 * For non-nested:
4269 * When it's written (to non-zero) for the first time, pass
4270 * it through.
4271 *
4272 * For nested:
4273 * The handling of the MSR bitmap for L2 guests is done in
4274 * nested_vmx_merge_msr_bitmap. We should not touch the
4275 * vmcs02.msr_bitmap here since it gets completely overwritten
4276 * in the merging.
4277 */
4278 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4279 MSR_TYPE_W);
4280 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004281 case MSR_IA32_ARCH_CAPABILITIES:
4282 if (!msr_info->host_initiated)
4283 return 1;
4284 vmx->arch_capabilities = data;
4285 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004286 case MSR_IA32_CR_PAT:
4287 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004288 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4289 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004290 vmcs_write64(GUEST_IA32_PAT, data);
4291 vcpu->arch.pat = data;
4292 break;
4293 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004294 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004295 break;
Will Auldba904632012-11-29 12:42:50 -08004296 case MSR_IA32_TSC_ADJUST:
4297 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004298 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004299 case MSR_IA32_MCG_EXT_CTL:
4300 if ((!msr_info->host_initiated &&
4301 !(to_vmx(vcpu)->msr_ia32_feature_control &
4302 FEATURE_CONTROL_LMCE)) ||
4303 (data & ~MCG_EXT_CTL_LMCE_EN))
4304 return 1;
4305 vcpu->arch.mcg_ext_ctl = data;
4306 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004307 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004308 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004309 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004310 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4311 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004312 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004313 if (msr_info->host_initiated && data == 0)
4314 vmx_leave_nested(vcpu);
4315 break;
4316 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004317 if (!msr_info->host_initiated)
4318 return 1; /* they are read-only */
4319 if (!nested_vmx_allowed(vcpu))
4320 return 1;
4321 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004322 case MSR_IA32_XSS:
4323 if (!vmx_xsaves_supported())
4324 return 1;
4325 /*
4326 * The only supported bit as of Skylake is bit 8, but
4327 * it is not supported on KVM.
4328 */
4329 if (data != 0)
4330 return 1;
4331 vcpu->arch.ia32_xss = data;
4332 if (vcpu->arch.ia32_xss != host_xss)
4333 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004334 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004335 else
4336 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4337 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004338 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004339 if (!msr_info->host_initiated &&
4340 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004341 return 1;
4342 /* Check reserved bit, higher 32 bits should be zero */
4343 if ((data >> 32) != 0)
4344 return 1;
4345 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004347 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004348 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004349 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004350 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004351 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4352 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004353 ret = kvm_set_shared_msr(msr->index, msr->data,
4354 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004355 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004356 if (ret)
4357 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004358 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004359 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004361 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362 }
4363
Eddie Dong2cc51562007-05-21 07:28:09 +03004364 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}
4366
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004367static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004369 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4370 switch (reg) {
4371 case VCPU_REGS_RSP:
4372 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4373 break;
4374 case VCPU_REGS_RIP:
4375 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4376 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004377 case VCPU_EXREG_PDPTR:
4378 if (enable_ept)
4379 ept_save_pdptrs(vcpu);
4380 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004381 default:
4382 break;
4383 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004384}
4385
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386static __init int cpu_has_kvm_support(void)
4387{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004388 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004389}
4390
4391static __init int vmx_disabled_by_bios(void)
4392{
4393 u64 msr;
4394
4395 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004396 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004397 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004398 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4399 && tboot_enabled())
4400 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004401 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004402 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004403 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004404 && !tboot_enabled()) {
4405 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004406 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004407 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004408 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004409 /* launched w/o TXT and VMX disabled */
4410 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4411 && !tboot_enabled())
4412 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004413 }
4414
4415 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004416}
4417
Dongxiao Xu7725b892010-05-11 18:29:38 +08004418static void kvm_cpu_vmxon(u64 addr)
4419{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004420 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004421 intel_pt_handle_vmx(1);
4422
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004423 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08004424}
4425
Radim Krčmář13a34e02014-08-28 15:13:03 +02004426static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427{
4428 int cpu = raw_smp_processor_id();
4429 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004430 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004432 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004433 return -EBUSY;
4434
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004435 /*
4436 * This can happen if we hot-added a CPU but failed to allocate
4437 * VP assist page for it.
4438 */
4439 if (static_branch_unlikely(&enable_evmcs) &&
4440 !hv_get_vp_assist_page(cpu))
4441 return -EFAULT;
4442
Nadav Har'Eld462b812011-05-24 15:26:10 +03004443 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004444 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4445 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004446
4447 /*
4448 * Now we can enable the vmclear operation in kdump
4449 * since the loaded_vmcss_on_cpu list on this cpu
4450 * has been initialized.
4451 *
4452 * Though the cpu is not in VMX operation now, there
4453 * is no problem to enable the vmclear operation
4454 * for the loaded_vmcss_on_cpu list is empty!
4455 */
4456 crash_enable_local_vmclear(cpu);
4457
Avi Kivity6aa8b732006-12-10 02:21:36 -08004458 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004459
4460 test_bits = FEATURE_CONTROL_LOCKED;
4461 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4462 if (tboot_enabled())
4463 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4464
4465 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004467 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4468 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004469 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004470 if (enable_ept)
4471 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004472
4473 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474}
4475
Nadav Har'Eld462b812011-05-24 15:26:10 +03004476static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004477{
4478 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004479 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004480
Nadav Har'Eld462b812011-05-24 15:26:10 +03004481 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4482 loaded_vmcss_on_cpu_link)
4483 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004484}
4485
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004486
4487/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4488 * tricks.
4489 */
4490static void kvm_cpu_vmxoff(void)
4491{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004492 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004493
4494 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004495 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004496}
4497
Radim Krčmář13a34e02014-08-28 15:13:03 +02004498static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004500 vmclear_local_loaded_vmcss();
4501 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004502}
4503
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004504static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004505 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004506{
4507 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004508 u32 ctl = ctl_min | ctl_opt;
4509
4510 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4511
4512 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4513 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4514
4515 /* Ensure minimum (required) set of control bits are supported. */
4516 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004517 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004518
4519 *result = ctl;
4520 return 0;
4521}
4522
Avi Kivity110312c2010-12-21 12:54:20 +02004523static __init bool allow_1_setting(u32 msr, u32 ctl)
4524{
4525 u32 vmx_msr_low, vmx_msr_high;
4526
4527 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4528 return vmx_msr_high & ctl;
4529}
4530
Yang, Sheng002c7f72007-07-31 14:23:01 +03004531static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004532{
4533 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004534 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004535 u32 _pin_based_exec_control = 0;
4536 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004537 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004538 u32 _vmexit_control = 0;
4539 u32 _vmentry_control = 0;
4540
Paolo Bonzini13893092018-02-26 13:40:09 +01004541 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304542 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004543#ifdef CONFIG_X86_64
4544 CPU_BASED_CR8_LOAD_EXITING |
4545 CPU_BASED_CR8_STORE_EXITING |
4546#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004547 CPU_BASED_CR3_LOAD_EXITING |
4548 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004549 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004550 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004551 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004552 CPU_BASED_MWAIT_EXITING |
4553 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004554 CPU_BASED_INVLPG_EXITING |
4555 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004556
Sheng Yangf78e0e22007-10-29 09:40:42 +08004557 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004558 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004559 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004560 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4561 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004562 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004563#ifdef CONFIG_X86_64
4564 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4565 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4566 ~CPU_BASED_CR8_STORE_EXITING;
4567#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004568 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004569 min2 = 0;
4570 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004571 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004572 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004573 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004574 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004575 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004576 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004577 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004578 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004579 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004580 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004581 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004582 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004583 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004584 SECONDARY_EXEC_RDSEED_EXITING |
4585 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004586 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004587 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004588 SECONDARY_EXEC_ENABLE_VMFUNC |
4589 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004590 if (adjust_vmx_controls(min2, opt2,
4591 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004592 &_cpu_based_2nd_exec_control) < 0)
4593 return -EIO;
4594 }
4595#ifndef CONFIG_X86_64
4596 if (!(_cpu_based_2nd_exec_control &
4597 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4598 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4599#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004600
4601 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4602 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004603 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004604 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4605 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004606
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004607 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4608 &vmx_capability.ept, &vmx_capability.vpid);
4609
Sheng Yangd56f5462008-04-25 10:13:16 +08004610 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004611 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4612 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004613 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4614 CPU_BASED_CR3_STORE_EXITING |
4615 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004616 } else if (vmx_capability.ept) {
4617 vmx_capability.ept = 0;
4618 pr_warn_once("EPT CAP should not exist if not support "
4619 "1-setting enable EPT VM-execution control\n");
4620 }
4621 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4622 vmx_capability.vpid) {
4623 vmx_capability.vpid = 0;
4624 pr_warn_once("VPID CAP should not exist if not support "
4625 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004626 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004627
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004628 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004629#ifdef CONFIG_X86_64
4630 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4631#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004632 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004633 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004634 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4635 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004636 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004637
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004638 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4639 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4640 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004641 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4642 &_pin_based_exec_control) < 0)
4643 return -EIO;
4644
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004645 if (cpu_has_broken_vmx_preemption_timer())
4646 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004647 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004648 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004649 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4650
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004651 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004652 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004653 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4654 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004655 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004657 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004658
4659 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4660 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004661 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004662
4663#ifdef CONFIG_X86_64
4664 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4665 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004666 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004667#endif
4668
4669 /* Require Write-Back (WB) memory type for VMCS accesses. */
4670 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004671 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004672
Yang, Sheng002c7f72007-07-31 14:23:01 +03004673 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004674 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004675 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004676
Liran Alon2307af12018-06-29 22:59:04 +03004677 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004678
Yang, Sheng002c7f72007-07-31 14:23:01 +03004679 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4680 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004681 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004682 vmcs_conf->vmexit_ctrl = _vmexit_control;
4683 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004684
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004685 if (static_branch_unlikely(&enable_evmcs))
4686 evmcs_sanitize_exec_ctrls(vmcs_conf);
4687
Avi Kivity110312c2010-12-21 12:54:20 +02004688 cpu_has_load_ia32_efer =
4689 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4690 VM_ENTRY_LOAD_IA32_EFER)
4691 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4692 VM_EXIT_LOAD_IA32_EFER);
4693
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004694 cpu_has_load_perf_global_ctrl =
4695 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4696 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4697 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4698 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4699
4700 /*
4701 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004702 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004703 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4704 *
4705 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4706 *
4707 * AAK155 (model 26)
4708 * AAP115 (model 30)
4709 * AAT100 (model 37)
4710 * BC86,AAY89,BD102 (model 44)
4711 * BA97 (model 46)
4712 *
4713 */
4714 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4715 switch (boot_cpu_data.x86_model) {
4716 case 26:
4717 case 30:
4718 case 37:
4719 case 44:
4720 case 46:
4721 cpu_has_load_perf_global_ctrl = false;
4722 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4723 "does not work properly. Using workaround\n");
4724 break;
4725 default:
4726 break;
4727 }
4728 }
4729
Borislav Petkov782511b2016-04-04 22:25:03 +02004730 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004731 rdmsrl(MSR_IA32_XSS, host_xss);
4732
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004733 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004734}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735
Liran Alon491a6032018-06-23 02:35:12 +03004736static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737{
4738 int node = cpu_to_node(cpu);
4739 struct page *pages;
4740 struct vmcs *vmcs;
4741
Vlastimil Babka96db8002015-09-08 15:03:50 -07004742 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004743 if (!pages)
4744 return NULL;
4745 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004746 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004747
4748 /* KVM supports Enlightened VMCS v1 only */
4749 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004750 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004751 else
Liran Alon392b2f22018-06-23 02:35:01 +03004752 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004753
Liran Alon491a6032018-06-23 02:35:12 +03004754 if (shadow)
4755 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756 return vmcs;
4757}
4758
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759static void free_vmcs(struct vmcs *vmcs)
4760{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004761 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762}
4763
Nadav Har'Eld462b812011-05-24 15:26:10 +03004764/*
4765 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4766 */
4767static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4768{
4769 if (!loaded_vmcs->vmcs)
4770 return;
4771 loaded_vmcs_clear(loaded_vmcs);
4772 free_vmcs(loaded_vmcs->vmcs);
4773 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004774 if (loaded_vmcs->msr_bitmap)
4775 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004776 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004777}
4778
Liran Alon491a6032018-06-23 02:35:12 +03004779static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004780{
Liran Alon491a6032018-06-23 02:35:12 +03004781 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004782}
4783
4784static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4785{
Liran Alon491a6032018-06-23 02:35:12 +03004786 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004787 if (!loaded_vmcs->vmcs)
4788 return -ENOMEM;
4789
4790 loaded_vmcs->shadow_vmcs = NULL;
4791 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004792
4793 if (cpu_has_vmx_msr_bitmap()) {
4794 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4795 if (!loaded_vmcs->msr_bitmap)
4796 goto out_vmcs;
4797 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004798
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004799 if (IS_ENABLED(CONFIG_HYPERV) &&
4800 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004801 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4802 struct hv_enlightened_vmcs *evmcs =
4803 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4804
4805 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4806 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004807 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004808
4809 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4810
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004811 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004812
4813out_vmcs:
4814 free_loaded_vmcs(loaded_vmcs);
4815 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004816}
4817
Sam Ravnborg39959582007-06-01 00:47:13 -07004818static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004819{
4820 int cpu;
4821
Zachary Amsden3230bb42009-09-29 11:38:37 -10004822 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004823 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004824 per_cpu(vmxarea, cpu) = NULL;
4825 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826}
4827
Jim Mattsond37f4262017-12-22 12:12:16 -08004828enum vmcs_field_width {
4829 VMCS_FIELD_WIDTH_U16 = 0,
4830 VMCS_FIELD_WIDTH_U64 = 1,
4831 VMCS_FIELD_WIDTH_U32 = 2,
4832 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004833};
4834
Jim Mattsond37f4262017-12-22 12:12:16 -08004835static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004836{
4837 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004838 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004839 return (field >> 13) & 0x3 ;
4840}
4841
4842static inline int vmcs_field_readonly(unsigned long field)
4843{
4844 return (((field >> 10) & 0x3) == 1);
4845}
4846
Bandan Dasfe2b2012014-04-21 15:20:14 -04004847static void init_vmcs_shadow_fields(void)
4848{
4849 int i, j;
4850
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004851 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4852 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004853 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004854 (i + 1 == max_shadow_read_only_fields ||
4855 shadow_read_only_fields[i + 1] != field + 1))
4856 pr_err("Missing field from shadow_read_only_field %x\n",
4857 field + 1);
4858
4859 clear_bit(field, vmx_vmread_bitmap);
4860#ifdef CONFIG_X86_64
4861 if (field & 1)
4862 continue;
4863#endif
4864 if (j < i)
4865 shadow_read_only_fields[j] = field;
4866 j++;
4867 }
4868 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004869
4870 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004871 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004872 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004873 (i + 1 == max_shadow_read_write_fields ||
4874 shadow_read_write_fields[i + 1] != field + 1))
4875 pr_err("Missing field from shadow_read_write_field %x\n",
4876 field + 1);
4877
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004878 /*
4879 * PML and the preemption timer can be emulated, but the
4880 * processor cannot vmwrite to fields that don't exist
4881 * on bare metal.
4882 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004883 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004884 case GUEST_PML_INDEX:
4885 if (!cpu_has_vmx_pml())
4886 continue;
4887 break;
4888 case VMX_PREEMPTION_TIMER_VALUE:
4889 if (!cpu_has_vmx_preemption_timer())
4890 continue;
4891 break;
4892 case GUEST_INTR_STATUS:
4893 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004894 continue;
4895 break;
4896 default:
4897 break;
4898 }
4899
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004900 clear_bit(field, vmx_vmwrite_bitmap);
4901 clear_bit(field, vmx_vmread_bitmap);
4902#ifdef CONFIG_X86_64
4903 if (field & 1)
4904 continue;
4905#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004906 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004907 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004908 j++;
4909 }
4910 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004911}
4912
Avi Kivity6aa8b732006-12-10 02:21:36 -08004913static __init int alloc_kvm_area(void)
4914{
4915 int cpu;
4916
Zachary Amsden3230bb42009-09-29 11:38:37 -10004917 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918 struct vmcs *vmcs;
4919
Liran Alon491a6032018-06-23 02:35:12 +03004920 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921 if (!vmcs) {
4922 free_kvm_area();
4923 return -ENOMEM;
4924 }
4925
Liran Alon2307af12018-06-29 22:59:04 +03004926 /*
4927 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4928 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4929 * revision_id reported by MSR_IA32_VMX_BASIC.
4930 *
4931 * However, even though not explictly documented by
4932 * TLFS, VMXArea passed as VMXON argument should
4933 * still be marked with revision_id reported by
4934 * physical CPU.
4935 */
4936 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004937 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004938
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939 per_cpu(vmxarea, cpu) = vmcs;
4940 }
4941 return 0;
4942}
4943
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004944static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004945 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004946{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004947 if (!emulate_invalid_guest_state) {
4948 /*
4949 * CS and SS RPL should be equal during guest entry according
4950 * to VMX spec, but in reality it is not always so. Since vcpu
4951 * is in the middle of the transition from real mode to
4952 * protected mode it is safe to assume that RPL 0 is a good
4953 * default value.
4954 */
4955 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004956 save->selector &= ~SEGMENT_RPL_MASK;
4957 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004958 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004960 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961}
4962
4963static void enter_pmode(struct kvm_vcpu *vcpu)
4964{
4965 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004966 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967
Gleb Natapovd99e4152012-12-20 16:57:45 +02004968 /*
4969 * Update real mode segment cache. It may be not up-to-date if sement
4970 * register was written while vcpu was in a guest mode.
4971 */
4972 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4973 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4974 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4975 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4976 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4977 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4978
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004979 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980
Avi Kivity2fb92db2011-04-27 19:42:18 +03004981 vmx_segment_cache_clear(vmx);
4982
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004983 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984
4985 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004986 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4987 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988 vmcs_writel(GUEST_RFLAGS, flags);
4989
Rusty Russell66aee912007-07-17 23:34:16 +10004990 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4991 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004992
4993 update_exception_bitmap(vcpu);
4994
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004995 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4996 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4997 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4998 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4999 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
5000 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001}
5002
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005003static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005004{
Mathias Krause772e0312012-08-30 01:30:19 +02005005 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02005006 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007
Gleb Natapovd99e4152012-12-20 16:57:45 +02005008 var.dpl = 0x3;
5009 if (seg == VCPU_SREG_CS)
5010 var.type = 0x3;
5011
5012 if (!emulate_invalid_guest_state) {
5013 var.selector = var.base >> 4;
5014 var.base = var.base & 0xffff0;
5015 var.limit = 0xffff;
5016 var.g = 0;
5017 var.db = 0;
5018 var.present = 1;
5019 var.s = 1;
5020 var.l = 0;
5021 var.unusable = 0;
5022 var.type = 0x3;
5023 var.avl = 0;
5024 if (save->base & 0xf)
5025 printk_once(KERN_WARNING "kvm: segment base is not "
5026 "paragraph aligned when entering "
5027 "protected mode (seg=%d)", seg);
5028 }
5029
5030 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005031 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005032 vmcs_write32(sf->limit, var.limit);
5033 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034}
5035
5036static void enter_rmode(struct kvm_vcpu *vcpu)
5037{
5038 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005039 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005040 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005042 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5043 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5044 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5045 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5046 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005047 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5048 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005049
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005050 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051
Gleb Natapov776e58e2011-03-13 12:34:27 +02005052 /*
5053 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005054 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005055 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005056 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005057 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5058 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005059
Avi Kivity2fb92db2011-04-27 19:42:18 +03005060 vmx_segment_cache_clear(vmx);
5061
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005062 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005063 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005064 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5065
5066 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005067 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005068
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005069 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005070
5071 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005072 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073 update_exception_bitmap(vcpu);
5074
Gleb Natapovd99e4152012-12-20 16:57:45 +02005075 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5076 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5077 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5078 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5079 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5080 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005081
Eddie Dong8668a3c2007-10-10 14:26:45 +08005082 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005083}
5084
Amit Shah401d10d2009-02-20 22:53:37 +05305085static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5086{
5087 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005088 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5089
5090 if (!msr)
5091 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305092
Avi Kivityf6801df2010-01-21 15:31:50 +02005093 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305094 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005095 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305096 msr->data = efer;
5097 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005098 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305099
5100 msr->data = efer & ~EFER_LME;
5101 }
5102 setup_msrs(vmx);
5103}
5104
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005105#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106
5107static void enter_lmode(struct kvm_vcpu *vcpu)
5108{
5109 u32 guest_tr_ar;
5110
Avi Kivity2fb92db2011-04-27 19:42:18 +03005111 vmx_segment_cache_clear(to_vmx(vcpu));
5112
Avi Kivity6aa8b732006-12-10 02:21:36 -08005113 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005114 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005115 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5116 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005117 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005118 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5119 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005120 }
Avi Kivityda38f432010-07-06 11:30:49 +03005121 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122}
5123
5124static void exit_lmode(struct kvm_vcpu *vcpu)
5125{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005126 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005127 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005128}
5129
5130#endif
5131
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005132static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5133 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005134{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005135 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005136 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005137 return;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005138 ept_sync_context(construct_eptp(vcpu,
5139 vcpu->arch.mmu->root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005140 } else {
5141 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005142 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005143}
5144
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005145static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005146{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005147 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005148}
5149
Junaid Shahidfaff8752018-06-29 13:10:05 -07005150static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5151{
5152 int vpid = to_vmx(vcpu)->vpid;
5153
5154 if (!vpid_sync_vcpu_addr(vpid, addr))
5155 vpid_sync_context(vpid);
5156
5157 /*
5158 * If VPIDs are not supported or enabled, then the above is a no-op.
5159 * But we don't really need a TLB flush in that case anyway, because
5160 * each VM entry/exit includes an implicit flush when VPID is 0.
5161 */
5162}
5163
Avi Kivitye8467fd2009-12-29 18:43:06 +02005164static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5165{
5166 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5167
5168 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5169 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5170}
5171
Avi Kivityaff48ba2010-12-05 18:56:11 +02005172static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5173{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005174 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005175 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5176 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5177}
5178
Anthony Liguori25c4c272007-04-27 09:29:21 +03005179static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005180{
Avi Kivityfc78f512009-12-07 12:16:48 +02005181 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5182
5183 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5184 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005185}
5186
Sheng Yang14394422008-04-28 12:24:45 +08005187static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5188{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005189 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5190
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005191 if (!test_bit(VCPU_EXREG_PDPTR,
5192 (unsigned long *)&vcpu->arch.regs_dirty))
5193 return;
5194
Sheng Yang14394422008-04-28 12:24:45 +08005195 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005196 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5197 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5198 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5199 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005200 }
5201}
5202
Avi Kivity8f5d5492009-05-31 18:41:29 +03005203static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5204{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005205 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5206
Avi Kivity8f5d5492009-05-31 18:41:29 +03005207 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005208 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5209 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5210 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5211 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005212 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005213
5214 __set_bit(VCPU_EXREG_PDPTR,
5215 (unsigned long *)&vcpu->arch.regs_avail);
5216 __set_bit(VCPU_EXREG_PDPTR,
5217 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005218}
5219
David Matlack38991522016-11-29 18:14:08 -08005220static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5221{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005222 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5223 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005224 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5225
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005226 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005227 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5228 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5229 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5230
5231 return fixed_bits_valid(val, fixed0, fixed1);
5232}
5233
5234static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5235{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005236 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5237 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005238
5239 return fixed_bits_valid(val, fixed0, fixed1);
5240}
5241
5242static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5243{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005244 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5245 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005246
5247 return fixed_bits_valid(val, fixed0, fixed1);
5248}
5249
5250/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5251#define nested_guest_cr4_valid nested_cr4_valid
5252#define nested_host_cr4_valid nested_cr4_valid
5253
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005254static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005255
5256static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5257 unsigned long cr0,
5258 struct kvm_vcpu *vcpu)
5259{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005260 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5261 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005262 if (!(cr0 & X86_CR0_PG)) {
5263 /* From paging/starting to nonpaging */
5264 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005265 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005266 (CPU_BASED_CR3_LOAD_EXITING |
5267 CPU_BASED_CR3_STORE_EXITING));
5268 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005269 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005270 } else if (!is_paging(vcpu)) {
5271 /* From nonpaging to paging */
5272 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005273 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005274 ~(CPU_BASED_CR3_LOAD_EXITING |
5275 CPU_BASED_CR3_STORE_EXITING));
5276 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005277 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005278 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005279
5280 if (!(cr0 & X86_CR0_WP))
5281 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005282}
5283
Avi Kivity6aa8b732006-12-10 02:21:36 -08005284static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5285{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005286 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005287 unsigned long hw_cr0;
5288
Sean Christopherson3de63472018-07-13 08:42:30 -07005289 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005290 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005291 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005292 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005293 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005294
Gleb Natapov218e7632013-01-21 15:36:45 +02005295 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5296 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005297
Gleb Natapov218e7632013-01-21 15:36:45 +02005298 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5299 enter_rmode(vcpu);
5300 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005302#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005303 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005304 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005305 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005306 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005307 exit_lmode(vcpu);
5308 }
5309#endif
5310
Sean Christophersonb4d18512018-03-05 12:04:40 -08005311 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005312 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5313
Avi Kivity6aa8b732006-12-10 02:21:36 -08005314 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005315 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005316 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005317
5318 /* depends on vcpu->arch.cr0 to be set to a new value */
5319 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005320}
5321
Yu Zhang855feb62017-08-24 20:27:55 +08005322static int get_ept_level(struct kvm_vcpu *vcpu)
5323{
5324 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5325 return 5;
5326 return 4;
5327}
5328
Peter Feiner995f00a2017-06-30 17:26:32 -07005329static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005330{
Yu Zhang855feb62017-08-24 20:27:55 +08005331 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005332
Yu Zhang855feb62017-08-24 20:27:55 +08005333 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005334
Peter Feiner995f00a2017-06-30 17:26:32 -07005335 if (enable_ept_ad_bits &&
5336 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005337 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005338 eptp |= (root_hpa & PAGE_MASK);
5339
5340 return eptp;
5341}
5342
Avi Kivity6aa8b732006-12-10 02:21:36 -08005343static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5344{
Tianyu Lan877ad952018-07-19 08:40:23 +00005345 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005346 unsigned long guest_cr3;
5347 u64 eptp;
5348
5349 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005350 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005351 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005352 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005353
5354 if (kvm_x86_ops->tlb_remote_flush) {
5355 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5356 to_vmx(vcpu)->ept_pointer = eptp;
5357 to_kvm_vmx(kvm)->ept_pointers_match
5358 = EPT_POINTERS_CHECK;
5359 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5360 }
5361
Sean Christophersone90008d2018-03-05 12:04:37 -08005362 if (enable_unrestricted_guest || is_paging(vcpu) ||
5363 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005364 guest_cr3 = kvm_read_cr3(vcpu);
5365 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005366 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005367 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005368 }
5369
Sheng Yang14394422008-04-28 12:24:45 +08005370 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005371}
5372
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005373static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005374{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005375 /*
5376 * Pass through host's Machine Check Enable value to hw_cr4, which
5377 * is in force while we are in guest mode. Do not let guests control
5378 * this bit, even if host CR4.MCE == 0.
5379 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005380 unsigned long hw_cr4;
5381
5382 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5383 if (enable_unrestricted_guest)
5384 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5385 else if (to_vmx(vcpu)->rmode.vm86_active)
5386 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5387 else
5388 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005389
Sean Christopherson64f7a112018-04-30 10:01:06 -07005390 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5391 if (cr4 & X86_CR4_UMIP) {
5392 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005393 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005394 hw_cr4 &= ~X86_CR4_UMIP;
5395 } else if (!is_guest_mode(vcpu) ||
5396 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5397 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5398 SECONDARY_EXEC_DESC);
5399 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005400
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005401 if (cr4 & X86_CR4_VMXE) {
5402 /*
5403 * To use VMXON (and later other VMX instructions), a guest
5404 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5405 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005406 * is here. We operate under the default treatment of SMM,
5407 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005408 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005409 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005410 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005411 }
David Matlack38991522016-11-29 18:14:08 -08005412
5413 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005414 return 1;
5415
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005416 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005417
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005418 if (!enable_unrestricted_guest) {
5419 if (enable_ept) {
5420 if (!is_paging(vcpu)) {
5421 hw_cr4 &= ~X86_CR4_PAE;
5422 hw_cr4 |= X86_CR4_PSE;
5423 } else if (!(cr4 & X86_CR4_PAE)) {
5424 hw_cr4 &= ~X86_CR4_PAE;
5425 }
5426 }
5427
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005428 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005429 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5430 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5431 * to be manually disabled when guest switches to non-paging
5432 * mode.
5433 *
5434 * If !enable_unrestricted_guest, the CPU is always running
5435 * with CR0.PG=1 and CR4 needs to be modified.
5436 * If enable_unrestricted_guest, the CPU automatically
5437 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005438 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005439 if (!is_paging(vcpu))
5440 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5441 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005442
Sheng Yang14394422008-04-28 12:24:45 +08005443 vmcs_writel(CR4_READ_SHADOW, cr4);
5444 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005445 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446}
5447
Avi Kivity6aa8b732006-12-10 02:21:36 -08005448static void vmx_get_segment(struct kvm_vcpu *vcpu,
5449 struct kvm_segment *var, int seg)
5450{
Avi Kivitya9179492011-01-03 14:28:52 +02005451 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452 u32 ar;
5453
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005454 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005455 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005456 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005457 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005458 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005459 var->base = vmx_read_guest_seg_base(vmx, seg);
5460 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5461 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005462 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005463 var->base = vmx_read_guest_seg_base(vmx, seg);
5464 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5465 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5466 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005467 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468 var->type = ar & 15;
5469 var->s = (ar >> 4) & 1;
5470 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005471 /*
5472 * Some userspaces do not preserve unusable property. Since usable
5473 * segment has to be present according to VMX spec we can use present
5474 * property to amend userspace bug by making unusable segment always
5475 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5476 * segment as unusable.
5477 */
5478 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005479 var->avl = (ar >> 12) & 1;
5480 var->l = (ar >> 13) & 1;
5481 var->db = (ar >> 14) & 1;
5482 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005483}
5484
Avi Kivitya9179492011-01-03 14:28:52 +02005485static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5486{
Avi Kivitya9179492011-01-03 14:28:52 +02005487 struct kvm_segment s;
5488
5489 if (to_vmx(vcpu)->rmode.vm86_active) {
5490 vmx_get_segment(vcpu, &s, seg);
5491 return s.base;
5492 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005493 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005494}
5495
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005496static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005497{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005498 struct vcpu_vmx *vmx = to_vmx(vcpu);
5499
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005500 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005501 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005502 else {
5503 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005504 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005505 }
Avi Kivity69c73022011-03-07 15:26:44 +02005506}
5507
Avi Kivity653e3102007-05-07 10:55:37 +03005508static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005509{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005510 u32 ar;
5511
Avi Kivityf0495f92012-06-07 17:06:10 +03005512 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005513 ar = 1 << 16;
5514 else {
5515 ar = var->type & 15;
5516 ar |= (var->s & 1) << 4;
5517 ar |= (var->dpl & 3) << 5;
5518 ar |= (var->present & 1) << 7;
5519 ar |= (var->avl & 1) << 12;
5520 ar |= (var->l & 1) << 13;
5521 ar |= (var->db & 1) << 14;
5522 ar |= (var->g & 1) << 15;
5523 }
Avi Kivity653e3102007-05-07 10:55:37 +03005524
5525 return ar;
5526}
5527
5528static void vmx_set_segment(struct kvm_vcpu *vcpu,
5529 struct kvm_segment *var, int seg)
5530{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005531 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005532 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005533
Avi Kivity2fb92db2011-04-27 19:42:18 +03005534 vmx_segment_cache_clear(vmx);
5535
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005536 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5537 vmx->rmode.segs[seg] = *var;
5538 if (seg == VCPU_SREG_TR)
5539 vmcs_write16(sf->selector, var->selector);
5540 else if (var->s)
5541 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005542 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005543 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005544
Avi Kivity653e3102007-05-07 10:55:37 +03005545 vmcs_writel(sf->base, var->base);
5546 vmcs_write32(sf->limit, var->limit);
5547 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005548
5549 /*
5550 * Fix the "Accessed" bit in AR field of segment registers for older
5551 * qemu binaries.
5552 * IA32 arch specifies that at the time of processor reset the
5553 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005554 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005555 * state vmexit when "unrestricted guest" mode is turned on.
5556 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5557 * tree. Newer qemu binaries with that qemu fix would not need this
5558 * kvm hack.
5559 */
5560 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005561 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005562
Gleb Natapovf924d662012-12-12 19:10:55 +02005563 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005564
5565out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005566 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567}
5568
Avi Kivity6aa8b732006-12-10 02:21:36 -08005569static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5570{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005571 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005572
5573 *db = (ar >> 14) & 1;
5574 *l = (ar >> 13) & 1;
5575}
5576
Gleb Natapov89a27f42010-02-16 10:51:48 +02005577static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005578{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005579 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5580 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005581}
5582
Gleb Natapov89a27f42010-02-16 10:51:48 +02005583static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005584{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005585 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5586 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005587}
5588
Gleb Natapov89a27f42010-02-16 10:51:48 +02005589static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005590{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005591 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5592 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005593}
5594
Gleb Natapov89a27f42010-02-16 10:51:48 +02005595static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005596{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005597 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5598 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005599}
5600
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005601static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5602{
5603 struct kvm_segment var;
5604 u32 ar;
5605
5606 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005607 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005608 if (seg == VCPU_SREG_CS)
5609 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005610 ar = vmx_segment_access_rights(&var);
5611
5612 if (var.base != (var.selector << 4))
5613 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005614 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005615 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005616 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005617 return false;
5618
5619 return true;
5620}
5621
5622static bool code_segment_valid(struct kvm_vcpu *vcpu)
5623{
5624 struct kvm_segment cs;
5625 unsigned int cs_rpl;
5626
5627 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005628 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005629
Avi Kivity1872a3f2009-01-04 23:26:52 +02005630 if (cs.unusable)
5631 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005632 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005633 return false;
5634 if (!cs.s)
5635 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005636 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005637 if (cs.dpl > cs_rpl)
5638 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005639 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005640 if (cs.dpl != cs_rpl)
5641 return false;
5642 }
5643 if (!cs.present)
5644 return false;
5645
5646 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5647 return true;
5648}
5649
5650static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5651{
5652 struct kvm_segment ss;
5653 unsigned int ss_rpl;
5654
5655 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005656 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005657
Avi Kivity1872a3f2009-01-04 23:26:52 +02005658 if (ss.unusable)
5659 return true;
5660 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005661 return false;
5662 if (!ss.s)
5663 return false;
5664 if (ss.dpl != ss_rpl) /* DPL != RPL */
5665 return false;
5666 if (!ss.present)
5667 return false;
5668
5669 return true;
5670}
5671
5672static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5673{
5674 struct kvm_segment var;
5675 unsigned int rpl;
5676
5677 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005678 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005679
Avi Kivity1872a3f2009-01-04 23:26:52 +02005680 if (var.unusable)
5681 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005682 if (!var.s)
5683 return false;
5684 if (!var.present)
5685 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005686 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005687 if (var.dpl < rpl) /* DPL < RPL */
5688 return false;
5689 }
5690
5691 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5692 * rights flags
5693 */
5694 return true;
5695}
5696
5697static bool tr_valid(struct kvm_vcpu *vcpu)
5698{
5699 struct kvm_segment tr;
5700
5701 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5702
Avi Kivity1872a3f2009-01-04 23:26:52 +02005703 if (tr.unusable)
5704 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005705 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005706 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005707 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005708 return false;
5709 if (!tr.present)
5710 return false;
5711
5712 return true;
5713}
5714
5715static bool ldtr_valid(struct kvm_vcpu *vcpu)
5716{
5717 struct kvm_segment ldtr;
5718
5719 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5720
Avi Kivity1872a3f2009-01-04 23:26:52 +02005721 if (ldtr.unusable)
5722 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005723 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005724 return false;
5725 if (ldtr.type != 2)
5726 return false;
5727 if (!ldtr.present)
5728 return false;
5729
5730 return true;
5731}
5732
5733static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5734{
5735 struct kvm_segment cs, ss;
5736
5737 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5738 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5739
Nadav Amitb32a9912015-03-29 16:33:04 +03005740 return ((cs.selector & SEGMENT_RPL_MASK) ==
5741 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005742}
5743
5744/*
5745 * Check if guest state is valid. Returns true if valid, false if
5746 * not.
5747 * We assume that registers are always usable
5748 */
5749static bool guest_state_valid(struct kvm_vcpu *vcpu)
5750{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005751 if (enable_unrestricted_guest)
5752 return true;
5753
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005754 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005755 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005756 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5757 return false;
5758 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5759 return false;
5760 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5761 return false;
5762 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5763 return false;
5764 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5765 return false;
5766 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5767 return false;
5768 } else {
5769 /* protected mode guest state checks */
5770 if (!cs_ss_rpl_check(vcpu))
5771 return false;
5772 if (!code_segment_valid(vcpu))
5773 return false;
5774 if (!stack_segment_valid(vcpu))
5775 return false;
5776 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5777 return false;
5778 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5779 return false;
5780 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5781 return false;
5782 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5783 return false;
5784 if (!tr_valid(vcpu))
5785 return false;
5786 if (!ldtr_valid(vcpu))
5787 return false;
5788 }
5789 /* TODO:
5790 * - Add checks on RIP
5791 * - Add checks on RFLAGS
5792 */
5793
5794 return true;
5795}
5796
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005797static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5798{
5799 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5800}
5801
Mike Dayd77c26f2007-10-08 09:02:08 -04005802static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005803{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005804 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005805 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005806 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005807
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005808 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005809 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005810 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5811 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005812 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005813 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005814 r = kvm_write_guest_page(kvm, fn++, &data,
5815 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005816 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005817 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005818 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5819 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005820 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005821 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5822 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005823 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005824 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005825 r = kvm_write_guest_page(kvm, fn, &data,
5826 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5827 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005828out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005829 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005830 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005831}
5832
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005833static int init_rmode_identity_map(struct kvm *kvm)
5834{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005835 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005836 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005837 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005838 u32 tmp;
5839
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005840 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005841 mutex_lock(&kvm->slots_lock);
5842
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005843 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005844 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005845
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005846 if (!kvm_vmx->ept_identity_map_addr)
5847 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5848 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005849
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005850 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005851 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005852 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005853 goto out2;
5854
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005855 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005856 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5857 if (r < 0)
5858 goto out;
5859 /* Set up identity-mapping pagetable for EPT in real mode */
5860 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5861 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5862 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5863 r = kvm_write_guest_page(kvm, identity_map_pfn,
5864 &tmp, i * sizeof(tmp), sizeof(tmp));
5865 if (r < 0)
5866 goto out;
5867 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005868 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005869
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005870out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005871 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005872
5873out2:
5874 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005875 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005876}
5877
Avi Kivity6aa8b732006-12-10 02:21:36 -08005878static void seg_setup(int seg)
5879{
Mathias Krause772e0312012-08-30 01:30:19 +02005880 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005881 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005882
5883 vmcs_write16(sf->selector, 0);
5884 vmcs_writel(sf->base, 0);
5885 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005886 ar = 0x93;
5887 if (seg == VCPU_SREG_CS)
5888 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005889
5890 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005891}
5892
Sheng Yangf78e0e22007-10-29 09:40:42 +08005893static int alloc_apic_access_page(struct kvm *kvm)
5894{
Xiao Guangrong44841412012-09-07 14:14:20 +08005895 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005896 int r = 0;
5897
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005898 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005899 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005900 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005901 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5902 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005903 if (r)
5904 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005905
Tang Chen73a6d942014-09-11 13:38:00 +08005906 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005907 if (is_error_page(page)) {
5908 r = -EFAULT;
5909 goto out;
5910 }
5911
Tang Chenc24ae0d2014-09-24 15:57:58 +08005912 /*
5913 * Do not pin the page in memory, so that memory hot-unplug
5914 * is able to migrate it.
5915 */
5916 put_page(page);
5917 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005918out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005919 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005920 return r;
5921}
5922
Wanpeng Li991e7a02015-09-16 17:30:05 +08005923static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005924{
5925 int vpid;
5926
Avi Kivity919818a2009-03-23 18:01:29 +02005927 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005928 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005929 spin_lock(&vmx_vpid_lock);
5930 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005931 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005932 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005933 else
5934 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005935 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005936 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005937}
5938
Wanpeng Li991e7a02015-09-16 17:30:05 +08005939static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005940{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005941 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005942 return;
5943 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005944 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005945 spin_unlock(&vmx_vpid_lock);
5946}
5947
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005948static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5949 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005950{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005951 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005952
5953 if (!cpu_has_vmx_msr_bitmap())
5954 return;
5955
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005956 if (static_branch_unlikely(&enable_evmcs))
5957 evmcs_touch_msr_bitmap();
5958
Sheng Yang25c5f222008-03-28 13:18:56 +08005959 /*
5960 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5961 * have the write-low and read-high bitmap offsets the wrong way round.
5962 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5963 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005964 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005965 if (type & MSR_TYPE_R)
5966 /* read-low */
5967 __clear_bit(msr, msr_bitmap + 0x000 / f);
5968
5969 if (type & MSR_TYPE_W)
5970 /* write-low */
5971 __clear_bit(msr, msr_bitmap + 0x800 / f);
5972
Sheng Yang25c5f222008-03-28 13:18:56 +08005973 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5974 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005975 if (type & MSR_TYPE_R)
5976 /* read-high */
5977 __clear_bit(msr, msr_bitmap + 0x400 / f);
5978
5979 if (type & MSR_TYPE_W)
5980 /* write-high */
5981 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5982
5983 }
5984}
5985
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005986static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5987 u32 msr, int type)
5988{
5989 int f = sizeof(unsigned long);
5990
5991 if (!cpu_has_vmx_msr_bitmap())
5992 return;
5993
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005994 if (static_branch_unlikely(&enable_evmcs))
5995 evmcs_touch_msr_bitmap();
5996
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005997 /*
5998 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5999 * have the write-low and read-high bitmap offsets the wrong way round.
6000 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6001 */
6002 if (msr <= 0x1fff) {
6003 if (type & MSR_TYPE_R)
6004 /* read-low */
6005 __set_bit(msr, msr_bitmap + 0x000 / f);
6006
6007 if (type & MSR_TYPE_W)
6008 /* write-low */
6009 __set_bit(msr, msr_bitmap + 0x800 / f);
6010
6011 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6012 msr &= 0x1fff;
6013 if (type & MSR_TYPE_R)
6014 /* read-high */
6015 __set_bit(msr, msr_bitmap + 0x400 / f);
6016
6017 if (type & MSR_TYPE_W)
6018 /* write-high */
6019 __set_bit(msr, msr_bitmap + 0xc00 / f);
6020
6021 }
6022}
6023
6024static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6025 u32 msr, int type, bool value)
6026{
6027 if (value)
6028 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6029 else
6030 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6031}
6032
Wincy Vanf2b93282015-02-03 23:56:03 +08006033/*
6034 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6035 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6036 */
6037static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6038 unsigned long *msr_bitmap_nested,
6039 u32 msr, int type)
6040{
6041 int f = sizeof(unsigned long);
6042
Wincy Vanf2b93282015-02-03 23:56:03 +08006043 /*
6044 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6045 * have the write-low and read-high bitmap offsets the wrong way round.
6046 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6047 */
6048 if (msr <= 0x1fff) {
6049 if (type & MSR_TYPE_R &&
6050 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6051 /* read-low */
6052 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6053
6054 if (type & MSR_TYPE_W &&
6055 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6056 /* write-low */
6057 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6058
6059 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6060 msr &= 0x1fff;
6061 if (type & MSR_TYPE_R &&
6062 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6063 /* read-high */
6064 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6065
6066 if (type & MSR_TYPE_W &&
6067 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6068 /* write-high */
6069 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6070
6071 }
6072}
6073
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006074static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006075{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006076 u8 mode = 0;
6077
6078 if (cpu_has_secondary_exec_ctrls() &&
6079 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6080 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6081 mode |= MSR_BITMAP_MODE_X2APIC;
6082 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6083 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6084 }
6085
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006086 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006087}
6088
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006089#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6090
6091static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6092 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006093{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006094 int msr;
6095
6096 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6097 unsigned word = msr / BITS_PER_LONG;
6098 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6099 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006100 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006101
6102 if (mode & MSR_BITMAP_MODE_X2APIC) {
6103 /*
6104 * TPR reads and writes can be virtualized even if virtual interrupt
6105 * delivery is not in use.
6106 */
6107 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6108 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6109 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6110 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6111 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6112 }
6113 }
6114}
6115
6116static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6117{
6118 struct vcpu_vmx *vmx = to_vmx(vcpu);
6119 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6120 u8 mode = vmx_msr_bitmap_mode(vcpu);
6121 u8 changed = mode ^ vmx->msr_bitmap_mode;
6122
6123 if (!changed)
6124 return;
6125
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006126 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6127 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6128
6129 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006130}
6131
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006132static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006133{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006134 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006135}
6136
David Matlackc9f04402017-08-01 14:00:40 -07006137static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6138{
6139 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6140 gfn_t gfn;
6141
6142 /*
6143 * Don't need to mark the APIC access page dirty; it is never
6144 * written to by the CPU during APIC virtualization.
6145 */
6146
6147 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6148 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6149 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6150 }
6151
6152 if (nested_cpu_has_posted_intr(vmcs12)) {
6153 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6154 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6155 }
6156}
6157
6158
David Hildenbrand6342c502017-01-25 11:58:58 +01006159static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006160{
6161 struct vcpu_vmx *vmx = to_vmx(vcpu);
6162 int max_irr;
6163 void *vapic_page;
6164 u16 status;
6165
David Matlackc9f04402017-08-01 14:00:40 -07006166 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6167 return;
Wincy Van705699a2015-02-03 23:58:17 +08006168
David Matlackc9f04402017-08-01 14:00:40 -07006169 vmx->nested.pi_pending = false;
6170 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6171 return;
Wincy Van705699a2015-02-03 23:58:17 +08006172
David Matlackc9f04402017-08-01 14:00:40 -07006173 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6174 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006175 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006176 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6177 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006178 kunmap(vmx->nested.virtual_apic_page);
6179
6180 status = vmcs_read16(GUEST_INTR_STATUS);
6181 if ((u8)max_irr > ((u8)status & 0xff)) {
6182 status &= ~0xff;
6183 status |= (u8)max_irr;
6184 vmcs_write16(GUEST_INTR_STATUS, status);
6185 }
6186 }
David Matlackc9f04402017-08-01 14:00:40 -07006187
6188 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006189}
6190
Paolo Bonzini7e712682018-10-03 13:44:26 +02006191static u8 vmx_get_rvi(void)
6192{
6193 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6194}
6195
Liran Alone6c67d82018-09-04 10:56:52 +03006196static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6197{
6198 struct vcpu_vmx *vmx = to_vmx(vcpu);
6199 void *vapic_page;
6200 u32 vppr;
6201 int rvi;
6202
6203 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6204 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6205 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6206 return false;
6207
Paolo Bonzini7e712682018-10-03 13:44:26 +02006208 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006209
6210 vapic_page = kmap(vmx->nested.virtual_apic_page);
6211 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6212 kunmap(vmx->nested.virtual_apic_page);
6213
6214 return ((rvi & 0xf0) > (vppr & 0xf0));
6215}
6216
Wincy Van06a55242017-04-28 13:13:59 +08006217static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6218 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006219{
6220#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006221 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6222
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006223 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006224 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006225 * The vector of interrupt to be delivered to vcpu had
6226 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006227 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006228 * Following cases will be reached in this block, and
6229 * we always send a notification event in all cases as
6230 * explained below.
6231 *
6232 * Case 1: vcpu keeps in non-root mode. Sending a
6233 * notification event posts the interrupt to vcpu.
6234 *
6235 * Case 2: vcpu exits to root mode and is still
6236 * runnable. PIR will be synced to vIRR before the
6237 * next vcpu entry. Sending a notification event in
6238 * this case has no effect, as vcpu is not in root
6239 * mode.
6240 *
6241 * Case 3: vcpu exits to root mode and is blocked.
6242 * vcpu_block() has already synced PIR to vIRR and
6243 * never blocks vcpu if vIRR is not cleared. Therefore,
6244 * a blocked vcpu here does not wait for any requested
6245 * interrupts in PIR, and sending a notification event
6246 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006247 */
Feng Wu28b835d2015-09-18 22:29:54 +08006248
Wincy Van06a55242017-04-28 13:13:59 +08006249 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006250 return true;
6251 }
6252#endif
6253 return false;
6254}
6255
Wincy Van705699a2015-02-03 23:58:17 +08006256static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6257 int vector)
6258{
6259 struct vcpu_vmx *vmx = to_vmx(vcpu);
6260
6261 if (is_guest_mode(vcpu) &&
6262 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006263 /*
6264 * If a posted intr is not recognized by hardware,
6265 * we will accomplish it in the next vmentry.
6266 */
6267 vmx->nested.pi_pending = true;
6268 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006269 /* the PIR and ON have been set by L1. */
6270 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6271 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006272 return 0;
6273 }
6274 return -1;
6275}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006276/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006277 * Send interrupt to vcpu via posted interrupt way.
6278 * 1. If target vcpu is running(non-root mode), send posted interrupt
6279 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6280 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6281 * interrupt from PIR in next vmentry.
6282 */
6283static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6284{
6285 struct vcpu_vmx *vmx = to_vmx(vcpu);
6286 int r;
6287
Wincy Van705699a2015-02-03 23:58:17 +08006288 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6289 if (!r)
6290 return;
6291
Yang Zhanga20ed542013-04-11 19:25:15 +08006292 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6293 return;
6294
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006295 /* If a previous notification has sent the IPI, nothing to do. */
6296 if (pi_test_and_set_on(&vmx->pi_desc))
6297 return;
6298
Wincy Van06a55242017-04-28 13:13:59 +08006299 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006300 kvm_vcpu_kick(vcpu);
6301}
6302
Avi Kivity6aa8b732006-12-10 02:21:36 -08006303/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006304 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6305 * will not change in the lifetime of the guest.
6306 * Note that host-state that does change is set elsewhere. E.g., host-state
6307 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6308 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006309static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006310{
6311 u32 low32, high32;
6312 unsigned long tmpl;
6313 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006314 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006315
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006316 cr0 = read_cr0();
6317 WARN_ON(cr0 & X86_CR0_TS);
6318 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006319
6320 /*
6321 * Save the most likely value for this task's CR3 in the VMCS.
6322 * We can't use __get_current_cr3_fast() because we're not atomic.
6323 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006324 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006325 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006326 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006327
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006328 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006329 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006330 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006331 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006332
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006333 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006334#ifdef CONFIG_X86_64
6335 /*
6336 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006337 * vmx_prepare_switch_to_host(), in case userspace uses
6338 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006339 */
6340 vmcs_write16(HOST_DS_SELECTOR, 0);
6341 vmcs_write16(HOST_ES_SELECTOR, 0);
6342#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006343 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6344 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006345#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006346 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6347 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6348
Juergen Gross87930012017-09-04 12:25:27 +02006349 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006350 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006351 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006352
Avi Kivity83287ea422012-09-16 15:10:57 +03006353 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006354
6355 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6356 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6357 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6358 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6359
6360 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6361 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6362 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6363 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07006364
6365 if (cpu_has_load_ia32_efer)
6366 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006367}
6368
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006369static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6370{
6371 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6372 if (enable_ept)
6373 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006374 if (is_guest_mode(&vmx->vcpu))
6375 vmx->vcpu.arch.cr4_guest_owned_bits &=
6376 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006377 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6378}
6379
Yang Zhang01e439b2013-04-11 19:25:12 +08006380static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6381{
6382 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6383
Andrey Smetanind62caab2015-11-10 15:36:33 +03006384 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006385 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006386
6387 if (!enable_vnmi)
6388 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6389
Yunhong Jiang64672c92016-06-13 14:19:59 -07006390 /* Enable the preemption timer dynamically */
6391 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006392 return pin_based_exec_ctrl;
6393}
6394
Andrey Smetanind62caab2015-11-10 15:36:33 +03006395static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6396{
6397 struct vcpu_vmx *vmx = to_vmx(vcpu);
6398
6399 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006400 if (cpu_has_secondary_exec_ctrls()) {
6401 if (kvm_vcpu_apicv_active(vcpu))
6402 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6403 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6404 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6405 else
6406 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6407 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6408 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6409 }
6410
6411 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006412 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006413}
6414
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006415static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6416{
6417 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006418
6419 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6420 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6421
Paolo Bonzini35754c92015-07-29 12:05:37 +02006422 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006423 exec_control &= ~CPU_BASED_TPR_SHADOW;
6424#ifdef CONFIG_X86_64
6425 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6426 CPU_BASED_CR8_LOAD_EXITING;
6427#endif
6428 }
6429 if (!enable_ept)
6430 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6431 CPU_BASED_CR3_LOAD_EXITING |
6432 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006433 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6434 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6435 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006436 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6437 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006438 return exec_control;
6439}
6440
Jim Mattson45ec3682017-08-23 16:32:04 -07006441static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006442{
Jim Mattson45ec3682017-08-23 16:32:04 -07006443 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006444 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006445}
6446
Jim Mattson75f4fc82017-08-23 16:32:03 -07006447static bool vmx_rdseed_supported(void)
6448{
6449 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006450 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006451}
6452
Paolo Bonzini80154d72017-08-24 13:55:35 +02006453static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006454{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006455 struct kvm_vcpu *vcpu = &vmx->vcpu;
6456
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006457 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006458
Paolo Bonzini80154d72017-08-24 13:55:35 +02006459 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006460 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6461 if (vmx->vpid == 0)
6462 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6463 if (!enable_ept) {
6464 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6465 enable_unrestricted_guest = 0;
6466 }
6467 if (!enable_unrestricted_guest)
6468 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006469 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006470 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006471 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006472 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6473 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006474 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006475
6476 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6477 * in vmx_set_cr4. */
6478 exec_control &= ~SECONDARY_EXEC_DESC;
6479
Abel Gordonabc4fc52013-04-18 14:35:25 +03006480 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6481 (handle_vmptrld).
6482 We can NOT enable shadow_vmcs here because we don't have yet
6483 a current VMCS12
6484 */
6485 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006486
6487 if (!enable_pml)
6488 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006489
Paolo Bonzini3db13482017-08-24 14:48:03 +02006490 if (vmx_xsaves_supported()) {
6491 /* Exposing XSAVES only when XSAVE is exposed */
6492 bool xsaves_enabled =
6493 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6494 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6495
6496 if (!xsaves_enabled)
6497 exec_control &= ~SECONDARY_EXEC_XSAVES;
6498
6499 if (nested) {
6500 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006501 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006502 SECONDARY_EXEC_XSAVES;
6503 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006504 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006505 ~SECONDARY_EXEC_XSAVES;
6506 }
6507 }
6508
Paolo Bonzini80154d72017-08-24 13:55:35 +02006509 if (vmx_rdtscp_supported()) {
6510 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6511 if (!rdtscp_enabled)
6512 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6513
6514 if (nested) {
6515 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006516 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006517 SECONDARY_EXEC_RDTSCP;
6518 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006519 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006520 ~SECONDARY_EXEC_RDTSCP;
6521 }
6522 }
6523
6524 if (vmx_invpcid_supported()) {
6525 /* Exposing INVPCID only when PCID is exposed */
6526 bool invpcid_enabled =
6527 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6528 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6529
6530 if (!invpcid_enabled) {
6531 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6532 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6533 }
6534
6535 if (nested) {
6536 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006537 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006538 SECONDARY_EXEC_ENABLE_INVPCID;
6539 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006540 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006541 ~SECONDARY_EXEC_ENABLE_INVPCID;
6542 }
6543 }
6544
Jim Mattson45ec3682017-08-23 16:32:04 -07006545 if (vmx_rdrand_supported()) {
6546 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6547 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006548 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006549
6550 if (nested) {
6551 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006552 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006553 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006554 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006555 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006556 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006557 }
6558 }
6559
Jim Mattson75f4fc82017-08-23 16:32:03 -07006560 if (vmx_rdseed_supported()) {
6561 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6562 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006563 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006564
6565 if (nested) {
6566 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006567 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006568 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006569 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006570 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006571 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006572 }
6573 }
6574
Paolo Bonzini80154d72017-08-24 13:55:35 +02006575 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006576}
6577
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006578static void ept_set_mmio_spte_mask(void)
6579{
6580 /*
6581 * EPT Misconfigurations can be generated if the value of bits 2:0
6582 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006583 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006584 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6585 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006586}
6587
Wanpeng Lif53cd632014-12-02 19:14:58 +08006588#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006589/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006590 * Sets up the vmcs for emulated real mode.
6591 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006592static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006593{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006594 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006595
Abel Gordon4607c2d2013-04-18 14:35:55 +03006596 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006597 /*
6598 * At vCPU creation, "VMWRITE to any supported field
6599 * in the VMCS" is supported, so use the more
6600 * permissive vmx_vmread_bitmap to specify both read
6601 * and write permissions for the shadow VMCS.
6602 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006603 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006604 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006605 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006606 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006607 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006608
Avi Kivity6aa8b732006-12-10 02:21:36 -08006609 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6610
Avi Kivity6aa8b732006-12-10 02:21:36 -08006611 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006612 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006613 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006614
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006615 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006616
Dan Williamsdfa169b2016-06-02 11:17:24 -07006617 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006618 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006619 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006620 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006621 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006622
Andrey Smetanind62caab2015-11-10 15:36:33 +03006623 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006624 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6625 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6626 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6627 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6628
6629 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006630
Li RongQing0bcf2612015-12-03 13:29:34 +08006631 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006632 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006633 }
6634
Wanpeng Lib31c1142018-03-12 04:53:04 -07006635 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006636 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006637 vmx->ple_window = ple_window;
6638 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006639 }
6640
Xiao Guangrongc3707952011-07-12 03:28:04 +08006641 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6642 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006643 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6644
Avi Kivity9581d442010-10-19 16:46:55 +02006645 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6646 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006647 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006648 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6649 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006650
Bandan Das2a499e42017-08-03 15:54:41 -04006651 if (cpu_has_vmx_vmfunc())
6652 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6653
Eddie Dong2cc51562007-05-21 07:28:09 +03006654 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6655 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006656 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006657 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006658 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006659
Radim Krčmář74545702015-04-27 15:11:25 +02006660 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6661 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006662
Paolo Bonzini03916db2014-07-24 14:21:57 +02006663 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006664 u32 index = vmx_msr_index[i];
6665 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006666 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006667
6668 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6669 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006670 if (wrmsr_safe(index, data_low, data_high) < 0)
6671 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006672 vmx->guest_msrs[j].index = i;
6673 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006674 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006675 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006676 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006677
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006678 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006679
6680 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006681
6682 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006683 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006684
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006685 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6686 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6687
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006688 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006689
Wanpeng Lif53cd632014-12-02 19:14:58 +08006690 if (vmx_xsaves_supported())
6691 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6692
Peter Feiner4e595162016-07-07 14:49:58 -07006693 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006694 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6695 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6696 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006697
6698 if (cpu_has_vmx_encls_vmexit())
6699 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006700}
6701
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006702static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006703{
6704 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006705 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006706 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006707
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006708 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006709 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006710
Wanpeng Li518e7b92018-02-28 14:03:31 +08006711 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006712 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006713 kvm_set_cr8(vcpu, 0);
6714
6715 if (!init_event) {
6716 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6717 MSR_IA32_APICBASE_ENABLE;
6718 if (kvm_vcpu_is_reset_bsp(vcpu))
6719 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6720 apic_base_msr.host_initiated = true;
6721 kvm_set_apic_base(vcpu, &apic_base_msr);
6722 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006723
Avi Kivity2fb92db2011-04-27 19:42:18 +03006724 vmx_segment_cache_clear(vmx);
6725
Avi Kivity5706be02008-08-20 15:07:31 +03006726 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006727 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006728 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006729
6730 seg_setup(VCPU_SREG_DS);
6731 seg_setup(VCPU_SREG_ES);
6732 seg_setup(VCPU_SREG_FS);
6733 seg_setup(VCPU_SREG_GS);
6734 seg_setup(VCPU_SREG_SS);
6735
6736 vmcs_write16(GUEST_TR_SELECTOR, 0);
6737 vmcs_writel(GUEST_TR_BASE, 0);
6738 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6739 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6740
6741 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6742 vmcs_writel(GUEST_LDTR_BASE, 0);
6743 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6744 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6745
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006746 if (!init_event) {
6747 vmcs_write32(GUEST_SYSENTER_CS, 0);
6748 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6749 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6750 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6751 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006752
Wanpeng Lic37c2872017-11-20 14:52:21 -08006753 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006754 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006755
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006756 vmcs_writel(GUEST_GDTR_BASE, 0);
6757 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6758
6759 vmcs_writel(GUEST_IDTR_BASE, 0);
6760 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6761
Anthony Liguori443381a2010-12-06 10:53:38 -06006762 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006763 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006764 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006765 if (kvm_mpx_supported())
6766 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006767
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006768 setup_msrs(vmx);
6769
Avi Kivity6aa8b732006-12-10 02:21:36 -08006770 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6771
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006772 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006773 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006774 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006775 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006776 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006777 vmcs_write32(TPR_THRESHOLD, 0);
6778 }
6779
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006780 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006781
Sheng Yang2384d2b2008-01-17 15:14:33 +08006782 if (vmx->vpid != 0)
6783 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6784
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006785 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006786 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006787 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006788 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006789 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006790
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006791 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006792
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006793 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006794 if (init_event)
6795 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006796}
6797
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006798/*
6799 * In nested virtualization, check if L1 asked to exit on external interrupts.
6800 * For most existing hypervisors, this will always return true.
6801 */
6802static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6803{
6804 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6805 PIN_BASED_EXT_INTR_MASK;
6806}
6807
Bandan Das77b0f5d2014-04-19 18:17:45 -04006808/*
6809 * In nested virtualization, check if L1 has set
6810 * VM_EXIT_ACK_INTR_ON_EXIT
6811 */
6812static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6813{
6814 return get_vmcs12(vcpu)->vm_exit_controls &
6815 VM_EXIT_ACK_INTR_ON_EXIT;
6816}
6817
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006818static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6819{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006820 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006821}
6822
Jan Kiszkac9a79532014-03-07 20:03:15 +01006823static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006824{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006825 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6826 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006827}
6828
Jan Kiszkac9a79532014-03-07 20:03:15 +01006829static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006830{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006831 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006832 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006833 enable_irq_window(vcpu);
6834 return;
6835 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006836
Paolo Bonzini47c01522016-12-19 11:44:07 +01006837 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6838 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006839}
6840
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006841static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006842{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006843 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006844 uint32_t intr;
6845 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006846
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006847 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006848
Avi Kivityfa89a812008-09-01 15:57:51 +03006849 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006850 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006851 int inc_eip = 0;
6852 if (vcpu->arch.interrupt.soft)
6853 inc_eip = vcpu->arch.event_exit_inst_len;
6854 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006855 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006856 return;
6857 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006858 intr = irq | INTR_INFO_VALID_MASK;
6859 if (vcpu->arch.interrupt.soft) {
6860 intr |= INTR_TYPE_SOFT_INTR;
6861 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6862 vmx->vcpu.arch.event_exit_inst_len);
6863 } else
6864 intr |= INTR_TYPE_EXT_INTR;
6865 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006866
6867 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006868}
6869
Sheng Yangf08864b2008-05-15 18:23:25 +08006870static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6871{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006872 struct vcpu_vmx *vmx = to_vmx(vcpu);
6873
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006874 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006875 /*
6876 * Tracking the NMI-blocked state in software is built upon
6877 * finding the next open IRQ window. This, in turn, depends on
6878 * well-behaving guests: They have to keep IRQs disabled at
6879 * least as long as the NMI handler runs. Otherwise we may
6880 * cause NMI nesting, maybe breaking the guest. But as this is
6881 * highly unlikely, we can live with the residual risk.
6882 */
6883 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6884 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6885 }
6886
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006887 ++vcpu->stat.nmi_injections;
6888 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006889
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006890 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006891 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006892 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006893 return;
6894 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006895
Sheng Yangf08864b2008-05-15 18:23:25 +08006896 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6897 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006898
6899 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006900}
6901
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006902static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6903{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006904 struct vcpu_vmx *vmx = to_vmx(vcpu);
6905 bool masked;
6906
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006907 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006908 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006909 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006910 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006911 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6912 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6913 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006914}
6915
6916static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6917{
6918 struct vcpu_vmx *vmx = to_vmx(vcpu);
6919
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006920 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006921 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6922 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6923 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6924 }
6925 } else {
6926 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6927 if (masked)
6928 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6929 GUEST_INTR_STATE_NMI);
6930 else
6931 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6932 GUEST_INTR_STATE_NMI);
6933 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006934}
6935
Jan Kiszka2505dc92013-04-14 12:12:47 +02006936static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6937{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006938 if (to_vmx(vcpu)->nested.nested_run_pending)
6939 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006940
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006941 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006942 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6943 return 0;
6944
Jan Kiszka2505dc92013-04-14 12:12:47 +02006945 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6946 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6947 | GUEST_INTR_STATE_NMI));
6948}
6949
Gleb Natapov78646122009-03-23 12:12:11 +02006950static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6951{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006952 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6953 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006954 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6955 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006956}
6957
Izik Eiduscbc94022007-10-25 00:29:55 +02006958static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6959{
6960 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006961
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006962 if (enable_unrestricted_guest)
6963 return 0;
6964
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006965 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6966 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006967 if (ret)
6968 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006969 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006970 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006971}
6972
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006973static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6974{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006975 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006976 return 0;
6977}
6978
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006979static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006980{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006981 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006982 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006983 /*
6984 * Update instruction length as we may reinject the exception
6985 * from user space while in guest debugging mode.
6986 */
6987 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6988 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006989 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006990 return false;
6991 /* fall through */
6992 case DB_VECTOR:
6993 if (vcpu->guest_debug &
6994 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6995 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006996 /* fall through */
6997 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006998 case OF_VECTOR:
6999 case BR_VECTOR:
7000 case UD_VECTOR:
7001 case DF_VECTOR:
7002 case SS_VECTOR:
7003 case GP_VECTOR:
7004 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007005 return true;
7006 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02007007 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007008 return false;
7009}
7010
7011static int handle_rmode_exception(struct kvm_vcpu *vcpu,
7012 int vec, u32 err_code)
7013{
7014 /*
7015 * Instruction with address size override prefix opcode 0x67
7016 * Cause the #SS fault with 0 error code in VM86 mode.
7017 */
7018 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007019 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007020 if (vcpu->arch.halt_request) {
7021 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007022 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007023 }
7024 return 1;
7025 }
7026 return 0;
7027 }
7028
7029 /*
7030 * Forward all other exceptions that are valid in real mode.
7031 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7032 * the required debugging infrastructure rework.
7033 */
7034 kvm_queue_exception(vcpu, vec);
7035 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007036}
7037
Andi Kleena0861c02009-06-08 17:37:09 +08007038/*
7039 * Trigger machine check on the host. We assume all the MSRs are already set up
7040 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7041 * We pass a fake environment to the machine check handler because we want
7042 * the guest to be always treated like user space, no matter what context
7043 * it used internally.
7044 */
7045static void kvm_machine_check(void)
7046{
7047#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7048 struct pt_regs regs = {
7049 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7050 .flags = X86_EFLAGS_IF,
7051 };
7052
7053 do_machine_check(&regs, 0);
7054#endif
7055}
7056
Avi Kivity851ba692009-08-24 11:10:17 +03007057static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007058{
7059 /* already handled by vcpu_run */
7060 return 1;
7061}
7062
Avi Kivity851ba692009-08-24 11:10:17 +03007063static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007064{
Avi Kivity1155f762007-11-22 11:30:47 +02007065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007066 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007067 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007068 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007069 u32 vect_info;
7070 enum emulation_result er;
7071
Avi Kivity1155f762007-11-22 11:30:47 +02007072 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007073 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007074
Andi Kleena0861c02009-06-08 17:37:09 +08007075 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007076 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007077
Jim Mattsonef85b672016-12-12 11:01:37 -08007078 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007079 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007080
Wanpeng Li082d06e2018-04-03 16:28:48 -07007081 if (is_invalid_opcode(intr_info))
7082 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007083
Avi Kivity6aa8b732006-12-10 02:21:36 -08007084 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007085 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007086 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007087
Liran Alon9e869482018-03-12 13:12:51 +02007088 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7089 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007090 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007091 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7092 if (er == EMULATE_USER_EXIT)
7093 return 0;
7094 else if (er != EMULATE_DONE)
7095 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7096 return 1;
7097 }
7098
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007099 /*
7100 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7101 * MMIO, it is better to report an internal error.
7102 * See the comments in vmx_handle_exit.
7103 */
7104 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7105 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7106 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7107 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007108 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007109 vcpu->run->internal.data[0] = vect_info;
7110 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007111 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007112 return 0;
7113 }
7114
Avi Kivity6aa8b732006-12-10 02:21:36 -08007115 if (is_page_fault(intr_info)) {
7116 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007117 /* EPT won't cause page fault directly */
7118 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007119 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120 }
7121
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007122 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007123
7124 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7125 return handle_rmode_exception(vcpu, ex_no, error_code);
7126
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007127 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007128 case AC_VECTOR:
7129 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7130 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007131 case DB_VECTOR:
7132 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7133 if (!(vcpu->guest_debug &
7134 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007135 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007136 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007137 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007138 skip_emulated_instruction(vcpu);
7139
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007140 kvm_queue_exception(vcpu, DB_VECTOR);
7141 return 1;
7142 }
7143 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7144 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7145 /* fall through */
7146 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01007147 /*
7148 * Update instruction length as we may reinject #BP from
7149 * user space while in guest debugging mode. Reading it for
7150 * #DB as well causes no harm, it is not used in that case.
7151 */
7152 vmx->vcpu.arch.event_exit_inst_len =
7153 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007154 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007155 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007156 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7157 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007158 break;
7159 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007160 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7161 kvm_run->ex.exception = ex_no;
7162 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007163 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007164 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007165 return 0;
7166}
7167
Avi Kivity851ba692009-08-24 11:10:17 +03007168static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007169{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007170 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007171 return 1;
7172}
7173
Avi Kivity851ba692009-08-24 11:10:17 +03007174static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007175{
Avi Kivity851ba692009-08-24 11:10:17 +03007176 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007177 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007178 return 0;
7179}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180
Avi Kivity851ba692009-08-24 11:10:17 +03007181static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007182{
He, Qingbfdaab02007-09-12 14:18:28 +08007183 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007184 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007185 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007186
He, Qingbfdaab02007-09-12 14:18:28 +08007187 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007188 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007189
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007190 ++vcpu->stat.io_exits;
7191
Sean Christopherson432baf62018-03-08 08:57:26 -08007192 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007193 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007194
7195 port = exit_qualification >> 16;
7196 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007197 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007198
Sean Christophersondca7f122018-03-08 08:57:27 -08007199 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007200}
7201
Ingo Molnar102d8322007-02-19 14:37:47 +02007202static void
7203vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7204{
7205 /*
7206 * Patch in the VMCALL instruction:
7207 */
7208 hypercall[0] = 0x0f;
7209 hypercall[1] = 0x01;
7210 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007211}
7212
Guo Chao0fa06072012-06-28 15:16:19 +08007213/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007214static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7215{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007216 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007217 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7218 unsigned long orig_val = val;
7219
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007220 /*
7221 * We get here when L2 changed cr0 in a way that did not change
7222 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007223 * but did change L0 shadowed bits. So we first calculate the
7224 * effective cr0 value that L1 would like to write into the
7225 * hardware. It consists of the L2-owned bits from the new
7226 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007227 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007228 val = (val & ~vmcs12->cr0_guest_host_mask) |
7229 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7230
David Matlack38991522016-11-29 18:14:08 -08007231 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007232 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007233
7234 if (kvm_set_cr0(vcpu, val))
7235 return 1;
7236 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007237 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007238 } else {
7239 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007240 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007241 return 1;
David Matlack38991522016-11-29 18:14:08 -08007242
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007243 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007244 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007245}
7246
7247static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7248{
7249 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007250 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7251 unsigned long orig_val = val;
7252
7253 /* analogously to handle_set_cr0 */
7254 val = (val & ~vmcs12->cr4_guest_host_mask) |
7255 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7256 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007257 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007258 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007259 return 0;
7260 } else
7261 return kvm_set_cr4(vcpu, val);
7262}
7263
Paolo Bonzini0367f202016-07-12 10:44:55 +02007264static int handle_desc(struct kvm_vcpu *vcpu)
7265{
7266 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007267 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007268}
7269
Avi Kivity851ba692009-08-24 11:10:17 +03007270static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007271{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007272 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007273 int cr;
7274 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007275 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007276 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007277
He, Qingbfdaab02007-09-12 14:18:28 +08007278 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007279 cr = exit_qualification & 15;
7280 reg = (exit_qualification >> 8) & 15;
7281 switch ((exit_qualification >> 4) & 3) {
7282 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007283 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007284 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007285 switch (cr) {
7286 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007287 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007288 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007290 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007291 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007292 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007293 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007294 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007295 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007296 case 8: {
7297 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007298 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007299 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007300 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007301 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007302 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007303 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007304 return ret;
7305 /*
7306 * TODO: we might be squashing a
7307 * KVM_GUESTDBG_SINGLESTEP-triggered
7308 * KVM_EXIT_DEBUG here.
7309 */
Avi Kivity851ba692009-08-24 11:10:17 +03007310 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007311 return 0;
7312 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007313 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007314 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007315 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007316 WARN_ONCE(1, "Guest should always own CR0.TS");
7317 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007318 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007319 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007320 case 1: /*mov from cr*/
7321 switch (cr) {
7322 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007323 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007324 val = kvm_read_cr3(vcpu);
7325 kvm_register_write(vcpu, reg, val);
7326 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007327 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007328 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007329 val = kvm_get_cr8(vcpu);
7330 kvm_register_write(vcpu, reg, val);
7331 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007332 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007333 }
7334 break;
7335 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007336 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007337 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007338 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007339
Kyle Huey6affcbe2016-11-29 12:40:40 -08007340 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007341 default:
7342 break;
7343 }
Avi Kivity851ba692009-08-24 11:10:17 +03007344 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007345 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007346 (int)(exit_qualification >> 4) & 3, cr);
7347 return 0;
7348}
7349
Avi Kivity851ba692009-08-24 11:10:17 +03007350static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007351{
He, Qingbfdaab02007-09-12 14:18:28 +08007352 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007353 int dr, dr7, reg;
7354
7355 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7356 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7357
7358 /* First, if DR does not exist, trigger UD */
7359 if (!kvm_require_dr(vcpu, dr))
7360 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007361
Jan Kiszkaf2483412010-01-20 18:20:20 +01007362 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007363 if (!kvm_require_cpl(vcpu, 0))
7364 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007365 dr7 = vmcs_readl(GUEST_DR7);
7366 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007367 /*
7368 * As the vm-exit takes precedence over the debug trap, we
7369 * need to emulate the latter, either for the host or the
7370 * guest debugging itself.
7371 */
7372 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007373 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007374 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007375 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007376 vcpu->run->debug.arch.exception = DB_VECTOR;
7377 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007378 return 0;
7379 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007380 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007381 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007382 kvm_queue_exception(vcpu, DB_VECTOR);
7383 return 1;
7384 }
7385 }
7386
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007387 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007388 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7389 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007390
7391 /*
7392 * No more DR vmexits; force a reload of the debug registers
7393 * and reenter on this instruction. The next vmexit will
7394 * retrieve the full state of the debug registers.
7395 */
7396 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7397 return 1;
7398 }
7399
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007400 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7401 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007402 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007403
7404 if (kvm_get_dr(vcpu, dr, &val))
7405 return 1;
7406 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007407 } else
Nadav Amit57773922014-06-18 17:19:23 +03007408 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007409 return 1;
7410
Kyle Huey6affcbe2016-11-29 12:40:40 -08007411 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007412}
7413
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007414static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7415{
7416 return vcpu->arch.dr6;
7417}
7418
7419static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7420{
7421}
7422
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007423static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7424{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007425 get_debugreg(vcpu->arch.db[0], 0);
7426 get_debugreg(vcpu->arch.db[1], 1);
7427 get_debugreg(vcpu->arch.db[2], 2);
7428 get_debugreg(vcpu->arch.db[3], 3);
7429 get_debugreg(vcpu->arch.dr6, 6);
7430 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7431
7432 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007433 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007434}
7435
Gleb Natapov020df072010-04-13 10:05:23 +03007436static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7437{
7438 vmcs_writel(GUEST_DR7, val);
7439}
7440
Avi Kivity851ba692009-08-24 11:10:17 +03007441static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007442{
Kyle Huey6a908b62016-11-29 12:40:37 -08007443 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007444}
7445
Avi Kivity851ba692009-08-24 11:10:17 +03007446static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007447{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007448 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007449 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007450
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007451 msr_info.index = ecx;
7452 msr_info.host_initiated = false;
7453 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007454 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007455 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007456 return 1;
7457 }
7458
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007459 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007460
Avi Kivity6aa8b732006-12-10 02:21:36 -08007461 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007462 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7463 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007464 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007465}
7466
Avi Kivity851ba692009-08-24 11:10:17 +03007467static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007468{
Will Auld8fe8ab42012-11-29 12:42:12 -08007469 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007470 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7471 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7472 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007473
Will Auld8fe8ab42012-11-29 12:42:12 -08007474 msr.data = data;
7475 msr.index = ecx;
7476 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007477 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007478 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007479 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007480 return 1;
7481 }
7482
Avi Kivity59200272010-01-25 19:47:02 +02007483 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007484 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007485}
7486
Avi Kivity851ba692009-08-24 11:10:17 +03007487static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007488{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007489 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007490 return 1;
7491}
7492
Avi Kivity851ba692009-08-24 11:10:17 +03007493static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007494{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007495 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7496 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007497
Avi Kivity3842d132010-07-27 12:30:24 +03007498 kvm_make_request(KVM_REQ_EVENT, vcpu);
7499
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007500 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007501 return 1;
7502}
7503
Avi Kivity851ba692009-08-24 11:10:17 +03007504static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007505{
Avi Kivityd3bef152007-06-05 15:53:05 +03007506 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007507}
7508
Avi Kivity851ba692009-08-24 11:10:17 +03007509static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007510{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007511 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007512}
7513
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007514static int handle_invd(struct kvm_vcpu *vcpu)
7515{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007516 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007517}
7518
Avi Kivity851ba692009-08-24 11:10:17 +03007519static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007520{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007521 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007522
7523 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007524 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007525}
7526
Avi Kivityfee84b02011-11-10 14:57:25 +02007527static int handle_rdpmc(struct kvm_vcpu *vcpu)
7528{
7529 int err;
7530
7531 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007532 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007533}
7534
Avi Kivity851ba692009-08-24 11:10:17 +03007535static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007536{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007537 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007538}
7539
Dexuan Cui2acf9232010-06-10 11:27:12 +08007540static int handle_xsetbv(struct kvm_vcpu *vcpu)
7541{
7542 u64 new_bv = kvm_read_edx_eax(vcpu);
7543 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7544
7545 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007546 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007547 return 1;
7548}
7549
Wanpeng Lif53cd632014-12-02 19:14:58 +08007550static int handle_xsaves(struct kvm_vcpu *vcpu)
7551{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007552 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007553 WARN(1, "this should never happen\n");
7554 return 1;
7555}
7556
7557static int handle_xrstors(struct kvm_vcpu *vcpu)
7558{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007559 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007560 WARN(1, "this should never happen\n");
7561 return 1;
7562}
7563
Avi Kivity851ba692009-08-24 11:10:17 +03007564static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007565{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007566 if (likely(fasteoi)) {
7567 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7568 int access_type, offset;
7569
7570 access_type = exit_qualification & APIC_ACCESS_TYPE;
7571 offset = exit_qualification & APIC_ACCESS_OFFSET;
7572 /*
7573 * Sane guest uses MOV to write EOI, with written value
7574 * not cared. So make a short-circuit here by avoiding
7575 * heavy instruction emulation.
7576 */
7577 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7578 (offset == APIC_EOI)) {
7579 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007580 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007581 }
7582 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007583 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007584}
7585
Yang Zhangc7c9c562013-01-25 10:18:51 +08007586static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7587{
7588 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7589 int vector = exit_qualification & 0xff;
7590
7591 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7592 kvm_apic_set_eoi_accelerated(vcpu, vector);
7593 return 1;
7594}
7595
Yang Zhang83d4c282013-01-25 10:18:49 +08007596static int handle_apic_write(struct kvm_vcpu *vcpu)
7597{
7598 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7599 u32 offset = exit_qualification & 0xfff;
7600
7601 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7602 kvm_apic_write_nodecode(vcpu, offset);
7603 return 1;
7604}
7605
Avi Kivity851ba692009-08-24 11:10:17 +03007606static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007607{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007608 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007609 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007610 bool has_error_code = false;
7611 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007612 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007613 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007614
7615 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007616 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007617 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007618
7619 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7620
7621 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007622 if (reason == TASK_SWITCH_GATE && idt_v) {
7623 switch (type) {
7624 case INTR_TYPE_NMI_INTR:
7625 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007626 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007627 break;
7628 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007629 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007630 kvm_clear_interrupt_queue(vcpu);
7631 break;
7632 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007633 if (vmx->idt_vectoring_info &
7634 VECTORING_INFO_DELIVER_CODE_MASK) {
7635 has_error_code = true;
7636 error_code =
7637 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7638 }
7639 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007640 case INTR_TYPE_SOFT_EXCEPTION:
7641 kvm_clear_exception_queue(vcpu);
7642 break;
7643 default:
7644 break;
7645 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007646 }
Izik Eidus37817f22008-03-24 23:14:53 +02007647 tss_selector = exit_qualification;
7648
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007649 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7650 type != INTR_TYPE_EXT_INTR &&
7651 type != INTR_TYPE_NMI_INTR))
7652 skip_emulated_instruction(vcpu);
7653
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007654 if (kvm_task_switch(vcpu, tss_selector,
7655 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7656 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007657 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7658 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7659 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007660 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007661 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007662
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007663 /*
7664 * TODO: What about debug traps on tss switch?
7665 * Are we supposed to inject them and update dr6?
7666 */
7667
7668 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007669}
7670
Avi Kivity851ba692009-08-24 11:10:17 +03007671static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007672{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007673 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007674 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007675 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007676
Sheng Yangf9c617f2009-03-25 10:08:52 +08007677 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007678
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007679 /*
7680 * EPT violation happened while executing iret from NMI,
7681 * "blocked by NMI" bit has to be set before next VM entry.
7682 * There are errata that may cause this bit to not be set:
7683 * AAK134, BY25.
7684 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007685 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007686 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007687 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007688 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7689
Sheng Yang14394422008-04-28 12:24:45 +08007690 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007691 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007692
Junaid Shahid27959a42016-12-06 16:46:10 -08007693 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007694 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007695 ? PFERR_USER_MASK : 0;
7696 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007697 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007698 ? PFERR_WRITE_MASK : 0;
7699 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007700 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007701 ? PFERR_FETCH_MASK : 0;
7702 /* ept page table entry is present? */
7703 error_code |= (exit_qualification &
7704 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7705 EPT_VIOLATION_EXECUTABLE))
7706 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007707
Paolo Bonzinieebed242016-11-28 14:39:58 +01007708 error_code |= (exit_qualification & 0x100) != 0 ?
7709 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007710
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007711 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007712 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007713}
7714
Avi Kivity851ba692009-08-24 11:10:17 +03007715static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007716{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007717 gpa_t gpa;
7718
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007719 /*
7720 * A nested guest cannot optimize MMIO vmexits, because we have an
7721 * nGPA here instead of the required GPA.
7722 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007723 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007724 if (!is_guest_mode(vcpu) &&
7725 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007726 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007727 /*
7728 * Doing kvm_skip_emulated_instruction() depends on undefined
7729 * behavior: Intel's manual doesn't mandate
7730 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7731 * occurs and while on real hardware it was observed to be set,
7732 * other hypervisors (namely Hyper-V) don't set it, we end up
7733 * advancing IP with some random value. Disable fast mmio when
7734 * running nested and keep it for real hardware in hope that
7735 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7736 */
7737 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7738 return kvm_skip_emulated_instruction(vcpu);
7739 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007740 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007741 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007742 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007743
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007744 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007745}
7746
Avi Kivity851ba692009-08-24 11:10:17 +03007747static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007748{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007749 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007750 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7751 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007752 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007753 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007754
7755 return 1;
7756}
7757
Mohammed Gamal80ced182009-09-01 12:48:18 +02007758static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007759{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007760 struct vcpu_vmx *vmx = to_vmx(vcpu);
7761 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007762 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007763 u32 cpu_exec_ctrl;
7764 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007765 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007766
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007767 /*
7768 * We should never reach the point where we are emulating L2
7769 * due to invalid guest state as that means we incorrectly
7770 * allowed a nested VMEntry with an invalid vmcs12.
7771 */
7772 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7773
Avi Kivity49e9d552010-09-19 14:34:08 +02007774 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7775 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007776
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007777 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007778 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007779 return handle_interrupt_window(&vmx->vcpu);
7780
Radim Krčmář72875d82017-04-26 22:32:19 +02007781 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007782 return 1;
7783
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007784 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007785
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007786 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007787 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007788 ret = 0;
7789 goto out;
7790 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007791
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007792 if (err != EMULATE_DONE)
7793 goto emulation_error;
7794
7795 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7796 vcpu->arch.exception.pending)
7797 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007798
Gleb Natapov8d76c492013-05-08 18:38:44 +03007799 if (vcpu->arch.halt_request) {
7800 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007801 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007802 goto out;
7803 }
7804
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007805 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007806 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007807 if (need_resched())
7808 schedule();
7809 }
7810
Mohammed Gamal80ced182009-09-01 12:48:18 +02007811out:
7812 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007813
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007814emulation_error:
7815 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7816 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7817 vcpu->run->internal.ndata = 0;
7818 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007819}
7820
7821static void grow_ple_window(struct kvm_vcpu *vcpu)
7822{
7823 struct vcpu_vmx *vmx = to_vmx(vcpu);
7824 int old = vmx->ple_window;
7825
Babu Mogerc8e88712018-03-16 16:37:24 -04007826 vmx->ple_window = __grow_ple_window(old, ple_window,
7827 ple_window_grow,
7828 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007829
7830 if (vmx->ple_window != old)
7831 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007832
7833 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007834}
7835
7836static void shrink_ple_window(struct kvm_vcpu *vcpu)
7837{
7838 struct vcpu_vmx *vmx = to_vmx(vcpu);
7839 int old = vmx->ple_window;
7840
Babu Mogerc8e88712018-03-16 16:37:24 -04007841 vmx->ple_window = __shrink_ple_window(old, ple_window,
7842 ple_window_shrink,
7843 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007844
7845 if (vmx->ple_window != old)
7846 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007847
7848 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007849}
7850
7851/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007852 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7853 */
7854static void wakeup_handler(void)
7855{
7856 struct kvm_vcpu *vcpu;
7857 int cpu = smp_processor_id();
7858
7859 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7860 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7861 blocked_vcpu_list) {
7862 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7863
7864 if (pi_test_on(pi_desc) == 1)
7865 kvm_vcpu_kick(vcpu);
7866 }
7867 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7868}
7869
Peng Haoe01bca22018-04-07 05:47:32 +08007870static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007871{
7872 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7873 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7874 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7875 0ull, VMX_EPT_EXECUTABLE_MASK,
7876 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007877 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007878
7879 ept_set_mmio_spte_mask();
7880 kvm_enable_tdp();
7881}
7882
Tiejun Chenf2c76482014-10-28 10:14:47 +08007883static __init int hardware_setup(void)
7884{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007885 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007886 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007887
7888 rdmsrl_safe(MSR_EFER, &host_efer);
7889
7890 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7891 kvm_define_shared_msr(i, vmx_msr_index[i]);
7892
Radim Krčmář23611332016-09-29 22:41:33 +02007893 for (i = 0; i < VMX_BITMAP_NR; i++) {
7894 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7895 if (!vmx_bitmap[i])
7896 goto out;
7897 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007898
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007899 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7900 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7901
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007902 if (setup_vmcs_config(&vmcs_config) < 0) {
7903 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007904 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007905 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007906
7907 if (boot_cpu_has(X86_FEATURE_NX))
7908 kvm_enable_efer_bits(EFER_NX);
7909
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007910 if (boot_cpu_has(X86_FEATURE_MPX)) {
7911 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7912 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7913 }
7914
Wanpeng Li08d839c2017-03-23 05:30:08 -07007915 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7916 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007917 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007918
Tiejun Chenf2c76482014-10-28 10:14:47 +08007919 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007920 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007921 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007922 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007923 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007924
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007925 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007926 enable_ept_ad_bits = 0;
7927
Wanpeng Li8ad81822017-10-09 15:51:53 -07007928 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007929 enable_unrestricted_guest = 0;
7930
Paolo Bonziniad15a292015-01-30 16:18:49 +01007931 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007932 flexpriority_enabled = 0;
7933
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007934 if (!cpu_has_virtual_nmis())
7935 enable_vnmi = 0;
7936
Paolo Bonziniad15a292015-01-30 16:18:49 +01007937 /*
7938 * set_apic_access_page_addr() is used to reload apic access
7939 * page upon invalidation. No need to do anything if not
7940 * using the APIC_ACCESS_ADDR VMCS field.
7941 */
7942 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007943 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007944
7945 if (!cpu_has_vmx_tpr_shadow())
7946 kvm_x86_ops->update_cr8_intercept = NULL;
7947
7948 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7949 kvm_disable_largepages();
7950
Tianyu Lan877ad952018-07-19 08:40:23 +00007951#if IS_ENABLED(CONFIG_HYPERV)
7952 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7953 && enable_ept)
7954 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7955#endif
7956
Wanpeng Li0f107682017-09-28 18:06:24 -07007957 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007958 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007959 ple_window = 0;
7960 ple_window_grow = 0;
7961 ple_window_max = 0;
7962 ple_window_shrink = 0;
7963 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007964
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007965 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007966 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007967 kvm_x86_ops->sync_pir_to_irr = NULL;
7968 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007969
Haozhong Zhang64903d62015-10-20 15:39:09 +08007970 if (cpu_has_vmx_tsc_scaling()) {
7971 kvm_has_tsc_control = true;
7972 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7973 kvm_tsc_scaling_ratio_frac_bits = 48;
7974 }
7975
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007976 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7977
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007978 if (enable_ept)
7979 vmx_enable_tdp();
7980 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007981 kvm_disable_tdp();
7982
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007983 if (!nested) {
7984 kvm_x86_ops->get_nested_state = NULL;
7985 kvm_x86_ops->set_nested_state = NULL;
7986 }
7987
Kai Huang843e4332015-01-28 10:54:28 +08007988 /*
7989 * Only enable PML when hardware supports PML feature, and both EPT
7990 * and EPT A/D bit features are enabled -- PML depends on them to work.
7991 */
7992 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7993 enable_pml = 0;
7994
7995 if (!enable_pml) {
7996 kvm_x86_ops->slot_enable_log_dirty = NULL;
7997 kvm_x86_ops->slot_disable_log_dirty = NULL;
7998 kvm_x86_ops->flush_log_dirty = NULL;
7999 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
8000 }
8001
Sean Christophersond264ee02018-08-27 15:21:12 -07008002 if (!cpu_has_vmx_preemption_timer())
8003 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
8004
Yunhong Jiang64672c92016-06-13 14:19:59 -07008005 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
8006 u64 vmx_msr;
8007
8008 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8009 cpu_preemption_timer_multi =
8010 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8011 } else {
8012 kvm_x86_ops->set_hv_timer = NULL;
8013 kvm_x86_ops->cancel_hv_timer = NULL;
8014 }
8015
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01008016 if (!cpu_has_vmx_shadow_vmcs())
8017 enable_shadow_vmcs = 0;
8018 if (enable_shadow_vmcs)
8019 init_vmcs_shadow_fields();
8020
Feng Wubf9f6ac2015-09-18 22:29:55 +08008021 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01008022 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008023
Ashok Rajc45dcc72016-06-22 14:59:56 +08008024 kvm_mce_cap_supported |= MCG_LMCE_P;
8025
Tiejun Chenf2c76482014-10-28 10:14:47 +08008026 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008027
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008028out:
Radim Krčmář23611332016-09-29 22:41:33 +02008029 for (i = 0; i < VMX_BITMAP_NR; i++)
8030 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008031
8032 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008033}
8034
8035static __exit void hardware_unsetup(void)
8036{
Radim Krčmář23611332016-09-29 22:41:33 +02008037 int i;
8038
8039 for (i = 0; i < VMX_BITMAP_NR; i++)
8040 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008041
Tiejun Chenf2c76482014-10-28 10:14:47 +08008042 free_kvm_area();
8043}
8044
Avi Kivity6aa8b732006-12-10 02:21:36 -08008045/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008046 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8047 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8048 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008049static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008050{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008051 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008052 grow_ple_window(vcpu);
8053
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008054 /*
8055 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8056 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8057 * never set PAUSE_EXITING and just set PLE if supported,
8058 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8059 */
8060 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008061 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008062}
8063
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008064static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008065{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008066 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008067}
8068
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008069static int handle_mwait(struct kvm_vcpu *vcpu)
8070{
8071 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8072 return handle_nop(vcpu);
8073}
8074
Jim Mattson45ec3682017-08-23 16:32:04 -07008075static int handle_invalid_op(struct kvm_vcpu *vcpu)
8076{
8077 kvm_queue_exception(vcpu, UD_VECTOR);
8078 return 1;
8079}
8080
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008081static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8082{
8083 return 1;
8084}
8085
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008086static int handle_monitor(struct kvm_vcpu *vcpu)
8087{
8088 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8089 return handle_nop(vcpu);
8090}
8091
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008092/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008093 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008094 * set the success or error code of an emulated VMX instruction (as specified
8095 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
8096 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008097 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008098static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008099{
8100 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8101 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8102 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008103 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008104}
8105
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008106static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008107{
8108 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8109 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8110 X86_EFLAGS_SF | X86_EFLAGS_OF))
8111 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008112 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008113}
8114
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008115static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
8116 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008117{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008118 struct vcpu_vmx *vmx = to_vmx(vcpu);
8119
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008120 /*
8121 * failValid writes the error number to the current VMCS, which
8122 * can't be done if there isn't a current VMCS.
8123 */
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008124 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008125 return nested_vmx_failInvalid(vcpu);
8126
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008127 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8128 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8129 X86_EFLAGS_SF | X86_EFLAGS_OF))
8130 | X86_EFLAGS_ZF);
8131 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8132 /*
8133 * We don't need to force a shadow sync because
8134 * VM_INSTRUCTION_ERROR is not shadowed
8135 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008136 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008137}
Abel Gordon145c28d2013-04-18 14:36:55 +03008138
Wincy Vanff651cb2014-12-11 08:52:58 +03008139static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8140{
8141 /* TODO: not to reset guest simply here. */
8142 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008143 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008144}
8145
Jan Kiszkaf41245002014-03-07 20:03:13 +01008146static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8147{
8148 struct vcpu_vmx *vmx =
8149 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8150
8151 vmx->nested.preemption_timer_expired = true;
8152 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8153 kvm_vcpu_kick(&vmx->vcpu);
8154
8155 return HRTIMER_NORESTART;
8156}
8157
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008158/*
Bandan Das19677e32014-05-06 02:19:15 -04008159 * Decode the memory-address operand of a vmx instruction, as recorded on an
8160 * exit caused by such an instruction (run by a guest hypervisor).
8161 * On success, returns 0. When the operand is invalid, returns 1 and throws
8162 * #UD or #GP.
8163 */
8164static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8165 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008166 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008167{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008168 gva_t off;
8169 bool exn;
8170 struct kvm_segment s;
8171
Bandan Das19677e32014-05-06 02:19:15 -04008172 /*
8173 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8174 * Execution", on an exit, vmx_instruction_info holds most of the
8175 * addressing components of the operand. Only the displacement part
8176 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8177 * For how an actual address is calculated from all these components,
8178 * refer to Vol. 1, "Operand Addressing".
8179 */
8180 int scaling = vmx_instruction_info & 3;
8181 int addr_size = (vmx_instruction_info >> 7) & 7;
8182 bool is_reg = vmx_instruction_info & (1u << 10);
8183 int seg_reg = (vmx_instruction_info >> 15) & 7;
8184 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8185 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8186 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8187 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8188
8189 if (is_reg) {
8190 kvm_queue_exception(vcpu, UD_VECTOR);
8191 return 1;
8192 }
8193
8194 /* Addr = segment_base + offset */
8195 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008196 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008197 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008198 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008199 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008200 off += kvm_register_read(vcpu, index_reg)<<scaling;
8201 vmx_get_segment(vcpu, &s, seg_reg);
8202 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008203
8204 if (addr_size == 1) /* 32 bit */
8205 *ret &= 0xffffffff;
8206
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008207 /* Checks for #GP/#SS exceptions. */
8208 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008209 if (is_long_mode(vcpu)) {
8210 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8211 * non-canonical form. This is the only check on the memory
8212 * destination for long mode!
8213 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008214 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008215 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008216 /* Protected mode: apply checks for segment validity in the
8217 * following order:
8218 * - segment type check (#GP(0) may be thrown)
8219 * - usability check (#GP(0)/#SS(0))
8220 * - limit check (#GP(0)/#SS(0))
8221 */
8222 if (wr)
8223 /* #GP(0) if the destination operand is located in a
8224 * read-only data segment or any code segment.
8225 */
8226 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8227 else
8228 /* #GP(0) if the source operand is located in an
8229 * execute-only code segment
8230 */
8231 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008232 if (exn) {
8233 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8234 return 1;
8235 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008236 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8237 */
8238 exn = (s.unusable != 0);
8239 /* Protected mode: #GP(0)/#SS(0) if the memory
8240 * operand is outside the segment limit.
8241 */
8242 exn = exn || (off + sizeof(u64) > s.limit);
8243 }
8244 if (exn) {
8245 kvm_queue_exception_e(vcpu,
8246 seg_reg == VCPU_SREG_SS ?
8247 SS_VECTOR : GP_VECTOR,
8248 0);
8249 return 1;
8250 }
8251
Bandan Das19677e32014-05-06 02:19:15 -04008252 return 0;
8253}
8254
Radim Krčmářcbf71272017-05-19 15:48:51 +02008255static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008256{
8257 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008258 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008259
8260 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008261 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008262 return 1;
8263
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008264 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008265 kvm_inject_page_fault(vcpu, &e);
8266 return 1;
8267 }
8268
Bandan Das3573e222014-05-06 02:19:16 -04008269 return 0;
8270}
8271
Liran Alonabfc52c2018-06-23 02:35:13 +03008272/*
8273 * Allocate a shadow VMCS and associate it with the currently loaded
8274 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8275 * VMCS is also VMCLEARed, so that it is ready for use.
8276 */
8277static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8278{
8279 struct vcpu_vmx *vmx = to_vmx(vcpu);
8280 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8281
8282 /*
8283 * We should allocate a shadow vmcs for vmcs01 only when L1
8284 * executes VMXON and free it when L1 executes VMXOFF.
8285 * As it is invalid to execute VMXON twice, we shouldn't reach
8286 * here when vmcs01 already have an allocated shadow vmcs.
8287 */
8288 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8289
8290 if (!loaded_vmcs->shadow_vmcs) {
8291 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8292 if (loaded_vmcs->shadow_vmcs)
8293 vmcs_clear(loaded_vmcs->shadow_vmcs);
8294 }
8295 return loaded_vmcs->shadow_vmcs;
8296}
8297
Jim Mattsone29acc52016-11-30 12:03:43 -08008298static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8299{
8300 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008301 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008302
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008303 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8304 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008305 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008306
8307 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8308 if (!vmx->nested.cached_vmcs12)
8309 goto out_cached_vmcs12;
8310
Liran Alon61ada742018-06-23 02:35:08 +03008311 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8312 if (!vmx->nested.cached_shadow_vmcs12)
8313 goto out_cached_shadow_vmcs12;
8314
Liran Alonabfc52c2018-06-23 02:35:13 +03008315 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8316 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008317
Jim Mattsone29acc52016-11-30 12:03:43 -08008318 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8319 HRTIMER_MODE_REL_PINNED);
8320 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8321
Roman Kagan63aff652018-07-19 21:59:07 +03008322 vmx->nested.vpid02 = allocate_vpid();
8323
Sean Christopherson9d6105b2018-09-26 09:23:51 -07008324 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008325 vmx->nested.vmxon = true;
8326 return 0;
8327
8328out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008329 kfree(vmx->nested.cached_shadow_vmcs12);
8330
8331out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008332 kfree(vmx->nested.cached_vmcs12);
8333
8334out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008335 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008336
Jim Mattsonde3a0022017-11-27 17:22:25 -06008337out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008338 return -ENOMEM;
8339}
8340
Bandan Das3573e222014-05-06 02:19:16 -04008341/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008342 * Emulate the VMXON instruction.
8343 * Currently, we just remember that VMX is active, and do not save or even
8344 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8345 * do not currently need to store anything in that guest-allocated memory
8346 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8347 * argument is different from the VMXON pointer (which the spec says they do).
8348 */
8349static int handle_vmon(struct kvm_vcpu *vcpu)
8350{
Jim Mattsone29acc52016-11-30 12:03:43 -08008351 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008352 gpa_t vmptr;
8353 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008354 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008355 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8356 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008357
Jim Mattson70f3aac2017-04-26 08:53:46 -07008358 /*
8359 * The Intel VMX Instruction Reference lists a bunch of bits that are
8360 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8361 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8362 * Otherwise, we should fail with #UD. But most faulting conditions
8363 * have already been checked by hardware, prior to the VM-exit for
8364 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8365 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008366 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008367 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008368 kvm_queue_exception(vcpu, UD_VECTOR);
8369 return 1;
8370 }
8371
Felix Wilhelm727ba742018-06-11 09:43:44 +02008372 /* CPL=0 must be checked manually. */
8373 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008374 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008375 return 1;
8376 }
8377
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008378 if (vmx->nested.vmxon)
8379 return nested_vmx_failValid(vcpu,
8380 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008381
Haozhong Zhang3b840802016-06-22 14:59:54 +08008382 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008383 != VMXON_NEEDED_FEATURES) {
8384 kvm_inject_gp(vcpu, 0);
8385 return 1;
8386 }
8387
Radim Krčmářcbf71272017-05-19 15:48:51 +02008388 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008389 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008390
8391 /*
8392 * SDM 3: 24.11.5
8393 * The first 4 bytes of VMXON region contain the supported
8394 * VMCS revision identifier
8395 *
8396 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8397 * which replaces physical address width with 32
8398 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008399 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8400 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008401
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008402 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008403 if (is_error_page(page))
8404 return nested_vmx_failInvalid(vcpu);
8405
Radim Krčmářcbf71272017-05-19 15:48:51 +02008406 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8407 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008408 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008409 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008410 }
8411 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008412 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008413
8414 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008415 ret = enter_vmx_operation(vcpu);
8416 if (ret)
8417 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008418
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008419 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008420}
8421
8422/*
8423 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8424 * for running VMX instructions (except VMXON, whose prerequisites are
8425 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008426 * Note that many of these exceptions have priority over VM exits, so they
8427 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008428 */
8429static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8430{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008431 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008432 kvm_queue_exception(vcpu, UD_VECTOR);
8433 return 0;
8434 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008435
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008436 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008437 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008438 return 0;
8439 }
8440
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008441 return 1;
8442}
8443
David Matlack8ca44e82017-08-01 14:00:39 -07008444static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8445{
8446 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8447 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8448}
8449
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008450static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008451{
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008452 struct vcpu_vmx *vmx = to_vmx(vcpu);
8453
8454 if (!vmx->nested.hv_evmcs)
8455 return;
8456
8457 kunmap(vmx->nested.hv_evmcs_page);
8458 kvm_release_page_dirty(vmx->nested.hv_evmcs_page);
8459 vmx->nested.hv_evmcs_vmptr = -1ull;
8460 vmx->nested.hv_evmcs_page = NULL;
8461 vmx->nested.hv_evmcs = NULL;
8462}
8463
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008464static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008465{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008466 struct vcpu_vmx *vmx = to_vmx(vcpu);
8467
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008468 if (vmx->nested.current_vmptr == -1ull)
8469 return;
8470
Abel Gordon012f83c2013-04-18 14:39:25 +03008471 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008472 /* copy to memory all shadowed fields in case
8473 they were modified */
8474 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02008475 vmx->nested.need_vmcs12_sync = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008476 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008477 }
Wincy Van705699a2015-02-03 23:58:17 +08008478 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008479
8480 /* Flush VMCS12 to guest memory */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008481 kvm_vcpu_write_guest_page(vcpu,
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008482 vmx->nested.current_vmptr >> PAGE_SHIFT,
8483 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008484
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008485 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8486
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008487 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008488}
8489
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008490/*
8491 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8492 * just stops using VMX.
8493 */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008494static void free_nested(struct kvm_vcpu *vcpu)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008495{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008496 struct vcpu_vmx *vmx = to_vmx(vcpu);
8497
Wanpeng Lib7455822017-11-22 14:04:00 -08008498 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008499 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008500
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008501 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008502 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008503 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008504 vmx->nested.posted_intr_nv = -1;
8505 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008506 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008507 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008508 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8509 free_vmcs(vmx->vmcs01.shadow_vmcs);
8510 vmx->vmcs01.shadow_vmcs = NULL;
8511 }
David Matlack4f2777b2016-07-13 17:16:37 -07008512 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008513 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008514 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008515 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008516 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008517 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008518 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008519 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008520 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008521 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008522 }
Wincy Van705699a2015-02-03 23:58:17 +08008523 if (vmx->nested.pi_desc_page) {
8524 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008525 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008526 vmx->nested.pi_desc_page = NULL;
8527 vmx->nested.pi_desc = NULL;
8528 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008529
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008530 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8531
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008532 nested_release_evmcs(vcpu);
8533
Jim Mattsonde3a0022017-11-27 17:22:25 -06008534 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008535}
8536
8537/* Emulate the VMXOFF instruction */
8538static int handle_vmoff(struct kvm_vcpu *vcpu)
8539{
8540 if (!nested_vmx_check_permission(vcpu))
8541 return 1;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008542 free_nested(vcpu);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008543 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008544}
8545
Nadav Har'El27d6c862011-05-25 23:06:59 +03008546/* Emulate the VMCLEAR instruction */
8547static int handle_vmclear(struct kvm_vcpu *vcpu)
8548{
8549 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008550 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008551 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008552
8553 if (!nested_vmx_check_permission(vcpu))
8554 return 1;
8555
Radim Krčmářcbf71272017-05-19 15:48:51 +02008556 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008557 return 1;
8558
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008559 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8560 return nested_vmx_failValid(vcpu,
8561 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008562
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008563 if (vmptr == vmx->nested.vmxon_ptr)
8564 return nested_vmx_failValid(vcpu,
8565 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008566
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008567 if (vmx->nested.hv_evmcs_page) {
8568 if (vmptr == vmx->nested.hv_evmcs_vmptr)
8569 nested_release_evmcs(vcpu);
8570 } else {
8571 if (vmptr == vmx->nested.current_vmptr)
8572 nested_release_vmcs12(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008573
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008574 kvm_vcpu_write_guest(vcpu,
8575 vmptr + offsetof(struct vmcs12,
8576 launch_state),
8577 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008578 }
8579
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008580 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008581}
8582
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008583static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8584
8585/* Emulate the VMLAUNCH instruction */
8586static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8587{
8588 return nested_vmx_run(vcpu, true);
8589}
8590
8591/* Emulate the VMRESUME instruction */
8592static int handle_vmresume(struct kvm_vcpu *vcpu)
8593{
8594
8595 return nested_vmx_run(vcpu, false);
8596}
8597
Nadav Har'El49f705c2011-05-25 23:08:30 +03008598/*
8599 * Read a vmcs12 field. Since these can have varying lengths and we return
8600 * one type, we chose the biggest type (u64) and zero-extend the return value
8601 * to that size. Note that the caller, handle_vmread, might need to use only
8602 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8603 * 64-bit fields are to be returned).
8604 */
Liran Alone2536742018-06-23 02:35:02 +03008605static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008606 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008607{
8608 short offset = vmcs_field_to_offset(field);
8609 char *p;
8610
8611 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008612 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008613
Liran Alone2536742018-06-23 02:35:02 +03008614 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008615
Jim Mattsond37f4262017-12-22 12:12:16 -08008616 switch (vmcs_field_width(field)) {
8617 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008618 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008619 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008620 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008621 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008622 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008623 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008624 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008625 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008626 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008627 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008628 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008629 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008630 WARN_ON(1);
8631 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008632 }
8633}
8634
Abel Gordon20b97fe2013-04-18 14:36:25 +03008635
Liran Alone2536742018-06-23 02:35:02 +03008636static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008637 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008638 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008639 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008640 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008641 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008642
Jim Mattsond37f4262017-12-22 12:12:16 -08008643 switch (vmcs_field_width(field)) {
8644 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008645 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008646 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008647 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008648 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008649 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008650 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008651 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008652 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008653 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008654 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008655 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008656 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008657 WARN_ON(1);
8658 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008659 }
8660
8661}
8662
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02008663static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
8664{
8665 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
8666 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
8667
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02008668 vmcs12->hdr.revision_id = evmcs->revision_id;
8669
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02008670 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
8671 vmcs12->tpr_threshold = evmcs->tpr_threshold;
8672 vmcs12->guest_rip = evmcs->guest_rip;
8673
8674 if (unlikely(!(evmcs->hv_clean_fields &
8675 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
8676 vmcs12->guest_rsp = evmcs->guest_rsp;
8677 vmcs12->guest_rflags = evmcs->guest_rflags;
8678 vmcs12->guest_interruptibility_info =
8679 evmcs->guest_interruptibility_info;
8680 }
8681
8682 if (unlikely(!(evmcs->hv_clean_fields &
8683 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
8684 vmcs12->cpu_based_vm_exec_control =
8685 evmcs->cpu_based_vm_exec_control;
8686 }
8687
8688 if (unlikely(!(evmcs->hv_clean_fields &
8689 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
8690 vmcs12->exception_bitmap = evmcs->exception_bitmap;
8691 }
8692
8693 if (unlikely(!(evmcs->hv_clean_fields &
8694 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
8695 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
8696 }
8697
8698 if (unlikely(!(evmcs->hv_clean_fields &
8699 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
8700 vmcs12->vm_entry_intr_info_field =
8701 evmcs->vm_entry_intr_info_field;
8702 vmcs12->vm_entry_exception_error_code =
8703 evmcs->vm_entry_exception_error_code;
8704 vmcs12->vm_entry_instruction_len =
8705 evmcs->vm_entry_instruction_len;
8706 }
8707
8708 if (unlikely(!(evmcs->hv_clean_fields &
8709 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
8710 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
8711 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
8712 vmcs12->host_cr0 = evmcs->host_cr0;
8713 vmcs12->host_cr3 = evmcs->host_cr3;
8714 vmcs12->host_cr4 = evmcs->host_cr4;
8715 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
8716 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
8717 vmcs12->host_rip = evmcs->host_rip;
8718 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
8719 vmcs12->host_es_selector = evmcs->host_es_selector;
8720 vmcs12->host_cs_selector = evmcs->host_cs_selector;
8721 vmcs12->host_ss_selector = evmcs->host_ss_selector;
8722 vmcs12->host_ds_selector = evmcs->host_ds_selector;
8723 vmcs12->host_fs_selector = evmcs->host_fs_selector;
8724 vmcs12->host_gs_selector = evmcs->host_gs_selector;
8725 vmcs12->host_tr_selector = evmcs->host_tr_selector;
8726 }
8727
8728 if (unlikely(!(evmcs->hv_clean_fields &
8729 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
8730 vmcs12->pin_based_vm_exec_control =
8731 evmcs->pin_based_vm_exec_control;
8732 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
8733 vmcs12->secondary_vm_exec_control =
8734 evmcs->secondary_vm_exec_control;
8735 }
8736
8737 if (unlikely(!(evmcs->hv_clean_fields &
8738 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
8739 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
8740 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
8741 }
8742
8743 if (unlikely(!(evmcs->hv_clean_fields &
8744 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
8745 vmcs12->msr_bitmap = evmcs->msr_bitmap;
8746 }
8747
8748 if (unlikely(!(evmcs->hv_clean_fields &
8749 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
8750 vmcs12->guest_es_base = evmcs->guest_es_base;
8751 vmcs12->guest_cs_base = evmcs->guest_cs_base;
8752 vmcs12->guest_ss_base = evmcs->guest_ss_base;
8753 vmcs12->guest_ds_base = evmcs->guest_ds_base;
8754 vmcs12->guest_fs_base = evmcs->guest_fs_base;
8755 vmcs12->guest_gs_base = evmcs->guest_gs_base;
8756 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
8757 vmcs12->guest_tr_base = evmcs->guest_tr_base;
8758 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
8759 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
8760 vmcs12->guest_es_limit = evmcs->guest_es_limit;
8761 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
8762 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
8763 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
8764 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
8765 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
8766 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
8767 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
8768 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
8769 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
8770 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
8771 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
8772 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
8773 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
8774 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
8775 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
8776 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
8777 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
8778 vmcs12->guest_es_selector = evmcs->guest_es_selector;
8779 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
8780 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
8781 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
8782 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
8783 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
8784 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
8785 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
8786 }
8787
8788 if (unlikely(!(evmcs->hv_clean_fields &
8789 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
8790 vmcs12->tsc_offset = evmcs->tsc_offset;
8791 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
8792 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
8793 }
8794
8795 if (unlikely(!(evmcs->hv_clean_fields &
8796 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
8797 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
8798 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
8799 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
8800 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
8801 vmcs12->guest_cr0 = evmcs->guest_cr0;
8802 vmcs12->guest_cr3 = evmcs->guest_cr3;
8803 vmcs12->guest_cr4 = evmcs->guest_cr4;
8804 vmcs12->guest_dr7 = evmcs->guest_dr7;
8805 }
8806
8807 if (unlikely(!(evmcs->hv_clean_fields &
8808 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
8809 vmcs12->host_fs_base = evmcs->host_fs_base;
8810 vmcs12->host_gs_base = evmcs->host_gs_base;
8811 vmcs12->host_tr_base = evmcs->host_tr_base;
8812 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
8813 vmcs12->host_idtr_base = evmcs->host_idtr_base;
8814 vmcs12->host_rsp = evmcs->host_rsp;
8815 }
8816
8817 if (unlikely(!(evmcs->hv_clean_fields &
8818 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
8819 vmcs12->ept_pointer = evmcs->ept_pointer;
8820 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
8821 }
8822
8823 if (unlikely(!(evmcs->hv_clean_fields &
8824 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
8825 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
8826 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
8827 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
8828 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
8829 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
8830 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
8831 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
8832 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
8833 vmcs12->guest_pending_dbg_exceptions =
8834 evmcs->guest_pending_dbg_exceptions;
8835 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
8836 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
8837 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
8838 vmcs12->guest_activity_state = evmcs->guest_activity_state;
8839 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
8840 }
8841
8842 /*
8843 * Not used?
8844 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
8845 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
8846 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
8847 * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
8848 * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
8849 * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
8850 * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
8851 * vmcs12->page_fault_error_code_mask =
8852 * evmcs->page_fault_error_code_mask;
8853 * vmcs12->page_fault_error_code_match =
8854 * evmcs->page_fault_error_code_match;
8855 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
8856 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
8857 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
8858 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
8859 */
8860
8861 /*
8862 * Read only fields:
8863 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
8864 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
8865 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
8866 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
8867 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
8868 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
8869 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
8870 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
8871 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
8872 * vmcs12->exit_qualification = evmcs->exit_qualification;
8873 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
8874 *
8875 * Not present in struct vmcs12:
8876 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
8877 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
8878 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
8879 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
8880 */
8881
8882 return 0;
8883}
8884
8885static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
8886{
8887 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
8888 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
8889
8890 /*
8891 * Should not be changed by KVM:
8892 *
8893 * evmcs->host_es_selector = vmcs12->host_es_selector;
8894 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
8895 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
8896 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
8897 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
8898 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
8899 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
8900 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
8901 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
8902 * evmcs->host_cr0 = vmcs12->host_cr0;
8903 * evmcs->host_cr3 = vmcs12->host_cr3;
8904 * evmcs->host_cr4 = vmcs12->host_cr4;
8905 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
8906 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
8907 * evmcs->host_rip = vmcs12->host_rip;
8908 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
8909 * evmcs->host_fs_base = vmcs12->host_fs_base;
8910 * evmcs->host_gs_base = vmcs12->host_gs_base;
8911 * evmcs->host_tr_base = vmcs12->host_tr_base;
8912 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
8913 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
8914 * evmcs->host_rsp = vmcs12->host_rsp;
8915 * sync_vmcs12() doesn't read these:
8916 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
8917 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
8918 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
8919 * evmcs->ept_pointer = vmcs12->ept_pointer;
8920 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
8921 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
8922 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
8923 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
8924 * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
8925 * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
8926 * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
8927 * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
8928 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
8929 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
8930 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
8931 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
8932 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
8933 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
8934 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
8935 * evmcs->page_fault_error_code_mask =
8936 * vmcs12->page_fault_error_code_mask;
8937 * evmcs->page_fault_error_code_match =
8938 * vmcs12->page_fault_error_code_match;
8939 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
8940 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
8941 * evmcs->tsc_offset = vmcs12->tsc_offset;
8942 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
8943 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
8944 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
8945 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
8946 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
8947 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
8948 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
8949 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
8950 *
8951 * Not present in struct vmcs12:
8952 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
8953 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
8954 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
8955 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
8956 */
8957
8958 evmcs->guest_es_selector = vmcs12->guest_es_selector;
8959 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
8960 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
8961 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
8962 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
8963 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
8964 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
8965 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
8966
8967 evmcs->guest_es_limit = vmcs12->guest_es_limit;
8968 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
8969 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
8970 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
8971 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
8972 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
8973 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
8974 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
8975 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
8976 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
8977
8978 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
8979 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
8980 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
8981 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
8982 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
8983 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
8984 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
8985 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
8986
8987 evmcs->guest_es_base = vmcs12->guest_es_base;
8988 evmcs->guest_cs_base = vmcs12->guest_cs_base;
8989 evmcs->guest_ss_base = vmcs12->guest_ss_base;
8990 evmcs->guest_ds_base = vmcs12->guest_ds_base;
8991 evmcs->guest_fs_base = vmcs12->guest_fs_base;
8992 evmcs->guest_gs_base = vmcs12->guest_gs_base;
8993 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
8994 evmcs->guest_tr_base = vmcs12->guest_tr_base;
8995 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
8996 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
8997
8998 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
8999 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
9000
9001 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
9002 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
9003 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
9004 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
9005
9006 evmcs->guest_pending_dbg_exceptions =
9007 vmcs12->guest_pending_dbg_exceptions;
9008 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
9009 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
9010
9011 evmcs->guest_activity_state = vmcs12->guest_activity_state;
9012 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
9013
9014 evmcs->guest_cr0 = vmcs12->guest_cr0;
9015 evmcs->guest_cr3 = vmcs12->guest_cr3;
9016 evmcs->guest_cr4 = vmcs12->guest_cr4;
9017 evmcs->guest_dr7 = vmcs12->guest_dr7;
9018
9019 evmcs->guest_physical_address = vmcs12->guest_physical_address;
9020
9021 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
9022 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
9023 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
9024 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
9025 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
9026 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
9027 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
9028 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
9029
9030 evmcs->exit_qualification = vmcs12->exit_qualification;
9031
9032 evmcs->guest_linear_address = vmcs12->guest_linear_address;
9033 evmcs->guest_rsp = vmcs12->guest_rsp;
9034 evmcs->guest_rflags = vmcs12->guest_rflags;
9035
9036 evmcs->guest_interruptibility_info =
9037 vmcs12->guest_interruptibility_info;
9038 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
9039 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
9040 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
9041 evmcs->vm_entry_exception_error_code =
9042 vmcs12->vm_entry_exception_error_code;
9043 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
9044
9045 evmcs->guest_rip = vmcs12->guest_rip;
9046
9047 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
9048
9049 return 0;
9050}
9051
Jim Mattsonf4160e42018-05-29 09:11:33 -07009052/*
9053 * Copy the writable VMCS shadow fields back to the VMCS12, in case
9054 * they have been modified by the L1 guest. Note that the "read-only"
9055 * VM-exit information fields are actually writable if the vCPU is
9056 * configured to support "VMWRITE to any supported field in the VMCS."
9057 */
Abel Gordon16f5b902013-04-18 14:38:25 +03009058static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
9059{
Jim Mattsonf4160e42018-05-29 09:11:33 -07009060 const u16 *fields[] = {
9061 shadow_read_write_fields,
9062 shadow_read_only_fields
9063 };
9064 const int max_fields[] = {
9065 max_shadow_read_write_fields,
9066 max_shadow_read_only_fields
9067 };
9068 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03009069 unsigned long field;
9070 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07009071 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03009072
Jan Kiszka282da872014-10-08 18:05:39 +02009073 preempt_disable();
9074
Abel Gordon16f5b902013-04-18 14:38:25 +03009075 vmcs_load(shadow_vmcs);
9076
Jim Mattsonf4160e42018-05-29 09:11:33 -07009077 for (q = 0; q < ARRAY_SIZE(fields); q++) {
9078 for (i = 0; i < max_fields[q]; i++) {
9079 field = fields[q][i];
9080 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03009081 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07009082 }
9083 /*
9084 * Skip the VM-exit information fields if they are read-only.
9085 */
9086 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
9087 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03009088 }
9089
9090 vmcs_clear(shadow_vmcs);
9091 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02009092
9093 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03009094}
9095
Abel Gordonc3114422013-04-18 14:38:55 +03009096static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
9097{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01009098 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02009099 shadow_read_write_fields,
9100 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03009101 };
Mathias Krausec2bae892013-06-26 20:36:21 +02009102 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03009103 max_shadow_read_write_fields,
9104 max_shadow_read_only_fields
9105 };
9106 int i, q;
9107 unsigned long field;
9108 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07009109 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03009110
9111 vmcs_load(shadow_vmcs);
9112
Mathias Krausec2bae892013-06-26 20:36:21 +02009113 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03009114 for (i = 0; i < max_fields[q]; i++) {
9115 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03009116 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01009117 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03009118 }
9119 }
9120
9121 vmcs_clear(shadow_vmcs);
9122 vmcs_load(vmx->loaded_vmcs->vmcs);
9123}
9124
Nadav Har'El49f705c2011-05-25 23:08:30 +03009125static int handle_vmread(struct kvm_vcpu *vcpu)
9126{
9127 unsigned long field;
9128 u64 field_value;
9129 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9130 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9131 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03009132 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03009133
Kyle Hueyeb277562016-11-29 12:40:39 -08009134 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009135 return 1;
9136
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009137 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
9138 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08009139
Liran Alon6d894f42018-06-23 02:35:09 +03009140 if (!is_guest_mode(vcpu))
9141 vmcs12 = get_vmcs12(vcpu);
9142 else {
9143 /*
9144 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
9145 * to shadowed-field sets the ALU flags for VMfailInvalid.
9146 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009147 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
9148 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009149 vmcs12 = get_shadow_vmcs12(vcpu);
9150 }
9151
Nadav Har'El49f705c2011-05-25 23:08:30 +03009152 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03009153 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03009154 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009155 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
9156 return nested_vmx_failValid(vcpu,
9157 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
9158
Nadav Har'El49f705c2011-05-25 23:08:30 +03009159 /*
9160 * Now copy part of this value to register or memory, as requested.
9161 * Note that the number of bits actually copied is 32 or 64 depending
9162 * on the guest's mode (32 or 64 bit), not on the given field's length.
9163 */
9164 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03009165 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03009166 field_value);
9167 } else {
9168 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009169 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009170 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02009171 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009172 kvm_write_guest_virt_system(vcpu, gva, &field_value,
9173 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009174 }
9175
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009176 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009177}
9178
9179
9180static int handle_vmwrite(struct kvm_vcpu *vcpu)
9181{
9182 unsigned long field;
9183 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009184 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009185 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9186 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009187
Nadav Har'El49f705c2011-05-25 23:08:30 +03009188 /* The value to write might be 32 or 64 bits, depending on L1's long
9189 * mode, and eventually we need to write that into a field of several
9190 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08009191 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03009192 * bits into the vmcs12 field.
9193 */
9194 u64 field_value = 0;
9195 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03009196 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03009197
Kyle Hueyeb277562016-11-29 12:40:39 -08009198 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009199 return 1;
9200
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009201 if (vmx->nested.current_vmptr == -1ull)
9202 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08009203
Nadav Har'El49f705c2011-05-25 23:08:30 +03009204 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03009205 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009206 (((vmx_instruction_info) >> 3) & 0xf));
9207 else {
9208 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009209 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03009210 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009211 if (kvm_read_guest_virt(vcpu, gva, &field_value,
9212 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03009213 kvm_inject_page_fault(vcpu, &e);
9214 return 1;
9215 }
9216 }
9217
9218
Nadav Amit27e6fb52014-06-18 17:19:26 +03009219 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07009220 /*
9221 * If the vCPU supports "VMWRITE to any supported field in the
9222 * VMCS," then the "read-only" fields are actually read/write.
9223 */
9224 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009225 !nested_cpu_has_vmwrite_any_field(vcpu))
9226 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009227 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009228
Liran Alon6d894f42018-06-23 02:35:09 +03009229 if (!is_guest_mode(vcpu))
9230 vmcs12 = get_vmcs12(vcpu);
9231 else {
9232 /*
9233 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
9234 * to shadowed-field sets the ALU flags for VMfailInvalid.
9235 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009236 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
9237 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009238 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03009239 }
9240
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009241 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
9242 return nested_vmx_failValid(vcpu,
9243 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009244
Liran Alon6d894f42018-06-23 02:35:09 +03009245 /*
9246 * Do not track vmcs12 dirty-state if in guest-mode
9247 * as we actually dirty shadow vmcs12 instead of vmcs12.
9248 */
9249 if (!is_guest_mode(vcpu)) {
9250 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009251#define SHADOW_FIELD_RW(x) case x:
9252#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03009253 /*
9254 * The fields that can be updated by L1 without a vmexit are
9255 * always updated in the vmcs02, the others go down the slow
9256 * path of prepare_vmcs02.
9257 */
9258 break;
9259 default:
9260 vmx->nested.dirty_vmcs12 = true;
9261 break;
9262 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009263 }
9264
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009265 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03009266}
9267
Jim Mattsona8bc2842016-11-30 12:03:44 -08009268static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
9269{
9270 vmx->nested.current_vmptr = vmptr;
9271 if (enable_shadow_vmcs) {
9272 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9273 SECONDARY_EXEC_SHADOW_VMCS);
9274 vmcs_write64(VMCS_LINK_POINTER,
9275 __pa(vmx->vmcs01.shadow_vmcs));
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +02009276 vmx->nested.need_vmcs12_sync = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08009277 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01009278 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08009279}
9280
Nadav Har'El63846662011-05-25 23:07:29 +03009281/* Emulate the VMPTRLD instruction */
9282static int handle_vmptrld(struct kvm_vcpu *vcpu)
9283{
9284 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03009285 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03009286
9287 if (!nested_vmx_check_permission(vcpu))
9288 return 1;
9289
Radim Krčmářcbf71272017-05-19 15:48:51 +02009290 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03009291 return 1;
9292
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009293 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
9294 return nested_vmx_failValid(vcpu,
9295 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02009296
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009297 if (vmptr == vmx->nested.vmxon_ptr)
9298 return nested_vmx_failValid(vcpu,
9299 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02009300
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009301 /* Forbid normal VMPTRLD if Enlightened version was used */
9302 if (vmx->nested.hv_evmcs)
9303 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009304
Nadav Har'El63846662011-05-25 23:07:29 +03009305 if (vmx->nested.current_vmptr != vmptr) {
9306 struct vmcs12 *new_vmcs12;
9307 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02009308 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009309 if (is_error_page(page))
9310 return nested_vmx_failInvalid(vcpu);
9311
Nadav Har'El63846662011-05-25 23:07:29 +03009312 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03009313 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02009314 (new_vmcs12->hdr.shadow_vmcs &&
9315 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03009316 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009317 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009318 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03009319 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03009320 }
Nadav Har'El63846662011-05-25 23:07:29 +03009321
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02009322 nested_release_vmcs12(vcpu);
9323
David Matlack4f2777b2016-07-13 17:16:37 -07009324 /*
9325 * Load VMCS12 from guest memory since it is not already
9326 * cached.
9327 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02009328 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
9329 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02009330 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02009331
Jim Mattsona8bc2842016-11-30 12:03:44 -08009332 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03009333 }
9334
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009335 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03009336}
9337
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009338/*
9339 * This is an equivalent of the nested hypervisor executing the vmptrld
9340 * instruction.
9341 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02009342static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
9343 bool from_launch)
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009344{
9345 struct vcpu_vmx *vmx = to_vmx(vcpu);
9346 struct hv_vp_assist_page assist_page;
9347
9348 if (likely(!vmx->nested.enlightened_vmcs_enabled))
9349 return 1;
9350
9351 if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
9352 return 1;
9353
9354 if (unlikely(!assist_page.enlighten_vmentry))
9355 return 1;
9356
9357 if (unlikely(assist_page.current_nested_vmcs !=
9358 vmx->nested.hv_evmcs_vmptr)) {
9359
9360 if (!vmx->nested.hv_evmcs)
9361 vmx->nested.current_vmptr = -1ull;
9362
9363 nested_release_evmcs(vcpu);
9364
9365 vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page(
9366 vcpu, assist_page.current_nested_vmcs);
9367
9368 if (unlikely(is_error_page(vmx->nested.hv_evmcs_page)))
9369 return 0;
9370
9371 vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page);
9372
9373 if (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION) {
9374 nested_release_evmcs(vcpu);
9375 return 0;
9376 }
9377
9378 vmx->nested.dirty_vmcs12 = true;
9379 /*
9380 * As we keep L2 state for one guest only 'hv_clean_fields' mask
9381 * can't be used when we switch between them. Reset it here for
9382 * simplicity.
9383 */
9384 vmx->nested.hv_evmcs->hv_clean_fields &=
9385 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9386 vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
9387
9388 /*
9389 * Unlike normal vmcs12, enlightened vmcs12 is not fully
9390 * reloaded from guest's memory (read only fields, fields not
9391 * present in struct hv_enlightened_vmcs, ...). Make sure there
9392 * are no leftovers.
9393 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +02009394 if (from_launch)
9395 memset(vmx->nested.cached_vmcs12, 0,
9396 sizeof(*vmx->nested.cached_vmcs12));
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009397
9398 }
9399 return 1;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009400}
9401
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009402/* Emulate the VMPTRST instruction */
9403static int handle_vmptrst(struct kvm_vcpu *vcpu)
9404{
Sean Christopherson0a06d422018-07-19 10:31:00 -07009405 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
9406 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9407 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009408 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07009409 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009410
9411 if (!nested_vmx_check_permission(vcpu))
9412 return 1;
9413
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +02009414 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
9415 return 1;
9416
Sean Christopherson0a06d422018-07-19 10:31:00 -07009417 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009418 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02009419 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07009420 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
9421 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009422 kvm_inject_page_fault(vcpu, &e);
9423 return 1;
9424 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009425 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009426}
9427
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009428/* Emulate the INVEPT instruction */
9429static int handle_invept(struct kvm_vcpu *vcpu)
9430{
Wincy Vanb9c237b2015-02-03 23:56:30 +08009431 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009432 u32 vmx_instruction_info, types;
9433 unsigned long type;
9434 gva_t gva;
9435 struct x86_exception e;
9436 struct {
9437 u64 eptp, gpa;
9438 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009439
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009440 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08009441 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009442 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009443 kvm_queue_exception(vcpu, UD_VECTOR);
9444 return 1;
9445 }
9446
9447 if (!nested_vmx_check_permission(vcpu))
9448 return 1;
9449
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009450 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03009451 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009452
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009453 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009454
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009455 if (type >= 32 || !(types & (1 << type)))
9456 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009457 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009458
9459 /* According to the Intel VMX instruction reference, the memory
9460 * operand is read even if it isn't needed (e.g., for type==global)
9461 */
9462 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009463 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009464 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009465 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009466 kvm_inject_page_fault(vcpu, &e);
9467 return 1;
9468 }
9469
9470 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009471 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04009472 /*
9473 * TODO: track mappings and invalidate
9474 * single context requests appropriately
9475 */
9476 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009477 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04009478 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009479 break;
9480 default:
9481 BUG_ON(1);
9482 break;
9483 }
9484
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009485 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009486}
9487
Liran Alon3d5bdae2018-10-08 23:42:18 +03009488static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
9489{
9490 struct vcpu_vmx *vmx = to_vmx(vcpu);
9491
9492 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
Nadav Har'El0140cae2011-05-25 23:06:28 +03009493}
9494
Petr Matouseka642fc32014-09-23 20:22:30 +02009495static int handle_invvpid(struct kvm_vcpu *vcpu)
9496{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009497 struct vcpu_vmx *vmx = to_vmx(vcpu);
9498 u32 vmx_instruction_info;
9499 unsigned long type, types;
9500 gva_t gva;
9501 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009502 struct {
9503 u64 vpid;
9504 u64 gla;
9505 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009506 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009507
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009508 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009509 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009510 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009511 kvm_queue_exception(vcpu, UD_VECTOR);
9512 return 1;
9513 }
9514
9515 if (!nested_vmx_check_permission(vcpu))
9516 return 1;
9517
9518 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9519 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9520
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009521 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009522 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009523
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009524 if (type >= 32 || !(types & (1 << type)))
9525 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009526 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009527
9528 /* according to the intel vmx instruction reference, the memory
9529 * operand is read even if it isn't needed (e.g., for type==global)
9530 */
9531 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9532 vmx_instruction_info, false, &gva))
9533 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009534 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009535 kvm_inject_page_fault(vcpu, &e);
9536 return 1;
9537 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009538 if (operand.vpid >> 16)
9539 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009540 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009541
Liran Alon3d5bdae2018-10-08 23:42:18 +03009542 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009543 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009544 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009545 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009546 is_noncanonical_address(operand.gla, vcpu))
9547 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009548 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03009549 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009550 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009551 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009552 } else
Liran Alon327c0722018-10-08 23:42:19 +03009553 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009554 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009555 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009556 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009557 if (!operand.vpid)
9558 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009559 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03009560 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009561 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009562 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009563 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009564 break;
9565 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009566 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009567 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009568 }
9569
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009570 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009571}
9572
Junaid Shahideb4b2482018-06-27 14:59:14 -07009573static int handle_invpcid(struct kvm_vcpu *vcpu)
9574{
9575 u32 vmx_instruction_info;
9576 unsigned long type;
9577 bool pcid_enabled;
9578 gva_t gva;
9579 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009580 unsigned i;
9581 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009582 struct {
9583 u64 pcid;
9584 u64 gla;
9585 } operand;
9586
9587 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9588 kvm_queue_exception(vcpu, UD_VECTOR);
9589 return 1;
9590 }
9591
9592 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9593 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9594
9595 if (type > 3) {
9596 kvm_inject_gp(vcpu, 0);
9597 return 1;
9598 }
9599
9600 /* According to the Intel instruction reference, the memory operand
9601 * is read even if it isn't needed (e.g., for type==all)
9602 */
9603 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9604 vmx_instruction_info, false, &gva))
9605 return 1;
9606
9607 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9608 kvm_inject_page_fault(vcpu, &e);
9609 return 1;
9610 }
9611
9612 if (operand.pcid >> 12 != 0) {
9613 kvm_inject_gp(vcpu, 0);
9614 return 1;
9615 }
9616
9617 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9618
9619 switch (type) {
9620 case INVPCID_TYPE_INDIV_ADDR:
9621 if ((!pcid_enabled && (operand.pcid != 0)) ||
9622 is_noncanonical_address(operand.gla, vcpu)) {
9623 kvm_inject_gp(vcpu, 0);
9624 return 1;
9625 }
9626 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9627 return kvm_skip_emulated_instruction(vcpu);
9628
9629 case INVPCID_TYPE_SINGLE_CTXT:
9630 if (!pcid_enabled && (operand.pcid != 0)) {
9631 kvm_inject_gp(vcpu, 0);
9632 return 1;
9633 }
9634
9635 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9636 kvm_mmu_sync_roots(vcpu);
9637 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9638 }
9639
Junaid Shahidb94742c2018-06-27 14:59:20 -07009640 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009641 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07009642 == operand.pcid)
9643 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009644
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02009645 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009646 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009647 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009648 * given PCID, then nothing needs to be done here because a
9649 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009650 */
9651
9652 return kvm_skip_emulated_instruction(vcpu);
9653
9654 case INVPCID_TYPE_ALL_NON_GLOBAL:
9655 /*
9656 * Currently, KVM doesn't mark global entries in the shadow
9657 * page tables, so a non-global flush just degenerates to a
9658 * global flush. If needed, we could optimize this later by
9659 * keeping track of global entries in shadow page tables.
9660 */
9661
9662 /* fall-through */
9663 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9664 kvm_mmu_unload(vcpu);
9665 return kvm_skip_emulated_instruction(vcpu);
9666
9667 default:
9668 BUG(); /* We have already checked above that type <= 3 */
9669 }
9670}
9671
Kai Huang843e4332015-01-28 10:54:28 +08009672static int handle_pml_full(struct kvm_vcpu *vcpu)
9673{
9674 unsigned long exit_qualification;
9675
9676 trace_kvm_pml_full(vcpu->vcpu_id);
9677
9678 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9679
9680 /*
9681 * PML buffer FULL happened while executing iret from NMI,
9682 * "blocked by NMI" bit has to be set before next VM entry.
9683 */
9684 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009685 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009686 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9687 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9688 GUEST_INTR_STATE_NMI);
9689
9690 /*
9691 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9692 * here.., and there's no userspace involvement needed for PML.
9693 */
9694 return 1;
9695}
9696
Yunhong Jiang64672c92016-06-13 14:19:59 -07009697static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9698{
Sean Christophersond264ee02018-08-27 15:21:12 -07009699 if (!to_vmx(vcpu)->req_immediate_exit)
9700 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009701 return 1;
9702}
9703
Bandan Das41ab9372017-08-03 15:54:43 -04009704static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9705{
9706 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009707 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9708
9709 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009710 switch (address & VMX_EPTP_MT_MASK) {
9711 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009712 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009713 return false;
9714 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009715 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009716 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009717 return false;
9718 break;
9719 default:
9720 return false;
9721 }
9722
David Hildenbrandbb97a012017-08-10 23:15:28 +02009723 /* only 4 levels page-walk length are valid */
9724 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009725 return false;
9726
9727 /* Reserved bits should not be set */
9728 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9729 return false;
9730
9731 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009732 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009733 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009734 return false;
9735 }
9736
9737 return true;
9738}
9739
9740static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9741 struct vmcs12 *vmcs12)
9742{
9743 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9744 u64 address;
9745 bool accessed_dirty;
9746 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9747
9748 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9749 !nested_cpu_has_ept(vmcs12))
9750 return 1;
9751
9752 if (index >= VMFUNC_EPTP_ENTRIES)
9753 return 1;
9754
9755
9756 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9757 &address, index * 8, 8))
9758 return 1;
9759
David Hildenbrandbb97a012017-08-10 23:15:28 +02009760 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009761
9762 /*
9763 * If the (L2) guest does a vmfunc to the currently
9764 * active ept pointer, we don't have to do anything else
9765 */
9766 if (vmcs12->ept_pointer != address) {
9767 if (!valid_ept_address(vcpu, address))
9768 return 1;
9769
9770 kvm_mmu_unload(vcpu);
9771 mmu->ept_ad = accessed_dirty;
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02009772 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Bandan Das41ab9372017-08-03 15:54:43 -04009773 vmcs12->ept_pointer = address;
9774 /*
9775 * TODO: Check what's the correct approach in case
9776 * mmu reload fails. Currently, we just let the next
9777 * reload potentially fail
9778 */
9779 kvm_mmu_reload(vcpu);
9780 }
9781
9782 return 0;
9783}
9784
Bandan Das2a499e42017-08-03 15:54:41 -04009785static int handle_vmfunc(struct kvm_vcpu *vcpu)
9786{
Bandan Das27c42a12017-08-03 15:54:42 -04009787 struct vcpu_vmx *vmx = to_vmx(vcpu);
9788 struct vmcs12 *vmcs12;
9789 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9790
9791 /*
9792 * VMFUNC is only supported for nested guests, but we always enable the
9793 * secondary control for simplicity; for non-nested mode, fake that we
9794 * didn't by injecting #UD.
9795 */
9796 if (!is_guest_mode(vcpu)) {
9797 kvm_queue_exception(vcpu, UD_VECTOR);
9798 return 1;
9799 }
9800
9801 vmcs12 = get_vmcs12(vcpu);
9802 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9803 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009804
9805 switch (function) {
9806 case 0:
9807 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9808 goto fail;
9809 break;
9810 default:
9811 goto fail;
9812 }
9813 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009814
9815fail:
9816 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9817 vmcs_read32(VM_EXIT_INTR_INFO),
9818 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009819 return 1;
9820}
9821
Sean Christopherson0b665d32018-08-14 09:33:34 -07009822static int handle_encls(struct kvm_vcpu *vcpu)
9823{
9824 /*
9825 * SGX virtualization is not yet supported. There is no software
9826 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9827 * to prevent the guest from executing ENCLS.
9828 */
9829 kvm_queue_exception(vcpu, UD_VECTOR);
9830 return 1;
9831}
9832
Nadav Har'El0140cae2011-05-25 23:06:28 +03009833/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009834 * The exit handlers return 1 if the exit was handled fully and guest execution
9835 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9836 * to be done to userspace and return 0.
9837 */
Mathias Krause772e0312012-08-30 01:30:19 +02009838static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009839 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9840 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009841 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009842 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009843 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009844 [EXIT_REASON_CR_ACCESS] = handle_cr,
9845 [EXIT_REASON_DR_ACCESS] = handle_dr,
9846 [EXIT_REASON_CPUID] = handle_cpuid,
9847 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9848 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9849 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9850 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009851 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009852 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009853 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009854 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009855 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009856 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009857 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009858 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009859 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009860 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009861 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009862 [EXIT_REASON_VMOFF] = handle_vmoff,
9863 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009864 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9865 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009866 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009867 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009868 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009869 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009870 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009871 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009872 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9873 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009874 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9875 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009876 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009877 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009878 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009879 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009880 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009881 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009882 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009883 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009884 [EXIT_REASON_XSAVES] = handle_xsaves,
9885 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009886 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009887 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009888 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009889 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009890 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009891};
9892
9893static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009894 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009895
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009896static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9897 struct vmcs12 *vmcs12)
9898{
9899 unsigned long exit_qualification;
9900 gpa_t bitmap, last_bitmap;
9901 unsigned int port;
9902 int size;
9903 u8 b;
9904
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009905 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009906 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009907
9908 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9909
9910 port = exit_qualification >> 16;
9911 size = (exit_qualification & 7) + 1;
9912
9913 last_bitmap = (gpa_t)-1;
9914 b = -1;
9915
9916 while (size > 0) {
9917 if (port < 0x8000)
9918 bitmap = vmcs12->io_bitmap_a;
9919 else if (port < 0x10000)
9920 bitmap = vmcs12->io_bitmap_b;
9921 else
Joe Perches1d804d02015-03-30 16:46:09 -07009922 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009923 bitmap += (port & 0x7fff) / 8;
9924
9925 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009926 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009927 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009928 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009929 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009930
9931 port++;
9932 size--;
9933 last_bitmap = bitmap;
9934 }
9935
Joe Perches1d804d02015-03-30 16:46:09 -07009936 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009937}
9938
Nadav Har'El644d7112011-05-25 23:12:35 +03009939/*
9940 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9941 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9942 * disinterest in the current event (read or write a specific MSR) by using an
9943 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9944 */
9945static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9946 struct vmcs12 *vmcs12, u32 exit_reason)
9947{
9948 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9949 gpa_t bitmap;
9950
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009951 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009952 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009953
9954 /*
9955 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9956 * for the four combinations of read/write and low/high MSR numbers.
9957 * First we need to figure out which of the four to use:
9958 */
9959 bitmap = vmcs12->msr_bitmap;
9960 if (exit_reason == EXIT_REASON_MSR_WRITE)
9961 bitmap += 2048;
9962 if (msr_index >= 0xc0000000) {
9963 msr_index -= 0xc0000000;
9964 bitmap += 1024;
9965 }
9966
9967 /* Then read the msr_index'th bit from this bitmap: */
9968 if (msr_index < 1024*8) {
9969 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009970 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009971 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009972 return 1 & (b >> (msr_index & 7));
9973 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009974 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009975}
9976
9977/*
9978 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9979 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9980 * intercept (via guest_host_mask etc.) the current event.
9981 */
9982static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9983 struct vmcs12 *vmcs12)
9984{
9985 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9986 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009987 int reg;
9988 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009989
9990 switch ((exit_qualification >> 4) & 3) {
9991 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009992 reg = (exit_qualification >> 8) & 15;
9993 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009994 switch (cr) {
9995 case 0:
9996 if (vmcs12->cr0_guest_host_mask &
9997 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009998 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009999 break;
10000 case 3:
10001 if ((vmcs12->cr3_target_count >= 1 &&
10002 vmcs12->cr3_target_value0 == val) ||
10003 (vmcs12->cr3_target_count >= 2 &&
10004 vmcs12->cr3_target_value1 == val) ||
10005 (vmcs12->cr3_target_count >= 3 &&
10006 vmcs12->cr3_target_value2 == val) ||
10007 (vmcs12->cr3_target_count >= 4 &&
10008 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -070010009 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010010 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -070010011 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010012 break;
10013 case 4:
10014 if (vmcs12->cr4_guest_host_mask &
10015 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -070010016 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010017 break;
10018 case 8:
10019 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -070010020 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010021 break;
10022 }
10023 break;
10024 case 2: /* clts */
10025 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
10026 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -070010027 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010028 break;
10029 case 1: /* mov from cr */
10030 switch (cr) {
10031 case 3:
10032 if (vmcs12->cpu_based_vm_exec_control &
10033 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -070010034 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010035 break;
10036 case 8:
10037 if (vmcs12->cpu_based_vm_exec_control &
10038 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -070010039 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010040 break;
10041 }
10042 break;
10043 case 3: /* lmsw */
10044 /*
10045 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
10046 * cr0. Other attempted changes are ignored, with no exit.
10047 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +020010048 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +030010049 if (vmcs12->cr0_guest_host_mask & 0xe &
10050 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -070010051 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010052 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
10053 !(vmcs12->cr0_read_shadow & 0x1) &&
10054 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -070010055 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010056 break;
10057 }
Joe Perches1d804d02015-03-30 16:46:09 -070010058 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010059}
10060
Liran Alona7cde482018-06-23 02:35:10 +030010061static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
10062 struct vmcs12 *vmcs12, gpa_t bitmap)
10063{
10064 u32 vmx_instruction_info;
10065 unsigned long field;
10066 u8 b;
10067
10068 if (!nested_cpu_has_shadow_vmcs(vmcs12))
10069 return true;
10070
10071 /* Decode instruction info and find the field to access */
10072 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10073 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
10074
10075 /* Out-of-range fields always cause a VM exit from L2 to L1 */
10076 if (field >> 15)
10077 return true;
10078
10079 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
10080 return true;
10081
10082 return 1 & (b >> (field & 7));
10083}
10084
Nadav Har'El644d7112011-05-25 23:12:35 +030010085/*
10086 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
10087 * should handle it ourselves in L0 (and then continue L2). Only call this
10088 * when in is_guest_mode (L2).
10089 */
Paolo Bonzini7313c692017-07-27 10:31:25 +020010090static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +030010091{
Nadav Har'El644d7112011-05-25 23:12:35 +030010092 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10093 struct vcpu_vmx *vmx = to_vmx(vcpu);
10094 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10095
Jim Mattson4f350c62017-09-14 16:31:44 -070010096 if (vmx->nested.nested_run_pending)
10097 return false;
10098
10099 if (unlikely(vmx->fail)) {
10100 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
10101 vmcs_read32(VM_INSTRUCTION_ERROR));
10102 return true;
10103 }
Jan Kiszka542060e2014-01-04 18:47:21 +010010104
David Matlackc9f04402017-08-01 14:00:40 -070010105 /*
10106 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -060010107 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
10108 * Page). The CPU may write to these pages via their host
10109 * physical address while L2 is running, bypassing any
10110 * address-translation-based dirty tracking (e.g. EPT write
10111 * protection).
David Matlackc9f04402017-08-01 14:00:40 -070010112 *
10113 * Mark them dirty on every exit from L2 to prevent them from
10114 * getting out of sync with dirty tracking.
10115 */
10116 nested_mark_vmcs12_pages_dirty(vcpu);
10117
Jim Mattson4f350c62017-09-14 16:31:44 -070010118 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
10119 vmcs_readl(EXIT_QUALIFICATION),
10120 vmx->idt_vectoring_info,
10121 intr_info,
10122 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10123 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +030010124
10125 switch (exit_reason) {
10126 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -080010127 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -070010128 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010129 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -070010130 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +010010131 else if (is_debug(intr_info) &&
10132 vcpu->guest_debug &
10133 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
10134 return false;
10135 else if (is_breakpoint(intr_info) &&
10136 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
10137 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010138 return vmcs12->exception_bitmap &
10139 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
10140 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -070010141 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010142 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -070010143 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010144 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +020010145 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010146 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +020010147 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010148 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -070010149 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010150 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -070010151 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010152 case EXIT_REASON_HLT:
10153 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
10154 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -070010155 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010156 case EXIT_REASON_INVLPG:
10157 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
10158 case EXIT_REASON_RDPMC:
10159 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +020010160 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +020010161 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +020010162 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +020010163 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010164 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +030010165 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +030010166 case EXIT_REASON_VMREAD:
10167 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
10168 vmcs12->vmread_bitmap);
10169 case EXIT_REASON_VMWRITE:
10170 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
10171 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +030010172 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
10173 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +030010174 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +030010175 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +020010176 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +030010177 /*
10178 * VMX instructions trap unconditionally. This allows L1 to
10179 * emulate them for its L2 guest, i.e., allows 3-level nesting!
10180 */
Joe Perches1d804d02015-03-30 16:46:09 -070010181 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010182 case EXIT_REASON_CR_ACCESS:
10183 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
10184 case EXIT_REASON_DR_ACCESS:
10185 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
10186 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +010010187 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +020010188 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
10189 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +030010190 case EXIT_REASON_MSR_READ:
10191 case EXIT_REASON_MSR_WRITE:
10192 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
10193 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -070010194 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010195 case EXIT_REASON_MWAIT_INSTRUCTION:
10196 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +030010197 case EXIT_REASON_MONITOR_TRAP_FLAG:
10198 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +030010199 case EXIT_REASON_MONITOR_INSTRUCTION:
10200 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
10201 case EXIT_REASON_PAUSE_INSTRUCTION:
10202 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
10203 nested_cpu_has2(vmcs12,
10204 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
10205 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -070010206 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010207 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010208 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +030010209 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +080010210 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +080010211 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -040010212 /*
10213 * The controls for "virtualize APIC accesses," "APIC-
10214 * register virtualization," and "virtual-interrupt
10215 * delivery" only come from vmcs12.
10216 */
Joe Perches1d804d02015-03-30 16:46:09 -070010217 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010218 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +030010219 /*
10220 * L0 always deals with the EPT violation. If nested EPT is
10221 * used, and the nested mmu code discovers that the address is
10222 * missing in the guest EPT table (EPT12), the EPT violation
10223 * will be injected with nested_ept_inject_page_fault()
10224 */
Joe Perches1d804d02015-03-30 16:46:09 -070010225 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010226 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +030010227 /*
10228 * L2 never uses directly L1's EPT, but rather L0's own EPT
10229 * table (shadow on EPT) or a merged EPT table that L0 built
10230 * (EPT on EPT). So any problems with the structure of the
10231 * table is L0's fault.
10232 */
Joe Perches1d804d02015-03-30 16:46:09 -070010233 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +020010234 case EXIT_REASON_INVPCID:
10235 return
10236 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
10237 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +030010238 case EXIT_REASON_WBINVD:
10239 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
10240 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -070010241 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010242 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
10243 /*
10244 * This should never happen, since it is not possible to
10245 * set XSS to a non-zero value---neither in L1 nor in L2.
10246 * If if it were, XSS would have to be checked against
10247 * the XSS exit bitmap in vmcs12.
10248 */
10249 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +080010250 case EXIT_REASON_PREEMPTION_TIMER:
10251 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +020010252 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -040010253 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +020010254 return false;
Bandan Das2a499e42017-08-03 15:54:41 -040010255 case EXIT_REASON_VMFUNC:
10256 /* VM functions are emulated through L2->L0 vmexits. */
10257 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -070010258 case EXIT_REASON_ENCLS:
10259 /* SGX is never exposed to L1 */
10260 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +030010261 default:
Joe Perches1d804d02015-03-30 16:46:09 -070010262 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +030010263 }
10264}
10265
Paolo Bonzini7313c692017-07-27 10:31:25 +020010266static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
10267{
10268 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10269
10270 /*
10271 * At this point, the exit interruption info in exit_intr_info
10272 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
10273 * we need to query the in-kernel LAPIC.
10274 */
10275 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
10276 if ((exit_intr_info &
10277 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10278 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
10279 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10280 vmcs12->vm_exit_intr_error_code =
10281 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
10282 }
10283
10284 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
10285 vmcs_readl(EXIT_QUALIFICATION));
10286 return 1;
10287}
10288
Avi Kivity586f9602010-11-18 13:09:54 +020010289static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
10290{
10291 *info1 = vmcs_readl(EXIT_QUALIFICATION);
10292 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
10293}
10294
Kai Huanga3eaa862015-11-04 13:46:05 +080010295static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +080010296{
Kai Huanga3eaa862015-11-04 13:46:05 +080010297 if (vmx->pml_pg) {
10298 __free_page(vmx->pml_pg);
10299 vmx->pml_pg = NULL;
10300 }
Kai Huang843e4332015-01-28 10:54:28 +080010301}
10302
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010303static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +080010304{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010305 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010306 u64 *pml_buf;
10307 u16 pml_idx;
10308
10309 pml_idx = vmcs_read16(GUEST_PML_INDEX);
10310
10311 /* Do nothing if PML buffer is empty */
10312 if (pml_idx == (PML_ENTITY_NUM - 1))
10313 return;
10314
10315 /* PML index always points to next available PML buffer entity */
10316 if (pml_idx >= PML_ENTITY_NUM)
10317 pml_idx = 0;
10318 else
10319 pml_idx++;
10320
10321 pml_buf = page_address(vmx->pml_pg);
10322 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
10323 u64 gpa;
10324
10325 gpa = pml_buf[pml_idx];
10326 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010327 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +080010328 }
10329
10330 /* reset PML index */
10331 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10332}
10333
10334/*
10335 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
10336 * Called before reporting dirty_bitmap to userspace.
10337 */
10338static void kvm_flush_pml_buffers(struct kvm *kvm)
10339{
10340 int i;
10341 struct kvm_vcpu *vcpu;
10342 /*
10343 * We only need to kick vcpu out of guest mode here, as PML buffer
10344 * is flushed at beginning of all VMEXITs, and it's obvious that only
10345 * vcpus running in guest are possible to have unflushed GPAs in PML
10346 * buffer.
10347 */
10348 kvm_for_each_vcpu(i, vcpu, kvm)
10349 kvm_vcpu_kick(vcpu);
10350}
10351
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010352static void vmx_dump_sel(char *name, uint32_t sel)
10353{
10354 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -050010355 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010356 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
10357 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
10358 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
10359}
10360
10361static void vmx_dump_dtsel(char *name, uint32_t limit)
10362{
10363 pr_err("%s limit=0x%08x, base=0x%016lx\n",
10364 name, vmcs_read32(limit),
10365 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
10366}
10367
10368static void dump_vmcs(void)
10369{
10370 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
10371 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
10372 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
10373 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
10374 u32 secondary_exec_control = 0;
10375 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +010010376 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010377 int i, n;
10378
10379 if (cpu_has_secondary_exec_ctrls())
10380 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10381
10382 pr_err("*** Guest State ***\n");
10383 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10384 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
10385 vmcs_readl(CR0_GUEST_HOST_MASK));
10386 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
10387 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
10388 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
10389 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
10390 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
10391 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010392 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
10393 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
10394 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
10395 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010396 }
10397 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
10398 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
10399 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
10400 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
10401 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10402 vmcs_readl(GUEST_SYSENTER_ESP),
10403 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
10404 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
10405 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
10406 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
10407 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
10408 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
10409 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
10410 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
10411 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
10412 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
10413 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
10414 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
10415 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010416 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10417 efer, vmcs_read64(GUEST_IA32_PAT));
10418 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
10419 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010420 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010421 if (cpu_has_load_perf_global_ctrl &&
10422 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010423 pr_err("PerfGlobCtl = 0x%016llx\n",
10424 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010425 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010426 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010427 pr_err("Interruptibility = %08x ActivityState = %08x\n",
10428 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
10429 vmcs_read32(GUEST_ACTIVITY_STATE));
10430 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
10431 pr_err("InterruptStatus = %04x\n",
10432 vmcs_read16(GUEST_INTR_STATUS));
10433
10434 pr_err("*** Host State ***\n");
10435 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
10436 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
10437 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
10438 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
10439 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
10440 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
10441 vmcs_read16(HOST_TR_SELECTOR));
10442 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
10443 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
10444 vmcs_readl(HOST_TR_BASE));
10445 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
10446 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
10447 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
10448 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
10449 vmcs_readl(HOST_CR4));
10450 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
10451 vmcs_readl(HOST_IA32_SYSENTER_ESP),
10452 vmcs_read32(HOST_IA32_SYSENTER_CS),
10453 vmcs_readl(HOST_IA32_SYSENTER_EIP));
10454 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010455 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10456 vmcs_read64(HOST_IA32_EFER),
10457 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010458 if (cpu_has_load_perf_global_ctrl &&
10459 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010460 pr_err("PerfGlobCtl = 0x%016llx\n",
10461 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010462
10463 pr_err("*** Control State ***\n");
10464 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
10465 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
10466 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
10467 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
10468 vmcs_read32(EXCEPTION_BITMAP),
10469 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10470 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10471 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10472 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10473 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10474 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10475 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10476 vmcs_read32(VM_EXIT_INTR_INFO),
10477 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10478 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10479 pr_err(" reason=%08x qualification=%016lx\n",
10480 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10481 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10482 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10483 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010484 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010485 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010486 pr_err("TSC Multiplier = 0x%016llx\n",
10487 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010488 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10489 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10490 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10491 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10492 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010493 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010494 n = vmcs_read32(CR3_TARGET_COUNT);
10495 for (i = 0; i + 1 < n; i += 4)
10496 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10497 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10498 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10499 if (i < n)
10500 pr_err("CR3 target%u=%016lx\n",
10501 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10502 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10503 pr_err("PLE Gap=%08x Window=%08x\n",
10504 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10505 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10506 pr_err("Virtual processor ID = 0x%04x\n",
10507 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10508}
10509
Avi Kivity6aa8b732006-12-10 02:21:36 -080010510/*
10511 * The guest has exited. See if we can fix it or if we need userspace
10512 * assistance.
10513 */
Avi Kivity851ba692009-08-24 11:10:17 +030010514static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010515{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010516 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010517 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010518 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010519
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010520 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10521
Kai Huang843e4332015-01-28 10:54:28 +080010522 /*
10523 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10524 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10525 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10526 * mode as if vcpus is in root mode, the PML buffer must has been
10527 * flushed already.
10528 */
10529 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010530 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010531
Mohammed Gamal80ced182009-09-01 12:48:18 +020010532 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010533 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010534 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010535
Paolo Bonzini7313c692017-07-27 10:31:25 +020010536 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10537 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010538
Mohammed Gamal51207022010-05-31 22:40:54 +030010539 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010540 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010541 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10542 vcpu->run->fail_entry.hardware_entry_failure_reason
10543 = exit_reason;
10544 return 0;
10545 }
10546
Avi Kivity29bd8a72007-09-10 17:27:03 +030010547 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010548 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10549 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010550 = vmcs_read32(VM_INSTRUCTION_ERROR);
10551 return 0;
10552 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010553
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010554 /*
10555 * Note:
10556 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10557 * delivery event since it indicates guest is accessing MMIO.
10558 * The vm-exit can be triggered again after return to guest that
10559 * will cause infinite loop.
10560 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010561 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010562 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010563 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010564 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010565 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10566 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10567 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010568 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010569 vcpu->run->internal.data[0] = vectoring_info;
10570 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010571 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10572 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10573 vcpu->run->internal.ndata++;
10574 vcpu->run->internal.data[3] =
10575 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10576 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010577 return 0;
10578 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010579
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010580 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010581 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10582 if (vmx_interrupt_allowed(vcpu)) {
10583 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10584 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10585 vcpu->arch.nmi_pending) {
10586 /*
10587 * This CPU don't support us in finding the end of an
10588 * NMI-blocked window if the guest runs with IRQs
10589 * disabled. So we pull the trigger after 1 s of
10590 * futile waiting, but inform the user about this.
10591 */
10592 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10593 "state on VCPU %d after 1 s timeout\n",
10594 __func__, vcpu->vcpu_id);
10595 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10596 }
10597 }
10598
Avi Kivity6aa8b732006-12-10 02:21:36 -080010599 if (exit_reason < kvm_vmx_max_exit_handlers
10600 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010601 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010602 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010603 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10604 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010605 kvm_queue_exception(vcpu, UD_VECTOR);
10606 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010607 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010608}
10609
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010610/*
10611 * Software based L1D cache flush which is used when microcode providing
10612 * the cache control MSR is not loaded.
10613 *
10614 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10615 * flush it is required to read in 64 KiB because the replacement algorithm
10616 * is not exactly LRU. This could be sized at runtime via topology
10617 * information but as all relevant affected CPUs have 32KiB L1D cache size
10618 * there is no point in doing so.
10619 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010620static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010621{
10622 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010623
10624 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010625 * This code is only executed when the the flush mode is 'cond' or
10626 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010627 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010628 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010629 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010630
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010631 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010632 * Clear the per-vcpu flush bit, it gets set again
10633 * either from vcpu_run() or from one of the unsafe
10634 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010635 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010636 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010637 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010638
10639 /*
10640 * Clear the per-cpu flush bit, it gets set again from
10641 * the interrupt handlers.
10642 */
10643 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10644 kvm_clear_cpu_l1tf_flush_l1d();
10645
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010646 if (!flush_l1d)
10647 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010648 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010649
10650 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010651
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010652 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10653 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10654 return;
10655 }
10656
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010657 asm volatile(
10658 /* First ensure the pages are in the TLB */
10659 "xorl %%eax, %%eax\n"
10660 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010661 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010662 "addl $4096, %%eax\n\t"
10663 "cmpl %%eax, %[size]\n\t"
10664 "jne .Lpopulate_tlb\n\t"
10665 "xorl %%eax, %%eax\n\t"
10666 "cpuid\n\t"
10667 /* Now fill the cache */
10668 "xorl %%eax, %%eax\n"
10669 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010670 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010671 "addl $64, %%eax\n\t"
10672 "cmpl %%eax, %[size]\n\t"
10673 "jne .Lfill_cache\n\t"
10674 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010675 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010676 [size] "r" (size)
10677 : "eax", "ebx", "ecx", "edx");
10678}
10679
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010680static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010681{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010682 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10683
10684 if (is_guest_mode(vcpu) &&
10685 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10686 return;
10687
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010688 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010689 vmcs_write32(TPR_THRESHOLD, 0);
10690 return;
10691 }
10692
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010693 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010694}
10695
Jim Mattson8d860bb2018-05-09 16:56:05 -040010696static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010697{
10698 u32 sec_exec_control;
10699
Jim Mattson8d860bb2018-05-09 16:56:05 -040010700 if (!lapic_in_kernel(vcpu))
10701 return;
10702
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010703 if (!flexpriority_enabled &&
10704 !cpu_has_vmx_virtualize_x2apic_mode())
10705 return;
10706
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010707 /* Postpone execution until vmcs01 is the current VMCS. */
10708 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010709 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010710 return;
10711 }
10712
Yang Zhang8d146952013-01-25 10:18:50 +080010713 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010714 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10715 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010716
Jim Mattson8d860bb2018-05-09 16:56:05 -040010717 switch (kvm_get_apic_mode(vcpu)) {
10718 case LAPIC_MODE_INVALID:
10719 WARN_ONCE(true, "Invalid local APIC state");
10720 case LAPIC_MODE_DISABLED:
10721 break;
10722 case LAPIC_MODE_XAPIC:
10723 if (flexpriority_enabled) {
10724 sec_exec_control |=
10725 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10726 vmx_flush_tlb(vcpu, true);
10727 }
10728 break;
10729 case LAPIC_MODE_X2APIC:
10730 if (cpu_has_vmx_virtualize_x2apic_mode())
10731 sec_exec_control |=
10732 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10733 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010734 }
10735 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10736
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010737 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010738}
10739
Tang Chen38b99172014-09-24 15:57:54 +080010740static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10741{
Jim Mattsonab5df312018-05-09 17:02:03 -040010742 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010743 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010744 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010745 }
Tang Chen38b99172014-09-24 15:57:54 +080010746}
10747
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010748static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010749{
10750 u16 status;
10751 u8 old;
10752
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010753 if (max_isr == -1)
10754 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010755
10756 status = vmcs_read16(GUEST_INTR_STATUS);
10757 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010758 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010759 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010760 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010761 vmcs_write16(GUEST_INTR_STATUS, status);
10762 }
10763}
10764
10765static void vmx_set_rvi(int vector)
10766{
10767 u16 status;
10768 u8 old;
10769
Wei Wang4114c272014-11-05 10:53:43 +080010770 if (vector == -1)
10771 vector = 0;
10772
Yang Zhangc7c9c562013-01-25 10:18:51 +080010773 status = vmcs_read16(GUEST_INTR_STATUS);
10774 old = (u8)status & 0xff;
10775 if ((u8)vector != old) {
10776 status &= ~0xff;
10777 status |= (u8)vector;
10778 vmcs_write16(GUEST_INTR_STATUS, status);
10779 }
10780}
10781
10782static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10783{
Liran Alon851c1a182017-12-24 18:12:56 +020010784 /*
10785 * When running L2, updating RVI is only relevant when
10786 * vmcs12 virtual-interrupt-delivery enabled.
10787 * However, it can be enabled only when L1 also
10788 * intercepts external-interrupts and in that case
10789 * we should not update vmcs02 RVI but instead intercept
10790 * interrupt. Therefore, do nothing when running L2.
10791 */
10792 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010793 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010794}
10795
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010796static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010797{
10798 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010799 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010800 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010801
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010802 WARN_ON(!vcpu->arch.apicv_active);
10803 if (pi_test_on(&vmx->pi_desc)) {
10804 pi_clear_on(&vmx->pi_desc);
10805 /*
10806 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10807 * But on x86 this is just a compiler barrier anyway.
10808 */
10809 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010810 max_irr_updated =
10811 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10812
10813 /*
10814 * If we are running L2 and L1 has a new pending interrupt
10815 * which can be injected, we should re-evaluate
10816 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010817 * If L1 intercepts external-interrupts, we should
10818 * exit from L2 to L1. Otherwise, interrupt should be
10819 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010820 */
Liran Alon851c1a182017-12-24 18:12:56 +020010821 if (is_guest_mode(vcpu) && max_irr_updated) {
10822 if (nested_exit_on_intr(vcpu))
10823 kvm_vcpu_exiting_guest_mode(vcpu);
10824 else
10825 kvm_make_request(KVM_REQ_EVENT, vcpu);
10826 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010827 } else {
10828 max_irr = kvm_lapic_find_highest_irr(vcpu);
10829 }
10830 vmx_hwapic_irr_update(vcpu, max_irr);
10831 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010832}
10833
Paolo Bonzini7e712682018-10-03 13:44:26 +020010834static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10835{
10836 u8 rvi = vmx_get_rvi();
10837 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10838
10839 return ((rvi & 0xf0) > (vppr & 0xf0));
10840}
10841
Andrey Smetanin63086302015-11-10 15:36:32 +030010842static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010843{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010844 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010845 return;
10846
Yang Zhangc7c9c562013-01-25 10:18:51 +080010847 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10848 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10849 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10850 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10851}
10852
Paolo Bonzini967235d2016-12-19 14:03:45 +010010853static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10854{
10855 struct vcpu_vmx *vmx = to_vmx(vcpu);
10856
10857 pi_clear_on(&vmx->pi_desc);
10858 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10859}
10860
Avi Kivity51aa01d2010-07-20 14:31:20 +030010861static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010862{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010863 u32 exit_intr_info = 0;
10864 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010865
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010866 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10867 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010868 return;
10869
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010870 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10871 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10872 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010873
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010874 /* if exit due to PF check for async PF */
10875 if (is_page_fault(exit_intr_info))
10876 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10877
Andi Kleena0861c02009-06-08 17:37:09 +080010878 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010879 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10880 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010881 kvm_machine_check();
10882
Gleb Natapov20f65982009-05-11 13:35:55 +030010883 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010884 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010885 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010886 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010887 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010888 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010889}
Gleb Natapov20f65982009-05-11 13:35:55 +030010890
Yang Zhanga547c6d2013-04-11 19:25:10 +080010891static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10892{
10893 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10894
Yang Zhanga547c6d2013-04-11 19:25:10 +080010895 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10896 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10897 unsigned int vector;
10898 unsigned long entry;
10899 gate_desc *desc;
10900 struct vcpu_vmx *vmx = to_vmx(vcpu);
10901#ifdef CONFIG_X86_64
10902 unsigned long tmp;
10903#endif
10904
10905 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10906 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010907 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010908 asm volatile(
10909#ifdef CONFIG_X86_64
10910 "mov %%" _ASM_SP ", %[sp]\n\t"
10911 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10912 "push $%c[ss]\n\t"
10913 "push %[sp]\n\t"
10914#endif
10915 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010916 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010917 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010918 :
10919#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010920 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010921#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010922 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010923 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010924 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010925 [ss]"i"(__KERNEL_DS),
10926 [cs]"i"(__KERNEL_CS)
10927 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010928 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010929}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010930STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010931
Tom Lendackybc226f02018-05-10 22:06:39 +020010932static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010933{
Tom Lendackybc226f02018-05-10 22:06:39 +020010934 switch (index) {
10935 case MSR_IA32_SMBASE:
10936 /*
10937 * We cannot do SMM unless we can run the guest in big
10938 * real mode.
10939 */
10940 return enable_unrestricted_guest || emulate_invalid_guest_state;
10941 case MSR_AMD64_VIRT_SPEC_CTRL:
10942 /* This is AMD only. */
10943 return false;
10944 default:
10945 return true;
10946 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010947}
10948
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010949static bool vmx_mpx_supported(void)
10950{
10951 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10952 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10953}
10954
Wanpeng Li55412b22014-12-02 19:21:30 +080010955static bool vmx_xsaves_supported(void)
10956{
10957 return vmcs_config.cpu_based_2nd_exec_ctrl &
10958 SECONDARY_EXEC_XSAVES;
10959}
10960
Avi Kivity51aa01d2010-07-20 14:31:20 +030010961static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10962{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010963 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010964 bool unblock_nmi;
10965 u8 vector;
10966 bool idtv_info_valid;
10967
10968 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010969
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010970 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010971 if (vmx->loaded_vmcs->nmi_known_unmasked)
10972 return;
10973 /*
10974 * Can't use vmx->exit_intr_info since we're not sure what
10975 * the exit reason is.
10976 */
10977 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10978 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10979 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10980 /*
10981 * SDM 3: 27.7.1.2 (September 2008)
10982 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10983 * a guest IRET fault.
10984 * SDM 3: 23.2.2 (September 2008)
10985 * Bit 12 is undefined in any of the following cases:
10986 * If the VM exit sets the valid bit in the IDT-vectoring
10987 * information field.
10988 * If the VM exit is due to a double fault.
10989 */
10990 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10991 vector != DF_VECTOR && !idtv_info_valid)
10992 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10993 GUEST_INTR_STATE_NMI);
10994 else
10995 vmx->loaded_vmcs->nmi_known_unmasked =
10996 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10997 & GUEST_INTR_STATE_NMI);
10998 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10999 vmx->loaded_vmcs->vnmi_blocked_time +=
11000 ktime_to_ns(ktime_sub(ktime_get(),
11001 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030011002}
11003
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011004static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030011005 u32 idt_vectoring_info,
11006 int instr_len_field,
11007 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030011008{
Avi Kivity51aa01d2010-07-20 14:31:20 +030011009 u8 vector;
11010 int type;
11011 bool idtv_info_valid;
11012
11013 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030011014
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011015 vcpu->arch.nmi_injected = false;
11016 kvm_clear_exception_queue(vcpu);
11017 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011018
11019 if (!idtv_info_valid)
11020 return;
11021
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011022 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030011023
Avi Kivity668f6122008-07-02 09:28:55 +030011024 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
11025 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030011026
Gleb Natapov64a7ec02009-03-30 16:03:29 +030011027 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030011028 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011029 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030011030 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030011031 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030011032 * Clear bit "block by NMI" before VM entry if a NMI
11033 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030011034 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011035 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011036 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030011037 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011038 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011039 /* fall through */
11040 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030011041 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030011042 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030011043 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030011044 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030011045 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011046 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011047 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011048 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030011049 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030011050 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011051 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030011052 break;
11053 default:
11054 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030011055 }
Avi Kivitycf393f72008-07-01 16:20:21 +030011056}
11057
Avi Kivity83422e12010-07-20 14:43:23 +030011058static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
11059{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011060 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030011061 VM_EXIT_INSTRUCTION_LEN,
11062 IDT_VECTORING_ERROR_CODE);
11063}
11064
Avi Kivityb463a6f2010-07-20 15:06:17 +030011065static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
11066{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010011067 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011068 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
11069 VM_ENTRY_INSTRUCTION_LEN,
11070 VM_ENTRY_EXCEPTION_ERROR_CODE);
11071
11072 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11073}
11074
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011075static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
11076{
11077 int i, nr_msrs;
11078 struct perf_guest_switch_msr *msrs;
11079
11080 msrs = perf_guest_get_msrs(&nr_msrs);
11081
11082 if (!msrs)
11083 return;
11084
11085 for (i = 0; i < nr_msrs; i++)
11086 if (msrs[i].host == msrs[i].guest)
11087 clear_atomic_switch_msr(vmx, msrs[i].msr);
11088 else
11089 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040011090 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011091}
11092
Sean Christophersonf459a702018-08-27 15:21:11 -070011093static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
11094{
11095 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
11096 if (!vmx->loaded_vmcs->hv_timer_armed)
11097 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11098 PIN_BASED_VMX_PREEMPTION_TIMER);
11099 vmx->loaded_vmcs->hv_timer_armed = true;
11100}
11101
11102static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070011103{
11104 struct vcpu_vmx *vmx = to_vmx(vcpu);
11105 u64 tscl;
11106 u32 delta_tsc;
11107
Sean Christophersond264ee02018-08-27 15:21:12 -070011108 if (vmx->req_immediate_exit) {
11109 vmx_arm_hv_timer(vmx, 0);
11110 return;
11111 }
11112
Sean Christophersonf459a702018-08-27 15:21:11 -070011113 if (vmx->hv_deadline_tsc != -1) {
11114 tscl = rdtsc();
11115 if (vmx->hv_deadline_tsc > tscl)
11116 /* set_hv_timer ensures the delta fits in 32-bits */
11117 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
11118 cpu_preemption_timer_multi);
11119 else
11120 delta_tsc = 0;
11121
11122 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070011123 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070011124 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070011125
Sean Christophersonf459a702018-08-27 15:21:11 -070011126 if (vmx->loaded_vmcs->hv_timer_armed)
11127 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11128 PIN_BASED_VMX_PREEMPTION_TIMER);
11129 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011130}
11131
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080011132static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011133{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011134 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011135 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020011136
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011137 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010011138 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010011139 vmx->loaded_vmcs->soft_vnmi_blocked))
11140 vmx->loaded_vmcs->entry_time = ktime_get();
11141
Avi Kivity104f2262010-11-18 13:12:52 +020011142 /* Don't enter VMX if guest state is invalid, let the exit handler
11143 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020011144 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020011145 return;
11146
Radim Krčmářa7653ec2014-08-21 18:08:07 +020011147 if (vmx->ple_window_dirty) {
11148 vmx->ple_window_dirty = false;
11149 vmcs_write32(PLE_WINDOW, vmx->ple_window);
11150 }
11151
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011152 if (vmx->nested.need_vmcs12_sync) {
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020011153 /*
11154 * hv_evmcs may end up being not mapped after migration (when
11155 * L2 was running), map it here to make sure vmcs12 changes are
11156 * properly reflected.
11157 */
11158 if (vmx->nested.enlightened_vmcs_enabled &&
11159 !vmx->nested.hv_evmcs)
11160 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
11161
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020011162 if (vmx->nested.hv_evmcs) {
11163 copy_vmcs12_to_enlightened(vmx);
11164 /* All fields are clean */
11165 vmx->nested.hv_evmcs->hv_clean_fields |=
11166 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
11167 } else {
11168 copy_vmcs12_to_shadow(vmx);
11169 }
11170 vmx->nested.need_vmcs12_sync = false;
Abel Gordon012f83c2013-04-18 14:39:25 +030011171 }
11172
Avi Kivity104f2262010-11-18 13:12:52 +020011173 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
11174 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
11175 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
11176 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
11177
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011178 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070011179 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011180 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070011181 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070011182 }
11183
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070011184 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070011185 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070011186 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070011187 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070011188 }
11189
Avi Kivity104f2262010-11-18 13:12:52 +020011190 /* When single-stepping over STI and MOV SS, we must clear the
11191 * corresponding interruptibility bits in the guest state. Otherwise
11192 * vmentry fails as it then expects bit 14 (BS) in pending debug
11193 * exceptions being set, but that's not correct for the guest debugging
11194 * case. */
11195 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
11196 vmx_set_interrupt_shadow(vcpu, 0);
11197
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020011198 if (static_cpu_has(X86_FEATURE_PKU) &&
11199 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
11200 vcpu->arch.pkru != vmx->host_pkru)
11201 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011202
Gleb Natapovd7cd9792011-10-05 14:01:23 +020011203 atomic_switch_perf_msrs(vmx);
11204
Sean Christophersonf459a702018-08-27 15:21:11 -070011205 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070011206
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011207 /*
11208 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
11209 * it's non-zero. Since vmentry is serialising on affected CPUs, there
11210 * is no need to worry about the conditional branch over the wrmsr
11211 * being speculatively taken.
11212 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020011213 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011214
Nadav Har'Eld462b812011-05-24 15:26:10 +030011215 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011216
11217 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
11218 (unsigned long)&current_evmcs->host_rsp : 0;
11219
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020011220 if (static_branch_unlikely(&vmx_l1d_should_flush))
11221 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011222
Avi Kivity104f2262010-11-18 13:12:52 +020011223 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080011224 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011225 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
11226 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
11227 "push %%" _ASM_CX " \n\t"
11228 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030011229 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011230 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011231 /* Avoid VMWRITE when Enlightened VMCS is in use */
11232 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
11233 "jz 2f \n\t"
11234 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
11235 "jmp 1f \n\t"
11236 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011237 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030011238 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030011239 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011240 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
11241 "mov %%cr2, %%" _ASM_DX " \n\t"
11242 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011243 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011244 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011245 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011246 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020011247 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011248 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011249 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
11250 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
11251 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
11252 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
11253 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
11254 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011255#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020011256 "mov %c[r8](%0), %%r8 \n\t"
11257 "mov %c[r9](%0), %%r9 \n\t"
11258 "mov %c[r10](%0), %%r10 \n\t"
11259 "mov %c[r11](%0), %%r11 \n\t"
11260 "mov %c[r12](%0), %%r12 \n\t"
11261 "mov %c[r13](%0), %%r13 \n\t"
11262 "mov %c[r14](%0), %%r14 \n\t"
11263 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011264#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030011265 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030011266
Avi Kivity6aa8b732006-12-10 02:21:36 -080011267 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030011268 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011269 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011270 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020011271 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011272 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080011273 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030011274 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020011275 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080011276 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011277 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
11278 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
11279 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
11280 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
11281 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
11282 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
11283 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011284#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020011285 "mov %%r8, %c[r8](%0) \n\t"
11286 "mov %%r9, %c[r9](%0) \n\t"
11287 "mov %%r10, %c[r10](%0) \n\t"
11288 "mov %%r11, %c[r11](%0) \n\t"
11289 "mov %%r12, %c[r12](%0) \n\t"
11290 "mov %%r13, %c[r13](%0) \n\t"
11291 "mov %%r14, %c[r14](%0) \n\t"
11292 "mov %%r15, %c[r15](%0) \n\t"
Uros Bizjak43ce76c2018-10-17 16:46:57 +020011293 /*
11294 * Clear host registers marked as clobbered to prevent
11295 * speculative use.
11296 */
Jim Mattson0cb5b302018-01-03 14:31:38 -080011297 "xor %%r8d, %%r8d \n\t"
11298 "xor %%r9d, %%r9d \n\t"
11299 "xor %%r10d, %%r10d \n\t"
11300 "xor %%r11d, %%r11d \n\t"
11301 "xor %%r12d, %%r12d \n\t"
11302 "xor %%r13d, %%r13d \n\t"
11303 "xor %%r14d, %%r14d \n\t"
11304 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080011305#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030011306 "mov %%cr2, %%" _ASM_AX " \n\t"
11307 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030011308
Jim Mattson0cb5b302018-01-03 14:31:38 -080011309 "xor %%eax, %%eax \n\t"
11310 "xor %%ebx, %%ebx \n\t"
11311 "xor %%esi, %%esi \n\t"
11312 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011313 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030011314 ".pushsection .rodata \n\t"
11315 ".global vmx_return \n\t"
11316 "vmx_return: " _ASM_PTR " 2b \n\t"
11317 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011318 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030011319 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020011320 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030011321 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080011322 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
11323 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
11324 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
11325 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
11326 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
11327 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
11328 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080011329#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080011330 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
11331 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
11332 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
11333 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
11334 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
11335 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
11336 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
11337 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080011338#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020011339 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
11340 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020011341 : "cc", "memory"
11342#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011343 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020011344 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030011345#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011346 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020011347#endif
11348 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080011349
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011350 /*
11351 * We do not use IBRS in the kernel. If this vCPU has used the
11352 * SPEC_CTRL MSR it may have left it on; save the value and
11353 * turn it off. This is much more efficient than blindly adding
11354 * it to the atomic save/restore list. Especially as the former
11355 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
11356 *
11357 * For non-nested case:
11358 * If the L01 MSR bitmap does not intercept the MSR, then we need to
11359 * save it.
11360 *
11361 * For nested case:
11362 * If the L02 MSR bitmap does not intercept the MSR, then we need to
11363 * save it.
11364 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010011365 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010011366 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011367
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020011368 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011369
David Woodhouse117cc7a2018-01-12 11:11:27 +000011370 /* Eliminate branch target predictions from guest mode */
11371 vmexit_fill_RSB();
11372
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010011373 /* All fields are clean at this point */
11374 if (static_branch_unlikely(&enable_evmcs))
11375 current_evmcs->hv_clean_fields |=
11376 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
11377
Gleb Natapov2a7921b2012-08-12 16:12:29 +030011378 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080011379 if (vmx->host_debugctlmsr)
11380 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030011381
Avi Kivityaa67f602012-08-01 16:48:03 +030011382#ifndef CONFIG_X86_64
11383 /*
11384 * The sysexit path does not restore ds/es, so we must set them to
11385 * a reasonable value ourselves.
11386 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070011387 * We can't defer this to vmx_prepare_switch_to_host() since that
11388 * function may be executed in interrupt context, which saves and
11389 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030011390 */
11391 loadsegment(ds, __USER_DS);
11392 loadsegment(es, __USER_DS);
11393#endif
11394
Avi Kivity6de4f3a2009-05-31 22:58:47 +030011395 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020011396 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020011397 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030011398 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020011399 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011400 vcpu->arch.regs_dirty = 0;
11401
Gleb Natapove0b890d2013-09-25 12:51:33 +030011402 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011403 * eager fpu is enabled if PKEY is supported and CR4 is switched
11404 * back on host, so it is safe to read guest PKRU from current
11405 * XSAVE.
11406 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020011407 if (static_cpu_has(X86_FEATURE_PKU) &&
11408 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
11409 vcpu->arch.pkru = __read_pkru();
11410 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011411 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080011412 }
11413
Gleb Natapove0b890d2013-09-25 12:51:33 +030011414 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070011415 vmx->idt_vectoring_info = 0;
11416
11417 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
11418 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
11419 return;
11420
11421 vmx->loaded_vmcs->launched = 1;
11422 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030011423
Avi Kivity51aa01d2010-07-20 14:31:20 +030011424 vmx_complete_atomic_exit(vmx);
11425 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030011426 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011427}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050011428STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011429
Sean Christopherson434a1e92018-03-20 12:17:18 -070011430static struct kvm *vmx_vm_alloc(void)
11431{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070011432 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070011433 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070011434}
11435
11436static void vmx_vm_free(struct kvm *kvm)
11437{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070011438 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070011439}
11440
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011441static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011442{
11443 struct vcpu_vmx *vmx = to_vmx(vcpu);
11444 int cpu;
11445
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011446 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011447 return;
11448
11449 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011450 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070011451 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011452 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011453 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070011454
11455 vm_entry_controls_reset_shadow(vmx);
11456 vm_exit_controls_reset_shadow(vmx);
11457 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011458}
11459
Jim Mattson2f1fe812016-07-08 15:36:06 -070011460/*
11461 * Ensure that the current vmcs of the logical processor is the
11462 * vmcs01 of the vcpu before calling free_nested().
11463 */
11464static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
11465{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011466 vcpu_load(vcpu);
11467 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
11468 free_nested(vcpu);
11469 vcpu_put(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070011470}
11471
Avi Kivity6aa8b732006-12-10 02:21:36 -080011472static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
11473{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011474 struct vcpu_vmx *vmx = to_vmx(vcpu);
11475
Kai Huang843e4332015-01-28 10:54:28 +080011476 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080011477 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080011478 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011479 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070011480 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020011481 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011482 kfree(vmx->guest_msrs);
11483 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100011484 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011485}
11486
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011487static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011488{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011489 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100011490 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011491 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030011492 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011493
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011494 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011495 return ERR_PTR(-ENOMEM);
11496
Wanpeng Li991e7a02015-09-16 17:30:05 +080011497 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011498
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011499 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11500 if (err)
11501 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011502
Peter Feiner4e595162016-07-07 14:49:58 -070011503 err = -ENOMEM;
11504
11505 /*
11506 * If PML is turned on, failure on enabling PML just results in failure
11507 * of creating the vcpu, therefore we can simplify PML logic (by
11508 * avoiding dealing with cases, such as enabling PML partially on vcpus
11509 * for the guest, etc.
11510 */
11511 if (enable_pml) {
11512 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11513 if (!vmx->pml_pg)
11514 goto uninit_vcpu;
11515 }
11516
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011517 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011518 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11519 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011520
Peter Feiner4e595162016-07-07 14:49:58 -070011521 if (!vmx->guest_msrs)
11522 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011523
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011524 err = alloc_loaded_vmcs(&vmx->vmcs01);
11525 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011526 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011527
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011528 msr_bitmap = vmx->vmcs01.msr_bitmap;
11529 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11530 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11531 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11532 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11533 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11534 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11535 vmx->msr_bitmap_mode = 0;
11536
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011537 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011538 cpu = get_cpu();
11539 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011540 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011541 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011542 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011543 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011544 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011545 err = alloc_apic_access_page(kvm);
11546 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011547 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011548 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011549
Sean Christophersone90008d2018-03-05 12:04:37 -080011550 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011551 err = init_rmode_identity_map(kvm);
11552 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011553 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011554 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011555
Roman Kagan63aff652018-07-19 21:59:07 +030011556 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011557 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11558 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011559
Wincy Van705699a2015-02-03 23:58:17 +080011560 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011561 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011562
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011563 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11564
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011565 /*
11566 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11567 * or POSTED_INTR_WAKEUP_VECTOR.
11568 */
11569 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11570 vmx->pi_desc.sn = 1;
11571
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011572 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011573
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011574free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011575 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011576free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011577 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011578free_pml:
11579 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011580uninit_vcpu:
11581 kvm_vcpu_uninit(&vmx->vcpu);
11582free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011583 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011584 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011585 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011586}
11587
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011588#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"
11589#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 -040011590
Wanpeng Lib31c1142018-03-12 04:53:04 -070011591static int vmx_vm_init(struct kvm *kvm)
11592{
Tianyu Lan877ad952018-07-19 08:40:23 +000011593 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11594
Wanpeng Lib31c1142018-03-12 04:53:04 -070011595 if (!ple_gap)
11596 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011597
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011598 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11599 switch (l1tf_mitigation) {
11600 case L1TF_MITIGATION_OFF:
11601 case L1TF_MITIGATION_FLUSH_NOWARN:
11602 /* 'I explicitly don't care' is set */
11603 break;
11604 case L1TF_MITIGATION_FLUSH:
11605 case L1TF_MITIGATION_FLUSH_NOSMT:
11606 case L1TF_MITIGATION_FULL:
11607 /*
11608 * Warn upon starting the first VM in a potentially
11609 * insecure environment.
11610 */
11611 if (cpu_smt_control == CPU_SMT_ENABLED)
11612 pr_warn_once(L1TF_MSG_SMT);
11613 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11614 pr_warn_once(L1TF_MSG_L1D);
11615 break;
11616 case L1TF_MITIGATION_FULL_FORCE:
11617 /* Flush is enforced */
11618 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011619 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011620 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011621 return 0;
11622}
11623
Yang, Sheng002c7f72007-07-31 14:23:01 +030011624static void __init vmx_check_processor_compat(void *rtn)
11625{
11626 struct vmcs_config vmcs_conf;
11627
11628 *(int *)rtn = 0;
11629 if (setup_vmcs_config(&vmcs_conf) < 0)
11630 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011631 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011632 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11633 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11634 smp_processor_id());
11635 *(int *)rtn = -EIO;
11636 }
11637}
11638
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011639static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011640{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011641 u8 cache;
11642 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011643
Sheng Yang522c68c2009-04-27 20:35:43 +080011644 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011645 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011646 * 2. EPT with VT-d:
11647 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011648 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011649 * b. VT-d with snooping control feature: snooping control feature of
11650 * VT-d engine can guarantee the cache correctness. Just set it
11651 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011652 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011653 * consistent with host MTRR
11654 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011655 if (is_mmio) {
11656 cache = MTRR_TYPE_UNCACHABLE;
11657 goto exit;
11658 }
11659
11660 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011661 ipat = VMX_EPT_IPAT_BIT;
11662 cache = MTRR_TYPE_WRBACK;
11663 goto exit;
11664 }
11665
11666 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11667 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011668 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011669 cache = MTRR_TYPE_WRBACK;
11670 else
11671 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011672 goto exit;
11673 }
11674
Xiao Guangrongff536042015-06-15 16:55:22 +080011675 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011676
11677exit:
11678 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011679}
11680
Sheng Yang17cc3932010-01-05 19:02:27 +080011681static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011682{
Sheng Yang878403b2010-01-05 19:02:29 +080011683 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11684 return PT_DIRECTORY_LEVEL;
11685 else
11686 /* For shadow and EPT supported 1GB page */
11687 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011688}
11689
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011690static void vmcs_set_secondary_exec_control(u32 new_ctl)
11691{
11692 /*
11693 * These bits in the secondary execution controls field
11694 * are dynamic, the others are mostly based on the hypervisor
11695 * architecture and the guest's CPUID. Do not touch the
11696 * dynamic bits.
11697 */
11698 u32 mask =
11699 SECONDARY_EXEC_SHADOW_VMCS |
11700 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011701 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11702 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011703
11704 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11705
11706 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11707 (new_ctl & ~mask) | (cur_ctl & mask));
11708}
11709
David Matlack8322ebb2016-11-29 18:14:09 -080011710/*
11711 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11712 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11713 */
11714static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11715{
11716 struct vcpu_vmx *vmx = to_vmx(vcpu);
11717 struct kvm_cpuid_entry2 *entry;
11718
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011719 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11720 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011721
11722#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11723 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011724 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011725} while (0)
11726
11727 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11728 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11729 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11730 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11731 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11732 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11733 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11734 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11735 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11736 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11737 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11738 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11739 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11740 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11741 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11742
11743 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11744 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11745 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11746 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11747 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011748 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011749
11750#undef cr4_fixed1_update
11751}
11752
Liran Alon5f76f6f2018-09-14 03:25:52 +030011753static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11754{
11755 struct vcpu_vmx *vmx = to_vmx(vcpu);
11756
11757 if (kvm_mpx_supported()) {
11758 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11759
11760 if (mpx_enabled) {
11761 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11762 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11763 } else {
11764 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11765 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11766 }
11767 }
11768}
11769
Sheng Yang0e851882009-12-18 16:48:46 +080011770static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11771{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011772 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011773
Paolo Bonzini80154d72017-08-24 13:55:35 +020011774 if (cpu_has_secondary_exec_ctrls()) {
11775 vmx_compute_secondary_exec_control(vmx);
11776 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011777 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011778
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011779 if (nested_vmx_allowed(vcpu))
11780 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11781 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11782 else
11783 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11784 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011785
Liran Alon5f76f6f2018-09-14 03:25:52 +030011786 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011787 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011788 nested_vmx_entry_exit_ctls_update(vcpu);
11789 }
Sheng Yang0e851882009-12-18 16:48:46 +080011790}
11791
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011792static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11793{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011794 if (func == 1 && nested)
11795 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011796}
11797
Yang Zhang25d92082013-08-06 12:00:32 +030011798static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11799 struct x86_exception *fault)
11800{
Jan Kiszka533558b2014-01-04 18:47:20 +010011801 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011802 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011803 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011804 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011805
Bandan Dasc5f983f2017-05-05 15:25:14 -040011806 if (vmx->nested.pml_full) {
11807 exit_reason = EXIT_REASON_PML_FULL;
11808 vmx->nested.pml_full = false;
11809 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11810 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011811 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011812 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011813 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011814
11815 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011816 vmcs12->guest_physical_address = fault->address;
11817}
11818
Peter Feiner995f00a2017-06-30 17:26:32 -070011819static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11820{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011821 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011822}
11823
Nadav Har'El155a97a2013-08-05 11:07:16 +030011824/* Callbacks for nested_ept_init_mmu_context: */
11825
11826static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11827{
11828 /* return the page table to be shadowed - in our case, EPT12 */
11829 return get_vmcs12(vcpu)->ept_pointer;
11830}
11831
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011832static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011833{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011834 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011835
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011836 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +020011837 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011838 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011839 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011840 nested_ept_ad_enabled(vcpu),
11841 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011842 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
11843 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
11844 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +020011845 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011846
11847 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011848}
11849
11850static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11851{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011852 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011853 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011854}
11855
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011856static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11857 u16 error_code)
11858{
11859 bool inequality, bit;
11860
11861 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11862 inequality =
11863 (error_code & vmcs12->page_fault_error_code_mask) !=
11864 vmcs12->page_fault_error_code_match;
11865 return inequality ^ bit;
11866}
11867
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011868static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11869 struct x86_exception *fault)
11870{
11871 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11872
11873 WARN_ON(!is_guest_mode(vcpu));
11874
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011875 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11876 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011877 vmcs12->vm_exit_intr_error_code = fault->error_code;
11878 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11879 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11880 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11881 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011882 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011883 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011884 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011885}
11886
Paolo Bonzinic9923842017-12-13 14:16:30 +010011887static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11888 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011889
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011890static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011891{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011892 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011893 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011894 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011895 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011896
11897 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011898 /*
11899 * Translate L1 physical address to host physical
11900 * address for vmcs02. Keep the page pinned, so this
11901 * physical address remains valid. We keep a reference
11902 * to it so we can release it later.
11903 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011904 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011905 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011906 vmx->nested.apic_access_page = NULL;
11907 }
11908 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011909 /*
11910 * If translation failed, no matter: This feature asks
11911 * to exit when accessing the given address, and if it
11912 * can never be accessed, this feature won't do
11913 * anything anyway.
11914 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011915 if (!is_error_page(page)) {
11916 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011917 hpa = page_to_phys(vmx->nested.apic_access_page);
11918 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11919 } else {
11920 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11921 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11922 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011923 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011924
11925 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011926 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011927 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011928 vmx->nested.virtual_apic_page = NULL;
11929 }
11930 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011931
11932 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011933 * If translation failed, VM entry will fail because
11934 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11935 * Failing the vm entry is _not_ what the processor
11936 * does but it's basically the only possibility we
11937 * have. We could still enter the guest if CR8 load
11938 * exits are enabled, CR8 store exits are enabled, and
11939 * virtualize APIC access is disabled; in this case
11940 * the processor would never use the TPR shadow and we
11941 * could simply clear the bit from the execution
11942 * control. But such a configuration is useless, so
11943 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011944 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011945 if (!is_error_page(page)) {
11946 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011947 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11948 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11949 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011950 }
11951
Wincy Van705699a2015-02-03 23:58:17 +080011952 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011953 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11954 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011955 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011956 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011957 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011958 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11959 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011960 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011961 vmx->nested.pi_desc_page = page;
11962 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011963 vmx->nested.pi_desc =
11964 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11965 (unsigned long)(vmcs12->posted_intr_desc_addr &
11966 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011967 vmcs_write64(POSTED_INTR_DESC_ADDR,
11968 page_to_phys(vmx->nested.pi_desc_page) +
11969 (unsigned long)(vmcs12->posted_intr_desc_addr &
11970 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011971 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011972 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011973 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11974 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011975 else
11976 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11977 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011978}
11979
Jan Kiszkaf41245002014-03-07 20:03:13 +010011980static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11981{
11982 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11983 struct vcpu_vmx *vmx = to_vmx(vcpu);
11984
Sean Christopherson4c008122018-08-27 15:21:10 -070011985 /*
11986 * A timer value of zero is architecturally guaranteed to cause
11987 * a VMExit prior to executing any instructions in the guest.
11988 */
11989 if (preemption_timeout == 0) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010011990 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11991 return;
11992 }
11993
Sean Christopherson4c008122018-08-27 15:21:10 -070011994 if (vcpu->arch.virtual_tsc_khz == 0)
11995 return;
11996
Jan Kiszkaf41245002014-03-07 20:03:13 +010011997 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11998 preemption_timeout *= 1000000;
11999 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
12000 hrtimer_start(&vmx->nested.preemption_timer,
12001 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
12002}
12003
Jim Mattson56a20512017-07-06 16:33:06 -070012004static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
12005 struct vmcs12 *vmcs12)
12006{
12007 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
12008 return 0;
12009
12010 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
12011 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
12012 return -EINVAL;
12013
12014 return 0;
12015}
12016
Wincy Van3af18d92015-02-03 23:49:31 +080012017static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
12018 struct vmcs12 *vmcs12)
12019{
Wincy Van3af18d92015-02-03 23:49:31 +080012020 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
12021 return 0;
12022
Jim Mattson5fa99cb2017-07-06 16:33:07 -070012023 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080012024 return -EINVAL;
12025
12026 return 0;
12027}
12028
Jim Mattson712b12d2017-08-24 13:24:47 -070012029static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
12030 struct vmcs12 *vmcs12)
12031{
12032 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
12033 return 0;
12034
12035 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
12036 return -EINVAL;
12037
12038 return 0;
12039}
12040
Wincy Van3af18d92015-02-03 23:49:31 +080012041/*
12042 * Merge L0's and L1's MSR bitmap, return false to indicate that
12043 * we do not use the hardware.
12044 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010012045static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
12046 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080012047{
Wincy Van82f0dd42015-02-03 23:57:18 +080012048 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080012049 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020012050 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012051 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010012052 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012053 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010012054 *
12055 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
12056 * ensures that we do not accidentally generate an L02 MSR bitmap
12057 * from the L12 MSR bitmap that is too permissive.
12058 * 2. That L1 or L2s have actually used the MSR. This avoids
12059 * unnecessarily merging of the bitmap if the MSR is unused. This
12060 * works properly because we only update the L01 MSR bitmap lazily.
12061 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
12062 * updated to reflect this when L1 (or its L2s) actually write to
12063 * the MSR.
12064 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000012065 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
12066 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080012067
Paolo Bonzinic9923842017-12-13 14:16:30 +010012068 /* Nothing to do if the MSR bitmap is not in use. */
12069 if (!cpu_has_vmx_msr_bitmap() ||
12070 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
12071 return false;
12072
Ashok Raj15d45072018-02-01 22:59:43 +010012073 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012074 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080012075 return false;
12076
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012077 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
12078 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080012079 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010012080
Radim Krčmářd048c092016-08-08 20:16:22 +020012081 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010012082 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
12083 /*
12084 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
12085 * just lets the processor take the value from the virtual-APIC page;
12086 * take those 256 bits directly from the L1 bitmap.
12087 */
12088 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
12089 unsigned word = msr / BITS_PER_LONG;
12090 msr_bitmap_l0[word] = msr_bitmap_l1[word];
12091 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080012092 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010012093 } else {
12094 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
12095 unsigned word = msr / BITS_PER_LONG;
12096 msr_bitmap_l0[word] = ~0;
12097 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
12098 }
12099 }
12100
12101 nested_vmx_disable_intercept_for_msr(
12102 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012103 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012104 MSR_TYPE_W);
12105
12106 if (nested_cpu_has_vid(vmcs12)) {
12107 nested_vmx_disable_intercept_for_msr(
12108 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012109 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012110 MSR_TYPE_W);
12111 nested_vmx_disable_intercept_for_msr(
12112 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010012113 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010012114 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080012115 }
Ashok Raj15d45072018-02-01 22:59:43 +010012116
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010012117 if (spec_ctrl)
12118 nested_vmx_disable_intercept_for_msr(
12119 msr_bitmap_l1, msr_bitmap_l0,
12120 MSR_IA32_SPEC_CTRL,
12121 MSR_TYPE_R | MSR_TYPE_W);
12122
Ashok Raj15d45072018-02-01 22:59:43 +010012123 if (pred_cmd)
12124 nested_vmx_disable_intercept_for_msr(
12125 msr_bitmap_l1, msr_bitmap_l0,
12126 MSR_IA32_PRED_CMD,
12127 MSR_TYPE_W);
12128
Wincy Vanf2b93282015-02-03 23:56:03 +080012129 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012130 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080012131
12132 return true;
12133}
12134
Liran Alon61ada742018-06-23 02:35:08 +030012135static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
12136 struct vmcs12 *vmcs12)
12137{
12138 struct vmcs12 *shadow;
12139 struct page *page;
12140
12141 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
12142 vmcs12->vmcs_link_pointer == -1ull)
12143 return;
12144
12145 shadow = get_shadow_vmcs12(vcpu);
12146 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12147
12148 memcpy(shadow, kmap(page), VMCS12_SIZE);
12149
12150 kunmap(page);
12151 kvm_release_page_clean(page);
12152}
12153
12154static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
12155 struct vmcs12 *vmcs12)
12156{
12157 struct vcpu_vmx *vmx = to_vmx(vcpu);
12158
12159 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
12160 vmcs12->vmcs_link_pointer == -1ull)
12161 return;
12162
12163 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
12164 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
12165}
12166
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012167static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
12168 struct vmcs12 *vmcs12)
12169{
12170 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
12171 !page_address_valid(vcpu, vmcs12->apic_access_addr))
12172 return -EINVAL;
12173 else
12174 return 0;
12175}
12176
Wincy Vanf2b93282015-02-03 23:56:03 +080012177static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
12178 struct vmcs12 *vmcs12)
12179{
Wincy Van82f0dd42015-02-03 23:57:18 +080012180 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080012181 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080012182 !nested_cpu_has_vid(vmcs12) &&
12183 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080012184 return 0;
12185
12186 /*
12187 * If virtualize x2apic mode is enabled,
12188 * virtualize apic access must be disabled.
12189 */
Wincy Van82f0dd42015-02-03 23:57:18 +080012190 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
12191 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080012192 return -EINVAL;
12193
Wincy Van608406e2015-02-03 23:57:51 +080012194 /*
12195 * If virtual interrupt delivery is enabled,
12196 * we must exit on external interrupts.
12197 */
12198 if (nested_cpu_has_vid(vmcs12) &&
12199 !nested_exit_on_intr(vcpu))
12200 return -EINVAL;
12201
Wincy Van705699a2015-02-03 23:58:17 +080012202 /*
12203 * bits 15:8 should be zero in posted_intr_nv,
12204 * the descriptor address has been already checked
12205 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040012206 *
12207 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080012208 */
12209 if (nested_cpu_has_posted_intr(vmcs12) &&
12210 (!nested_cpu_has_vid(vmcs12) ||
12211 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040012212 (vmcs12->posted_intr_nv & 0xff00) ||
12213 (vmcs12->posted_intr_desc_addr & 0x3f) ||
KarimAllah Ahmed22a7cdc2018-10-20 23:42:59 +020012214 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
Wincy Van705699a2015-02-03 23:58:17 +080012215 return -EINVAL;
12216
Wincy Vanf2b93282015-02-03 23:56:03 +080012217 /* tpr shadow is needed by all apicv features. */
12218 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
12219 return -EINVAL;
12220
12221 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080012222}
12223
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012224static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
12225 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012226 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030012227{
Liran Alone2536742018-06-23 02:35:02 +030012228 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012229 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012230 u64 count, addr;
12231
Liran Alone2536742018-06-23 02:35:02 +030012232 if (vmcs12_read_any(vmcs12, count_field, &count) ||
12233 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012234 WARN_ON(1);
12235 return -EINVAL;
12236 }
12237 if (count == 0)
12238 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012239 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012240 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
12241 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012242 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012243 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
12244 addr_field, maxphyaddr, count, addr);
12245 return -EINVAL;
12246 }
12247 return 0;
12248}
12249
12250static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
12251 struct vmcs12 *vmcs12)
12252{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012253 if (vmcs12->vm_exit_msr_load_count == 0 &&
12254 vmcs12->vm_exit_msr_store_count == 0 &&
12255 vmcs12->vm_entry_msr_load_count == 0)
12256 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012257 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012258 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012259 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012260 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012261 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030012262 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030012263 return -EINVAL;
12264 return 0;
12265}
12266
Bandan Dasc5f983f2017-05-05 15:25:14 -040012267static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
12268 struct vmcs12 *vmcs12)
12269{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040012270 if (!nested_cpu_has_pml(vmcs12))
12271 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040012272
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040012273 if (!nested_cpu_has_ept(vmcs12) ||
12274 !page_address_valid(vcpu, vmcs12->pml_address))
12275 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040012276
12277 return 0;
12278}
12279
Liran Alona8a7c022018-06-23 02:35:06 +030012280static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
12281 struct vmcs12 *vmcs12)
12282{
12283 if (!nested_cpu_has_shadow_vmcs(vmcs12))
12284 return 0;
12285
12286 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
12287 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
12288 return -EINVAL;
12289
12290 return 0;
12291}
12292
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012293static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
12294 struct vmx_msr_entry *e)
12295{
12296 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020012297 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012298 return -EINVAL;
12299 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
12300 e->index == MSR_IA32_UCODE_REV)
12301 return -EINVAL;
12302 if (e->reserved != 0)
12303 return -EINVAL;
12304 return 0;
12305}
12306
12307static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
12308 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030012309{
12310 if (e->index == MSR_FS_BASE ||
12311 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012312 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
12313 nested_vmx_msr_check_common(vcpu, e))
12314 return -EINVAL;
12315 return 0;
12316}
12317
12318static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
12319 struct vmx_msr_entry *e)
12320{
12321 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
12322 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030012323 return -EINVAL;
12324 return 0;
12325}
12326
12327/*
12328 * Load guest's/host's msr at nested entry/exit.
12329 * return 0 for success, entry index for failure.
12330 */
12331static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12332{
12333 u32 i;
12334 struct vmx_msr_entry e;
12335 struct msr_data msr;
12336
12337 msr.host_initiated = false;
12338 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012339 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
12340 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012341 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012342 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12343 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030012344 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012345 }
12346 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012347 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012348 "%s check failed (%u, 0x%x, 0x%x)\n",
12349 __func__, i, e.index, e.reserved);
12350 goto fail;
12351 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012352 msr.index = e.index;
12353 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012354 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012355 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012356 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
12357 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030012358 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012359 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012360 }
12361 return 0;
12362fail:
12363 return i + 1;
12364}
12365
12366static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
12367{
12368 u32 i;
12369 struct vmx_msr_entry e;
12370
12371 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012372 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012373 if (kvm_vcpu_read_guest(vcpu,
12374 gpa + i * sizeof(e),
12375 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012376 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012377 "%s cannot read MSR entry (%u, 0x%08llx)\n",
12378 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030012379 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012380 }
12381 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012382 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012383 "%s check failed (%u, 0x%x, 0x%x)\n",
12384 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030012385 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012386 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012387 msr_info.host_initiated = false;
12388 msr_info.index = e.index;
12389 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012390 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012391 "%s cannot read MSR (%u, 0x%x)\n",
12392 __func__, i, e.index);
12393 return -EINVAL;
12394 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020012395 if (kvm_vcpu_write_guest(vcpu,
12396 gpa + i * sizeof(e) +
12397 offsetof(struct vmx_msr_entry, value),
12398 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020012399 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012400 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020012401 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030012402 return -EINVAL;
12403 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012404 }
12405 return 0;
12406}
12407
Ladi Prosek1dc35da2016-11-30 16:03:11 +010012408static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
12409{
12410 unsigned long invalid_mask;
12411
12412 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
12413 return (val & invalid_mask) == 0;
12414}
12415
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012416/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012417 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
12418 * emulating VM entry into a guest with EPT enabled.
12419 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12420 * is assigned to entry_failure_code on failure.
12421 */
12422static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080012423 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012424{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012425 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010012426 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012427 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12428 return 1;
12429 }
12430
12431 /*
12432 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
12433 * must not be dereferenced.
12434 */
12435 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
12436 !nested_ept) {
12437 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
12438 *entry_failure_code = ENTRY_FAIL_PDPTE;
12439 return 1;
12440 }
12441 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012442 }
12443
Junaid Shahid50c28f22018-06-27 14:59:11 -070012444 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070012445 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070012446
12447 vcpu->arch.cr3 = cr3;
12448 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
12449
12450 kvm_init_mmu(vcpu, false);
12451
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012452 return 0;
12453}
12454
Liran Alonefebf0a2018-10-08 23:42:20 +030012455/*
12456 * Returns if KVM is able to config CPU to tag TLB entries
12457 * populated by L2 differently than TLB entries populated
12458 * by L1.
12459 *
12460 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
12461 *
12462 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
12463 * with different VPID (L1 entries are tagged with vmx->vpid
12464 * while L2 entries are tagged with vmx->nested.vpid02).
12465 */
12466static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012467{
Liran Alonefebf0a2018-10-08 23:42:20 +030012468 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012469
Liran Alonefebf0a2018-10-08 23:42:20 +030012470 return nested_cpu_has_ept(vmcs12) ||
12471 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
12472}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012473
Sean Christopherson3df5c372018-09-26 09:23:44 -070012474static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12475{
12476 if (vmx->nested.nested_run_pending &&
12477 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
12478 return vmcs12->guest_ia32_efer;
12479 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12480 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
12481 else
12482 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
12483}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012484
Sean Christopherson09abe322018-09-26 09:23:50 -070012485static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012486{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012487 /*
Sean Christopherson9d6105b2018-09-26 09:23:51 -070012488 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070012489 * according to L0's settings (vmcs12 is irrelevant here). Host
12490 * fields that come from L0 and are not constant, e.g. HOST_CR3,
12491 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012492 */
Sean Christopherson9d6105b2018-09-26 09:23:51 -070012493 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070012494 return;
Sean Christopherson9d6105b2018-09-26 09:23:51 -070012495 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012496
12497 /*
Sean Christopherson52017602018-09-26 09:23:57 -070012498 * We don't care what the EPTP value is we just need to guarantee
12499 * it's valid so we don't get a false positive when doing early
12500 * consistency checks.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012501 */
Sean Christopherson52017602018-09-26 09:23:57 -070012502 if (enable_ept && nested_early_check)
12503 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012504
12505 /* All VMFUNCs are currently emulated through L0 vmexits. */
12506 if (cpu_has_vmx_vmfunc())
12507 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12508
Sean Christopherson09abe322018-09-26 09:23:50 -070012509 if (cpu_has_vmx_posted_intr())
12510 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12511
12512 if (cpu_has_vmx_msr_bitmap())
12513 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12514
12515 if (enable_pml)
12516 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012517
12518 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012519 * Set the MSR load/store lists to match L0's settings. Only the
12520 * addresses are constant (for vmcs02), the counts can change based
12521 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012522 */
12523 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012524 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012525 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012526
Sean Christopherson09abe322018-09-26 09:23:50 -070012527 vmx_set_constant_host_state(vmx);
12528}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012529
Sean Christopherson09abe322018-09-26 09:23:50 -070012530static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
12531 struct vmcs12 *vmcs12)
12532{
12533 prepare_vmcs02_constant_state(vmx);
12534
12535 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012536
12537 if (enable_vpid) {
12538 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12539 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12540 else
12541 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12542 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012543}
12544
Sean Christopherson09abe322018-09-26 09:23:50 -070012545static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012546{
Bandan Das03efce62017-05-05 15:25:15 -040012547 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012548 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012549
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012550 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
Sean Christopherson09abe322018-09-26 09:23:50 -070012551 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012552
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012553 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012554 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12555 * entry, but only if the current (host) sp changed from the value
12556 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12557 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070012558 * here we just force the write to happen on entry. host_rsp will
12559 * also be written unconditionally by nested_vmx_check_vmentry_hw()
12560 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012561 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012562 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012563
Sean Christopherson09abe322018-09-26 09:23:50 -070012564 /*
12565 * PIN CONTROLS
12566 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010012567 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012568
Sean Christophersonf459a702018-08-27 15:21:11 -070012569 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012570 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012571 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12572 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012573
12574 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012575 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012576 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12577 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012578 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012579 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012580 }
Jan Kiszkaf41245002014-03-07 20:03:13 +010012581 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012582
Sean Christopherson09abe322018-09-26 09:23:50 -070012583 /*
12584 * EXEC CONTROLS
12585 */
12586 exec_control = vmx_exec_control(vmx); /* L0's desires */
12587 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12588 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12589 exec_control &= ~CPU_BASED_TPR_SHADOW;
12590 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012591
Sean Christopherson09abe322018-09-26 09:23:50 -070012592 /*
12593 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12594 * nested_get_vmcs12_pages can't fix it up, the illegal value
12595 * will result in a VM entry failure.
12596 */
12597 if (exec_control & CPU_BASED_TPR_SHADOW) {
12598 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12599 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12600 } else {
12601#ifdef CONFIG_X86_64
12602 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12603 CPU_BASED_CR8_STORE_EXITING;
12604#endif
12605 }
12606
12607 /*
12608 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12609 * for I/O port accesses.
12610 */
12611 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12612 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12613 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12614
12615 /*
12616 * SECONDARY EXEC CONTROLS
12617 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012618 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012619 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012620
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012621 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012622 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012623 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012624 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012625 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012626 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012627 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12628 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012629 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012630 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12631 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12632 ~SECONDARY_EXEC_ENABLE_PML;
12633 exec_control |= vmcs12_exec_ctrl;
12634 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012635
Liran Alon32c7acf2018-06-23 02:35:11 +030012636 /* VMCS shadowing for L2 is emulated for now */
12637 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12638
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012639 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012640 vmcs_write16(GUEST_INTR_STATUS,
12641 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012642
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012643 /*
12644 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12645 * nested_get_vmcs12_pages will either fix it up or
12646 * remove the VM execution control.
12647 */
12648 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12649 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12650
Sean Christopherson0b665d32018-08-14 09:33:34 -070012651 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12652 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12653
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012654 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12655 }
12656
Jim Mattson83bafef2016-10-04 10:48:38 -070012657 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012658 * ENTRY CONTROLS
12659 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012660 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012661 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12662 * on the related bits (if supported by the CPU) in the hope that
12663 * we can avoid VMWrites during vmx_set_efer().
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012664 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012665 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12666 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12667 if (cpu_has_load_ia32_efer) {
12668 if (guest_efer & EFER_LMA)
12669 exec_control |= VM_ENTRY_IA32E_MODE;
12670 if (guest_efer != host_efer)
12671 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12672 }
12673 vm_entry_controls_init(vmx, exec_control);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012674
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012675 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012676 * EXIT CONTROLS
12677 *
12678 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12679 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12680 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012681 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012682 exec_control = vmcs_config.vmexit_ctrl;
12683 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12684 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12685 vm_exit_controls_init(vmx, exec_control);
12686
12687 /*
12688 * Conceptually we want to copy the PML address and index from
12689 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12690 * since we always flush the log on each vmexit and never change
12691 * the PML address (once set), this happens to be equivalent to
12692 * simply resetting the index in vmcs02.
12693 */
12694 if (enable_pml)
12695 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12696
12697 /*
12698 * Interrupt/Exception Fields
12699 */
12700 if (vmx->nested.nested_run_pending) {
12701 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12702 vmcs12->vm_entry_intr_info_field);
12703 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12704 vmcs12->vm_entry_exception_error_code);
12705 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12706 vmcs12->vm_entry_instruction_len);
12707 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12708 vmcs12->guest_interruptibility_info);
12709 vmx->loaded_vmcs->nmi_known_unmasked =
12710 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012711 } else {
Sean Christopherson09abe322018-09-26 09:23:50 -070012712 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12713 }
12714}
12715
12716static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12717{
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012718 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070012719
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012720 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12721 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
12722 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012723 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012724 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12725 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12726 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12727 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12728 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12729 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12730 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012731 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012732 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12733 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12734 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12735 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12736 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12737 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12738 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12739 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12740 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012741 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12742 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12743 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12744 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12745 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012746 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12747 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012748 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12749 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12750 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12751 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12752 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12753 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12754 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12755 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12756 }
12757
12758 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12759 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
12760 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12761 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12762 vmcs12->guest_pending_dbg_exceptions);
12763 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12764 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12765
12766 /*
12767 * L1 may access the L2's PDPTR, so save them to construct
12768 * vmcs12
12769 */
12770 if (enable_ept) {
12771 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12772 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12773 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12774 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12775 }
12776 }
Sean Christopherson09abe322018-09-26 09:23:50 -070012777
12778 if (nested_cpu_has_xsaves(vmcs12))
12779 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12780
12781 /*
12782 * Whether page-faults are trapped is determined by a combination of
12783 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12784 * If enable_ept, L0 doesn't care about page faults and we should
12785 * set all of these to L1's desires. However, if !enable_ept, L0 does
12786 * care about (at least some) page faults, and because it is not easy
12787 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12788 * to exit on each and every L2 page fault. This is done by setting
12789 * MASK=MATCH=0 and (see below) EB.PF=1.
12790 * Note that below we don't need special code to set EB.PF beyond the
12791 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12792 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12793 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12794 */
12795 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12796 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12797 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12798 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12799
12800 if (cpu_has_vmx_apicv()) {
12801 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12802 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12803 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12804 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12805 }
12806
12807 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12808 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12809
12810 set_cr4_guest_host_mask(vmx);
12811
12812 if (kvm_mpx_supported()) {
12813 if (vmx->nested.nested_run_pending &&
12814 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12815 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12816 else
12817 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12818 }
Sean Christopherson09abe322018-09-26 09:23:50 -070012819}
12820
12821/*
12822 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12823 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12824 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12825 * guest in a way that will both be appropriate to L1's requests, and our
12826 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12827 * function also has additional necessary side-effects, like setting various
12828 * vcpu->arch fields.
12829 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12830 * is assigned to entry_failure_code on failure.
12831 */
12832static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12833 u32 *entry_failure_code)
12834{
12835 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012836 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
Sean Christopherson09abe322018-09-26 09:23:50 -070012837
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020012838 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
Sean Christopherson09abe322018-09-26 09:23:50 -070012839 prepare_vmcs02_full(vmx, vmcs12);
12840 vmx->nested.dirty_vmcs12 = false;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012841 }
12842
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012843 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012844 * First, the fields that are shadowed. This must be kept in sync
12845 * with vmx_shadow_fields.h.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012846 */
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012847 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
12848 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012849 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Vitaly Kuznetsovcbe3f892018-10-19 16:16:03 +020012850 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
Vitaly Kuznetsovc4ebd622018-10-16 18:50:04 +020012851 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012852
Sean Christopherson09abe322018-09-26 09:23:50 -070012853 if (vmx->nested.nested_run_pending &&
12854 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12855 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12856 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12857 } else {
12858 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12859 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12860 }
12861 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12862
12863 vmx->nested.preemption_timer_expired = false;
12864 if (nested_cpu_has_preemption_timer(vmcs12))
12865 vmx_start_preemption_timer(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012866
12867 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12868 * bitwise-or of what L1 wants to trap for L2, and what we want to
12869 * trap. Note that CR0.TS also needs updating - we do this later.
12870 */
12871 update_exception_bitmap(vcpu);
12872 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12873 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12874
Jim Mattson6514dc32018-04-26 16:09:12 -070012875 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012876 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012877 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012878 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012879 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012880 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012881 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012882
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012883 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12884
Peter Feinerc95ba922016-08-17 09:36:47 -070012885 if (kvm_has_tsc_control)
12886 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012887
12888 if (enable_vpid) {
12889 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012890 * There is no direct mapping between vpid02 and vpid12, the
12891 * vpid02 is per-vCPU for L0 and reused while the value of
12892 * vpid12 is changed w/ one invvpid during nested vmentry.
12893 * The vpid12 is allocated by L1 for L2, so it will not
12894 * influence global bitmap(for vpid01 and vpid02 allocation)
12895 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012896 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012897 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012898 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12899 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012900 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012901 }
12902 } else {
Liran Alon14389212018-10-08 23:42:17 +030012903 /*
12904 * If L1 use EPT, then L0 needs to execute INVEPT on
12905 * EPTP02 instead of EPTP01. Therefore, delay TLB
12906 * flush until vmcs02->eptp is fully updated by
12907 * KVM_REQ_LOAD_CR3. Note that this assumes
12908 * KVM_REQ_TLB_FLUSH is evaluated after
12909 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12910 */
12911 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012912 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012913 }
12914
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012915 if (nested_cpu_has_ept(vmcs12))
12916 nested_ept_init_mmu_context(vcpu);
12917 else if (nested_cpu_has2(vmcs12,
12918 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012919 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012920
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012921 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012922 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12923 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012924 * The CR0_READ_SHADOW is what L2 should have expected to read given
12925 * the specifications by L1; It's not enough to take
12926 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12927 * have more bits than L1 expected.
12928 */
12929 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12930 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12931
12932 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12933 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12934
Sean Christopherson09abe322018-09-26 09:23:50 -070012935 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012936 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012937 vmx_set_efer(vcpu, vcpu->arch.efer);
12938
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012939 /*
12940 * Guest state is invalid and unrestricted guest is disabled,
12941 * which means L1 attempted VMEntry to L2 with invalid state.
12942 * Fail the VMEntry.
12943 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012944 if (vmx->emulation_required) {
12945 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012946 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012947 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012948
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012949 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012950 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012951 entry_failure_code))
12952 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012953
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012954 if (!enable_ept)
12955 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12956
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012957 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12958 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012959 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012960}
12961
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012962static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12963{
12964 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12965 nested_cpu_has_virtual_nmis(vmcs12))
12966 return -EINVAL;
12967
12968 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12969 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12970 return -EINVAL;
12971
12972 return 0;
12973}
12974
Jim Mattsonca0bde22016-11-30 12:03:46 -080012975static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12976{
12977 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012978 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012979
12980 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12981 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12982 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12983
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012984 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12985 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12986
Jim Mattson56a20512017-07-06 16:33:06 -070012987 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12988 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12989
Jim Mattsonca0bde22016-11-30 12:03:46 -080012990 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12991 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12992
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012993 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12994 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12995
Jim Mattson712b12d2017-08-24 13:24:47 -070012996 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12997 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12998
Jim Mattsonca0bde22016-11-30 12:03:46 -080012999 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
13000 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13001
13002 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
13003 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13004
Bandan Dasc5f983f2017-05-05 15:25:14 -040013005 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
13006 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13007
Liran Alona8a7c022018-06-23 02:35:06 +030013008 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
13009 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13010
Jim Mattsonca0bde22016-11-30 12:03:46 -080013011 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013012 vmx->nested.msrs.procbased_ctls_low,
13013 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070013014 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
13015 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013016 vmx->nested.msrs.secondary_ctls_low,
13017 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013018 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013019 vmx->nested.msrs.pinbased_ctls_low,
13020 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013021 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013022 vmx->nested.msrs.exit_ctls_low,
13023 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080013024 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013025 vmx->nested.msrs.entry_ctls_low,
13026 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080013027 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13028
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050013029 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080013030 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13031
Bandan Das41ab9372017-08-03 15:54:43 -040013032 if (nested_cpu_has_vmfunc(vmcs12)) {
13033 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010013034 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040013035 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13036
13037 if (nested_cpu_has_eptp_switching(vmcs12)) {
13038 if (!nested_cpu_has_ept(vmcs12) ||
13039 !page_address_valid(vcpu, vmcs12->eptp_list_address))
13040 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13041 }
13042 }
Bandan Das27c42a12017-08-03 15:54:42 -040013043
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070013044 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
13045 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13046
Jim Mattsonca0bde22016-11-30 12:03:46 -080013047 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
13048 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
13049 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
13050 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
13051
Marc Orr04473782018-06-20 17:21:29 -070013052 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070013053 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
13054 * IA32_EFER MSR must be 0 in the field for that register. In addition,
13055 * the values of the LMA and LME bits in the field must each be that of
13056 * the host address-space size VM-exit control.
13057 */
13058 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
13059 ia32e = (vmcs12->vm_exit_controls &
13060 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
13061 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
13062 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
13063 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
13064 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
13065 }
13066
13067 /*
Marc Orr04473782018-06-20 17:21:29 -070013068 * From the Intel SDM, volume 3:
13069 * Fields relevant to VM-entry event injection must be set properly.
13070 * These fields are the VM-entry interruption-information field, the
13071 * VM-entry exception error code, and the VM-entry instruction length.
13072 */
13073 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
13074 u32 intr_info = vmcs12->vm_entry_intr_info_field;
13075 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
13076 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
13077 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
13078 bool should_have_error_code;
13079 bool urg = nested_cpu_has2(vmcs12,
13080 SECONDARY_EXEC_UNRESTRICTED_GUEST);
13081 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
13082
13083 /* VM-entry interruption-info field: interruption type */
13084 if (intr_type == INTR_TYPE_RESERVED ||
13085 (intr_type == INTR_TYPE_OTHER_EVENT &&
13086 !nested_cpu_supports_monitor_trap_flag(vcpu)))
13087 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13088
13089 /* VM-entry interruption-info field: vector */
13090 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
13091 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
13092 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
13093 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13094
13095 /* VM-entry interruption-info field: deliver error code */
13096 should_have_error_code =
13097 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
13098 x86_exception_has_error_code(vector);
13099 if (has_error_code != should_have_error_code)
13100 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13101
13102 /* VM-entry exception error code */
13103 if (has_error_code &&
13104 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
13105 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13106
13107 /* VM-entry interruption-info field: reserved bits */
13108 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
13109 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13110
13111 /* VM-entry instruction length */
13112 switch (intr_type) {
13113 case INTR_TYPE_SOFT_EXCEPTION:
13114 case INTR_TYPE_SOFT_INTR:
13115 case INTR_TYPE_PRIV_SW_EXCEPTION:
13116 if ((vmcs12->vm_entry_instruction_len > 15) ||
13117 (vmcs12->vm_entry_instruction_len == 0 &&
13118 !nested_cpu_has_zero_length_injection(vcpu)))
13119 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13120 }
13121 }
13122
Sean Christopherson5b8ba412018-09-26 09:23:40 -070013123 if (nested_cpu_has_ept(vmcs12) &&
13124 !valid_ept_address(vcpu, vmcs12->ept_pointer))
13125 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
13126
Jim Mattsonca0bde22016-11-30 12:03:46 -080013127 return 0;
13128}
13129
Liran Alonf145d902018-06-23 02:35:07 +030013130static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
13131 struct vmcs12 *vmcs12)
13132{
13133 int r;
13134 struct page *page;
13135 struct vmcs12 *shadow;
13136
13137 if (vmcs12->vmcs_link_pointer == -1ull)
13138 return 0;
13139
13140 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
13141 return -EINVAL;
13142
13143 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
13144 if (is_error_page(page))
13145 return -EINVAL;
13146
13147 r = 0;
13148 shadow = kmap(page);
13149 if (shadow->hdr.revision_id != VMCS12_REVISION ||
13150 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
13151 r = -EINVAL;
13152 kunmap(page);
13153 kvm_release_page_clean(page);
13154 return r;
13155}
13156
Jim Mattsonca0bde22016-11-30 12:03:46 -080013157static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13158 u32 *exit_qual)
13159{
13160 bool ia32e;
13161
13162 *exit_qual = ENTRY_FAIL_DEFAULT;
13163
13164 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
13165 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
13166 return 1;
13167
Liran Alonf145d902018-06-23 02:35:07 +030013168 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080013169 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
13170 return 1;
13171 }
13172
13173 /*
13174 * If the load IA32_EFER VM-entry control is 1, the following checks
13175 * are performed on the field for the IA32_EFER MSR:
13176 * - Bits reserved in the IA32_EFER MSR must be 0.
13177 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
13178 * the IA-32e mode guest VM-exit control. It must also be identical
13179 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
13180 * CR0.PG) is 1.
13181 */
13182 if (to_vmx(vcpu)->nested.nested_run_pending &&
13183 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
13184 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
13185 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
13186 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
13187 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
13188 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
13189 return 1;
13190 }
13191
Wanpeng Lif1b026a2017-11-05 16:54:48 -080013192 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
13193 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
13194 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
13195 return 1;
13196
Jim Mattsonca0bde22016-11-30 12:03:46 -080013197 return 0;
13198}
13199
Sean Christopherson52017602018-09-26 09:23:57 -070013200static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
13201{
13202 struct vcpu_vmx *vmx = to_vmx(vcpu);
13203 unsigned long cr3, cr4;
13204
13205 if (!nested_early_check)
13206 return 0;
13207
13208 if (vmx->msr_autoload.host.nr)
13209 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
13210 if (vmx->msr_autoload.guest.nr)
13211 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
13212
13213 preempt_disable();
13214
13215 vmx_prepare_switch_to_guest(vcpu);
13216
13217 /*
13218 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
13219 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
13220 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
13221 * there is no need to preserve other bits or save/restore the field.
13222 */
13223 vmcs_writel(GUEST_RFLAGS, 0);
13224
13225 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
13226
13227 cr3 = __get_current_cr3_fast();
13228 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
13229 vmcs_writel(HOST_CR3, cr3);
13230 vmx->loaded_vmcs->host_state.cr3 = cr3;
13231 }
13232
13233 cr4 = cr4_read_shadow();
13234 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
13235 vmcs_writel(HOST_CR4, cr4);
13236 vmx->loaded_vmcs->host_state.cr4 = cr4;
13237 }
13238
13239 vmx->__launched = vmx->loaded_vmcs->launched;
13240
13241 asm(
13242 /* Set HOST_RSP */
Uros Bizjak4b1e5472018-10-11 19:40:44 +020013243 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013244 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
13245
13246 /* Check if vmlaunch of vmresume is needed */
13247 "cmpl $0, %c[launched](%0)\n\t"
13248 "je 1f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020013249 __ex("vmresume") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013250 "jmp 2f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020013251 "1: " __ex("vmlaunch") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070013252 "jmp 2f\n\t"
13253 "2: "
13254
13255 /* Set vmx->fail accordingly */
13256 "setbe %c[fail](%0)\n\t"
13257
13258 ".pushsection .rodata\n\t"
13259 ".global vmx_early_consistency_check_return\n\t"
13260 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
13261 ".popsection"
13262 :
13263 : "c"(vmx), "d"((unsigned long)HOST_RSP),
13264 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
13265 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
13266 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
13267 : "rax", "cc", "memory"
13268 );
13269
13270 vmcs_writel(HOST_RIP, vmx_return);
13271
13272 preempt_enable();
13273
13274 if (vmx->msr_autoload.host.nr)
13275 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13276 if (vmx->msr_autoload.guest.nr)
13277 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
13278
13279 if (vmx->fail) {
13280 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
13281 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13282 vmx->fail = 0;
13283 return 1;
13284 }
13285
13286 /*
13287 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
13288 */
13289 local_irq_enable();
13290 if (hw_breakpoint_active())
13291 set_debugreg(__this_cpu_read(cpu_dr7), 7);
13292
13293 /*
13294 * A non-failing VMEntry means we somehow entered guest mode with
13295 * an illegal RIP, and that's just the tip of the iceberg. There
13296 * is no telling what memory has been modified or what state has
13297 * been exposed to unknown code. Hitting this all but guarantees
13298 * a (very critical) hardware issue.
13299 */
13300 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
13301 VMX_EXIT_REASONS_FAILED_VMENTRY));
13302
13303 return 0;
13304}
13305STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
13306
Sean Christophersona633e412018-09-26 09:23:47 -070013307static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13308 struct vmcs12 *vmcs12);
13309
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013310/*
Sean Christophersona633e412018-09-26 09:23:47 -070013311 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013312 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070013313+ *
13314+ * Returns:
13315+ * 0 - success, i.e. proceed with actual VMEnter
13316+ * 1 - consistency check VMExit
13317+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013318 */
Sean Christophersona633e412018-09-26 09:23:47 -070013319static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
13320 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080013321{
13322 struct vcpu_vmx *vmx = to_vmx(vcpu);
13323 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020013324 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070013325 u32 exit_reason = EXIT_REASON_INVALID_STATE;
13326 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080013327
Paolo Bonzini7e712682018-10-03 13:44:26 +020013328 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
13329 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
13330 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
13331 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030013332
Jim Mattson858e25c2016-11-30 12:03:47 -080013333 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
13334 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030013335 if (kvm_mpx_supported() &&
13336 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
13337 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080013338
Jim Mattsonde3a0022017-11-27 17:22:25 -060013339 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080013340
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013341 prepare_vmcs02_early(vmx, vmcs12);
Jim Mattson858e25c2016-11-30 12:03:47 -080013342
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013343 if (from_vmentry) {
13344 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080013345
Sean Christopherson52017602018-09-26 09:23:57 -070013346 if (nested_vmx_check_vmentry_hw(vcpu)) {
13347 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
13348 return -1;
13349 }
13350
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013351 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13352 goto vmentry_fail_vmexit;
13353 }
13354
13355 enter_guest_mode(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080013356 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13357 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
13358
Sean Christophersona633e412018-09-26 09:23:47 -070013359 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070013360 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080013361
13362 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070013363 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
13364 exit_qual = nested_vmx_load_msr(vcpu,
13365 vmcs12->vm_entry_msr_load_addr,
13366 vmcs12->vm_entry_msr_load_count);
13367 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070013368 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013369 } else {
13370 /*
13371 * The MMU is not initialized to point at the right entities yet and
13372 * "get pages" would need to read data from the guest (i.e. we will
13373 * need to perform gpa to hpa translation). Request a call
13374 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
13375 * have already been set at vmentry time and should not be reset.
13376 */
13377 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
13378 }
Jim Mattson858e25c2016-11-30 12:03:47 -080013379
Jim Mattson858e25c2016-11-30 12:03:47 -080013380 /*
Liran Alonb5861e52018-09-03 15:20:22 +030013381 * If L1 had a pending IRQ/NMI until it executed
13382 * VMLAUNCH/VMRESUME which wasn't delivered because it was
13383 * disallowed (e.g. interrupts disabled), L0 needs to
13384 * evaluate if this pending event should cause an exit from L2
13385 * to L1 or delivered directly to L2 (e.g. In case L1 don't
13386 * intercept EXTERNAL_INTERRUPT).
13387 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020013388 * Usually this would be handled by the processor noticing an
13389 * IRQ/NMI window request, or checking RVI during evaluation of
13390 * pending virtual interrupts. However, this setting was done
13391 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
13392 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030013393 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020013394 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030013395 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030013396
13397 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080013398 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
13399 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
13400 * returned as far as L1 is concerned. It will only return (and set
13401 * the success flag) when L2 exits (see nested_vmx_vmexit()).
13402 */
13403 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013404
Sean Christophersona633e412018-09-26 09:23:47 -070013405 /*
13406 * A failed consistency check that leads to a VMExit during L1's
13407 * VMEnter to L2 is a variation of a normal VMexit, as explained in
13408 * 26.7 "VM-entry failures during or after loading guest state".
13409 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070013410vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013411 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13412 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13413 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070013414
13415vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013416 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070013417
13418 if (!from_vmentry)
13419 return 1;
13420
Sean Christophersona633e412018-09-26 09:23:47 -070013421 load_vmcs12_host_state(vcpu, vmcs12);
13422 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13423 vmcs12->exit_qualification = exit_qual;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013424 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
13425 vmx->nested.need_vmcs12_sync = true;
Sean Christophersona633e412018-09-26 09:23:47 -070013426 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080013427}
13428
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013429/*
13430 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
13431 * for running an L2 nested guest.
13432 */
13433static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
13434{
13435 struct vmcs12 *vmcs12;
13436 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070013437 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080013438 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013439
Kyle Hueyeb277562016-11-29 12:40:39 -080013440 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013441 return 1;
13442
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020013443 if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
Vitaly Kuznetsovb8bbab92018-10-16 18:50:03 +020013444 return 1;
13445
13446 if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013447 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080013448
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013449 vmcs12 = get_vmcs12(vcpu);
13450
Liran Alona6192d42018-06-23 02:35:04 +030013451 /*
13452 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
13453 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
13454 * rather than RFLAGS.ZF, and no error number is stored to the
13455 * VM-instruction error field.
13456 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013457 if (vmcs12->hdr.shadow_vmcs)
13458 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030013459
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013460 if (vmx->nested.hv_evmcs) {
13461 copy_enlightened_to_vmcs12(vmx);
13462 /* Enlightened VMCS doesn't have launch state */
13463 vmcs12->launch_state = !launch;
13464 } else if (enable_shadow_vmcs) {
Abel Gordon012f83c2013-04-18 14:39:25 +030013465 copy_shadow_to_vmcs12(vmx);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020013466 }
Abel Gordon012f83c2013-04-18 14:39:25 +030013467
Nadav Har'El7c177932011-05-25 23:12:04 +030013468 /*
13469 * The nested entry process starts with enforcing various prerequisites
13470 * on vmcs12 as required by the Intel SDM, and act appropriately when
13471 * they fail: As the SDM explains, some conditions should cause the
13472 * instruction to fail, while others will cause the instruction to seem
13473 * to succeed, but return an EXIT_REASON_INVALID_STATE.
13474 * To speed up the normal (success) code path, we should avoid checking
13475 * for misconfigurations which will anyway be caught by the processor
13476 * when using the merged vmcs02.
13477 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013478 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
13479 return nested_vmx_failValid(vcpu,
13480 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070013481
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013482 if (vmcs12->launch_state == launch)
13483 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030013484 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
13485 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030013486
Jim Mattsonca0bde22016-11-30 12:03:46 -080013487 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013488 if (ret)
13489 return nested_vmx_failValid(vcpu, ret);
Jan Kiszka384bb782013-04-20 10:52:36 +020013490
13491 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030013492 * We're finally done with prerequisite checking, and can start with
13493 * the nested entry.
13494 */
Jim Mattson6514dc32018-04-26 16:09:12 -070013495 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070013496 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070013497 vmx->nested.nested_run_pending = !ret;
13498 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013499 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070013500 else if (ret)
13501 return nested_vmx_failValid(vcpu,
13502 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030013503
Paolo Bonzinic595cee2018-07-02 13:07:14 +020013504 /* Hide L1D cache contents from the nested guest. */
13505 vmx->vcpu.arch.l1tf_flush_l1d = true;
13506
Chao Gao135a06c2018-02-11 10:06:30 +080013507 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070013508 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030013509 * also be used as part of restoring nVMX state for
13510 * snapshot restore (migration).
13511 *
13512 * In this flow, it is assumed that vmcs12 cache was
13513 * trasferred as part of captured nVMX state and should
13514 * therefore not be read from guest memory (which may not
13515 * exist on destination host yet).
13516 */
13517 nested_cache_shadow_vmcs12(vcpu, vmcs12);
13518
13519 /*
Chao Gao135a06c2018-02-11 10:06:30 +080013520 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
13521 * by event injection, halt vcpu.
13522 */
13523 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070013524 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
13525 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060013526 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070013527 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030013528 return 1;
13529}
13530
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013531/*
13532 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
13533 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
13534 * This function returns the new value we should put in vmcs12.guest_cr0.
13535 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
13536 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
13537 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
13538 * didn't trap the bit, because if L1 did, so would L0).
13539 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
13540 * been modified by L2, and L1 knows it. So just leave the old value of
13541 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
13542 * isn't relevant, because if L0 traps this bit it can set it to anything.
13543 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
13544 * changed these bits, and therefore they need to be updated, but L0
13545 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
13546 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
13547 */
13548static inline unsigned long
13549vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13550{
13551 return
13552 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
13553 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
13554 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
13555 vcpu->arch.cr0_guest_owned_bits));
13556}
13557
13558static inline unsigned long
13559vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13560{
13561 return
13562 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
13563 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
13564 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13565 vcpu->arch.cr4_guest_owned_bits));
13566}
13567
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013568static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13569 struct vmcs12 *vmcs12)
13570{
13571 u32 idt_vectoring;
13572 unsigned int nr;
13573
Wanpeng Li664f8e22017-08-24 03:35:09 -070013574 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013575 nr = vcpu->arch.exception.nr;
13576 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13577
13578 if (kvm_exception_is_soft(nr)) {
13579 vmcs12->vm_exit_instruction_len =
13580 vcpu->arch.event_exit_inst_len;
13581 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13582 } else
13583 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13584
13585 if (vcpu->arch.exception.has_error_code) {
13586 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13587 vmcs12->idt_vectoring_error_code =
13588 vcpu->arch.exception.error_code;
13589 }
13590
13591 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010013592 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013593 vmcs12->idt_vectoring_info_field =
13594 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030013595 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013596 nr = vcpu->arch.interrupt.nr;
13597 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13598
13599 if (vcpu->arch.interrupt.soft) {
13600 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13601 vmcs12->vm_entry_instruction_len =
13602 vcpu->arch.event_exit_inst_len;
13603 } else
13604 idt_vectoring |= INTR_TYPE_EXT_INTR;
13605
13606 vmcs12->idt_vectoring_info_field = idt_vectoring;
13607 }
13608}
13609
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013610static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
13611{
13612 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013613 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020013614 bool block_nested_events =
13615 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080013616
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013617 if (vcpu->arch.exception.pending &&
13618 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020013619 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013620 return -EBUSY;
13621 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013622 return 0;
13623 }
13624
Jan Kiszkaf41245002014-03-07 20:03:13 +010013625 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13626 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013627 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010013628 return -EBUSY;
13629 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13630 return 0;
13631 }
13632
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013633 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013634 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013635 return -EBUSY;
13636 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13637 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13638 INTR_INFO_VALID_MASK, 0);
13639 /*
13640 * The NMI-triggered VM exit counts as injection:
13641 * clear this one and block further NMIs.
13642 */
13643 vcpu->arch.nmi_pending = 0;
13644 vmx_set_nmi_mask(vcpu, true);
13645 return 0;
13646 }
13647
13648 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13649 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013650 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013651 return -EBUSY;
13652 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013653 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013654 }
13655
David Hildenbrand6342c502017-01-25 11:58:58 +010013656 vmx_complete_nested_posted_interrupt(vcpu);
13657 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013658}
13659
Sean Christophersond264ee02018-08-27 15:21:12 -070013660static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13661{
13662 to_vmx(vcpu)->req_immediate_exit = true;
13663}
13664
Jan Kiszkaf41245002014-03-07 20:03:13 +010013665static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13666{
13667 ktime_t remaining =
13668 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13669 u64 value;
13670
13671 if (ktime_to_ns(remaining) <= 0)
13672 return 0;
13673
13674 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13675 do_div(value, 1000000);
13676 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13677}
13678
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013679/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013680 * Update the guest state fields of vmcs12 to reflect changes that
13681 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13682 * VM-entry controls is also updated, since this is really a guest
13683 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013684 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013685static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013686{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013687 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13688 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13689
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013690 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13691 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13692 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13693
13694 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13695 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13696 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13697 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13698 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13699 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13700 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13701 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13702 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13703 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13704 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13705 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13706 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13707 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13708 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13709 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13710 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13711 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13712 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13713 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13714 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13715 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13716 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13717 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13718 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13719 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13720 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13721 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13722 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13723 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13724 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13725 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13726 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13727 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13728 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13729 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13730
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013731 vmcs12->guest_interruptibility_info =
13732 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13733 vmcs12->guest_pending_dbg_exceptions =
13734 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013735 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13736 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13737 else
13738 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013739
Jan Kiszkaf41245002014-03-07 20:03:13 +010013740 if (nested_cpu_has_preemption_timer(vmcs12)) {
13741 if (vmcs12->vm_exit_controls &
13742 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13743 vmcs12->vmx_preemption_timer_value =
13744 vmx_get_preemption_timer_value(vcpu);
13745 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13746 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013747
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013748 /*
13749 * In some cases (usually, nested EPT), L2 is allowed to change its
13750 * own CR3 without exiting. If it has changed it, we must keep it.
13751 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13752 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13753 *
13754 * Additionally, restore L2's PDPTR to vmcs12.
13755 */
13756 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013757 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013758 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13759 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13760 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13761 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13762 }
13763
Jim Mattsond281e132017-06-01 12:44:46 -070013764 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013765
Wincy Van608406e2015-02-03 23:57:51 +080013766 if (nested_cpu_has_vid(vmcs12))
13767 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13768
Jan Kiszkac18911a2013-03-13 16:06:41 +010013769 vmcs12->vm_entry_controls =
13770 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013771 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013772
Jan Kiszka2996fca2014-06-16 13:59:43 +020013773 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13774 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13775 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13776 }
13777
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013778 /* TODO: These cannot have changed unless we have MSR bitmaps and
13779 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013780 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013781 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013782 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13783 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013784 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13785 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13786 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013787 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013788 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013789}
13790
13791/*
13792 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13793 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13794 * and this function updates it to reflect the changes to the guest state while
13795 * L2 was running (and perhaps made some exits which were handled directly by L0
13796 * without going back to L1), and to reflect the exit reason.
13797 * Note that we do not have to copy here all VMCS fields, just those that
13798 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13799 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13800 * which already writes to vmcs12 directly.
13801 */
13802static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13803 u32 exit_reason, u32 exit_intr_info,
13804 unsigned long exit_qualification)
13805{
13806 /* update guest state fields: */
13807 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013808
13809 /* update exit information fields: */
13810
Jan Kiszka533558b2014-01-04 18:47:20 +010013811 vmcs12->vm_exit_reason = exit_reason;
13812 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013813 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013814
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013815 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013816 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13817 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13818
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013819 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013820 vmcs12->launch_state = 1;
13821
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013822 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13823 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013824 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013825
13826 /*
13827 * Transfer the event that L0 or L1 may wanted to inject into
13828 * L2 to IDT_VECTORING_INFO_FIELD.
13829 */
13830 vmcs12_save_pending_event(vcpu, vmcs12);
13831 }
13832
13833 /*
13834 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13835 * preserved above and would only end up incorrectly in L1.
13836 */
13837 vcpu->arch.nmi_injected = false;
13838 kvm_clear_exception_queue(vcpu);
13839 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013840}
13841
13842/*
13843 * A part of what we need to when the nested L2 guest exits and we want to
13844 * run its L1 parent, is to reset L1's guest state to the host state specified
13845 * in vmcs12.
13846 * This function is to be called not only on normal nested exit, but also on
13847 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13848 * Failures During or After Loading Guest State").
13849 * This function should be called when the active VMCS is L1's (vmcs01).
13850 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013851static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13852 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013853{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013854 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013855 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013856
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013857 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13858 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013859 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013860 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13861 else
13862 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13863 vmx_set_efer(vcpu, vcpu->arch.efer);
13864
13865 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13866 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013867 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013868 vmx_set_interrupt_shadow(vcpu, 0);
13869
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013870 /*
13871 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013872 * actually changed, because vmx_set_cr0 refers to efer set above.
13873 *
13874 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13875 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013876 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013877 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020013878 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013879
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013880 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013881 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013882 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013883
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013884 nested_ept_uninit_mmu_context(vcpu);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013885
Liran Alon6f1e03b2018-05-22 17:16:14 +030013886 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013887 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13888 * couldn't have changed.
13889 */
13890 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13891 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13892
13893 if (!enable_ept)
13894 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Jan Kiszka44811c02013-08-04 17:17:27 +020013895
Liran Alon6f1e03b2018-05-22 17:16:14 +030013896 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013897 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013898 * VMEntry/VMExit. Thus, no need to flush TLB.
13899 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013900 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13901 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013902 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013903 * Otherwise, we can preserve TLB entries as long as we are
13904 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013905 *
13906 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13907 * and therefore we request the TLB flush to happen only after VMCS EPTP
13908 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013909 */
13910 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013911 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013912 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013913 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013914
13915 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13916 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13917 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13918 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013919 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013920 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13921 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013922
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013923 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13924 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13925 vmcs_write64(GUEST_BNDCFGS, 0);
13926
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013927 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
13928 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
13929 vcpu->arch.pat = vmcs12->host_ia32_pat;
13930 }
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013931 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013932 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13933 vmcs12->host_ia32_perf_global_ctrl);
13934
13935 /* Set L1 segment info according to Intel SDM
13936 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13937 seg = (struct kvm_segment) {
13938 .base = 0,
13939 .limit = 0xFFFFFFFF,
13940 .selector = vmcs12->host_cs_selector,
13941 .type = 11,
13942 .present = 1,
13943 .s = 1,
13944 .g = 1
13945 };
13946 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13947 seg.l = 1;
13948 else
13949 seg.db = 1;
13950 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13951 seg = (struct kvm_segment) {
13952 .base = 0,
13953 .limit = 0xFFFFFFFF,
13954 .type = 3,
13955 .present = 1,
13956 .s = 1,
13957 .db = 1,
13958 .g = 1
13959 };
13960 seg.selector = vmcs12->host_ds_selector;
13961 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13962 seg.selector = vmcs12->host_es_selector;
13963 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13964 seg.selector = vmcs12->host_ss_selector;
13965 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13966 seg.selector = vmcs12->host_fs_selector;
13967 seg.base = vmcs12->host_fs_base;
13968 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13969 seg.selector = vmcs12->host_gs_selector;
Gleb Natapov205befd2013-08-04 15:08:06 +030013970 seg.base = vmcs12->host_gs_base;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013971 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13972 seg = (struct kvm_segment) {
13973 .base = vmcs12->host_tr_base,
13974 .limit = 0x67,
13975 .selector = vmcs12->host_tr_selector,
13976 .type = 11,
13977 .present = 1
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013978 };
13979 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013980
13981 kvm_set_dr(vcpu, 7, 0x400);
13982 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013983
Wincy Van3af18d92015-02-03 23:49:31 +080013984 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013985 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013986
Wincy Vanff651cb2014-12-11 08:52:58 +030013987 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13988 vmcs12->vm_exit_msr_load_count))
13989 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013990}
13991
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013992static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13993{
13994 struct shared_msr_entry *efer_msr;
13995 unsigned int i;
13996
13997 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13998 return vmcs_read64(GUEST_IA32_EFER);
13999
14000 if (cpu_has_load_ia32_efer)
14001 return host_efer;
14002
14003 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
14004 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
14005 return vmx->msr_autoload.guest.val[i].value;
14006 }
14007
14008 efer_msr = find_msr_entry(vmx, MSR_EFER);
14009 if (efer_msr)
14010 return efer_msr->data;
14011
14012 return host_efer;
14013}
14014
14015static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
14016{
14017 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14018 struct vcpu_vmx *vmx = to_vmx(vcpu);
14019 struct vmx_msr_entry g, h;
14020 struct msr_data msr;
14021 gpa_t gpa;
14022 u32 i, j;
14023
14024 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
14025
14026 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
14027 /*
14028 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
14029 * as vmcs01.GUEST_DR7 contains a userspace defined value
14030 * and vcpu->arch.dr7 is not squirreled away before the
14031 * nested VMENTER (not worth adding a variable in nested_vmx).
14032 */
14033 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
14034 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
14035 else
14036 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
14037 }
14038
14039 /*
14040 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
14041 * handle a variety of side effects to KVM's software model.
14042 */
14043 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
14044
14045 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
14046 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
14047
14048 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
14049 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
14050
14051 nested_ept_uninit_mmu_context(vcpu);
14052 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
14053 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
14054
14055 /*
14056 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
14057 * from vmcs01 (if necessary). The PDPTRs are not loaded on
14058 * VMFail, like everything else we just need to ensure our
14059 * software model is up-to-date.
14060 */
14061 ept_save_pdptrs(vcpu);
14062
14063 kvm_mmu_reset_context(vcpu);
14064
14065 if (cpu_has_vmx_msr_bitmap())
14066 vmx_update_msr_bitmap(vcpu);
14067
14068 /*
14069 * This nasty bit of open coding is a compromise between blindly
14070 * loading L1's MSRs using the exit load lists (incorrect emulation
14071 * of VMFail), leaving the nested VM's MSRs in the software model
14072 * (incorrect behavior) and snapshotting the modified MSRs (too
14073 * expensive since the lists are unbound by hardware). For each
14074 * MSR that was (prematurely) loaded from the nested VMEntry load
14075 * list, reload it from the exit load list if it exists and differs
14076 * from the guest value. The intent is to stuff host state as
14077 * silently as possible, not to fully process the exit load list.
14078 */
14079 msr.host_initiated = false;
14080 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
14081 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
14082 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
14083 pr_debug_ratelimited(
14084 "%s read MSR index failed (%u, 0x%08llx)\n",
14085 __func__, i, gpa);
14086 goto vmabort;
14087 }
14088
14089 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
14090 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
14091 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
14092 pr_debug_ratelimited(
14093 "%s read MSR failed (%u, 0x%08llx)\n",
14094 __func__, j, gpa);
14095 goto vmabort;
14096 }
14097 if (h.index != g.index)
14098 continue;
14099 if (h.value == g.value)
14100 break;
14101
14102 if (nested_vmx_load_msr_check(vcpu, &h)) {
14103 pr_debug_ratelimited(
14104 "%s check failed (%u, 0x%x, 0x%x)\n",
14105 __func__, j, h.index, h.reserved);
14106 goto vmabort;
14107 }
14108
14109 msr.index = h.index;
14110 msr.data = h.value;
14111 if (kvm_set_msr(vcpu, &msr)) {
14112 pr_debug_ratelimited(
14113 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
14114 __func__, j, h.index, h.value);
14115 goto vmabort;
14116 }
14117 }
14118 }
14119
14120 return;
14121
14122vmabort:
14123 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
14124}
14125
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014126/*
14127 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
14128 * and modify vmcs12 to make it see what it would expect to see there if
14129 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
14130 */
Jan Kiszka533558b2014-01-04 18:47:20 +010014131static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
14132 u32 exit_intr_info,
14133 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014134{
14135 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014136 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14137
Jan Kiszka5f3d5792013-04-14 12:12:46 +020014138 /* trying to cancel vmlaunch/vmresume is a bug */
14139 WARN_ON_ONCE(vmx->nested.nested_run_pending);
14140
Jim Mattson4f350c62017-09-14 16:31:44 -070014141 leave_guest_mode(vcpu);
14142
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014143 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
14144 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
14145
Jim Mattson4f350c62017-09-14 16:31:44 -070014146 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014147 if (exit_reason == -1)
14148 sync_vmcs12(vcpu, vmcs12);
14149 else
14150 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
14151 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070014152
Liran Alon61ada742018-06-23 02:35:08 +030014153 /*
14154 * Must happen outside of sync_vmcs12() as it will
14155 * also be used to capture vmcs12 cache as part of
14156 * capturing nVMX state for snapshot (migration).
14157 *
14158 * Otherwise, this flush will dirty guest memory at a
14159 * point it is already assumed by user-space to be
14160 * immutable.
14161 */
14162 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
14163
Jim Mattson4f350c62017-09-14 16:31:44 -070014164 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
14165 vmcs12->vm_exit_msr_store_count))
14166 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070014167 } else {
14168 /*
14169 * The only expected VM-instruction error is "VM entry with
14170 * invalid control field(s)." Anything else indicates a
14171 * problem with L0. And we should never get here with a
14172 * VMFail of any type if early consistency checks are enabled.
14173 */
14174 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
14175 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
14176 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040014177 }
14178
Jim Mattson4f350c62017-09-14 16:31:44 -070014179 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010014180
Paolo Bonzini9314006db2016-07-06 13:23:51 +020014181 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040014182 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
14183 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010014184 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070014185
Peter Feinerc95ba922016-08-17 09:36:47 -070014186 if (kvm_has_tsc_control)
14187 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014188
Jim Mattson8d860bb2018-05-09 16:56:05 -040014189 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
14190 vmx->nested.change_vmcs01_virtual_apic_mode = false;
14191 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070014192 } else if (!nested_cpu_has_ept(vmcs12) &&
14193 nested_cpu_has2(vmcs12,
14194 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070014195 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020014196 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014197
14198 /* This is needed for same reason as it was needed in prepare_vmcs02 */
14199 vmx->host_rsp = 0;
14200
14201 /* Unpin physical memory we referred to in vmcs02 */
14202 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020014203 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014204 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014205 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080014206 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020014207 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014208 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080014209 }
Wincy Van705699a2015-02-03 23:58:17 +080014210 if (vmx->nested.pi_desc_page) {
14211 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020014212 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080014213 vmx->nested.pi_desc_page = NULL;
14214 vmx->nested.pi_desc = NULL;
14215 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014216
14217 /*
Tang Chen38b99172014-09-24 15:57:54 +080014218 * We are now running in L2, mmu_notifier will force to reload the
14219 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
14220 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080014221 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080014222
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014223 if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
14224 vmx->nested.need_vmcs12_sync = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014225
14226 /* in case we halted in L2 */
14227 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070014228
14229 if (likely(!vmx->fail)) {
14230 /*
14231 * TODO: SDM says that with acknowledge interrupt on
14232 * exit, bit 31 of the VM-exit interrupt information
14233 * (valid interrupt) is always set to 1 on
14234 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
14235 * need kvm_cpu_has_interrupt(). See the commit
14236 * message for details.
14237 */
14238 if (nested_exit_intr_ack_set(vcpu) &&
14239 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
14240 kvm_cpu_has_interrupt(vcpu)) {
14241 int irq = kvm_cpu_get_interrupt(vcpu);
14242 WARN_ON(irq < 0);
14243 vmcs12->vm_exit_intr_info = irq |
14244 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
14245 }
14246
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014247 if (exit_reason != -1)
14248 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
14249 vmcs12->exit_qualification,
14250 vmcs12->idt_vectoring_info_field,
14251 vmcs12->vm_exit_intr_info,
14252 vmcs12->vm_exit_intr_error_code,
14253 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070014254
14255 load_vmcs12_host_state(vcpu, vmcs12);
14256
14257 return;
14258 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070014259
Jim Mattson4f350c62017-09-14 16:31:44 -070014260 /*
14261 * After an early L2 VM-entry failure, we're now back
14262 * in L1 which thinks it just finished a VMLAUNCH or
14263 * VMRESUME instruction, so we need to set the failure
14264 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070014265 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070014266 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070014267 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080014268
Jim Mattson4f350c62017-09-14 16:31:44 -070014269 /*
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014270 * Restore L1's host state to KVM's software model. We're here
14271 * because a consistency check was caught by hardware, which
14272 * means some amount of guest state has been propagated to KVM's
14273 * model and needs to be unwound to the host's state.
Jim Mattson4f350c62017-09-14 16:31:44 -070014274 */
Sean Christophersonbd18bff2018-08-22 14:57:07 -070014275 nested_vmx_restore_host_state(vcpu);
Jim Mattson4f350c62017-09-14 16:31:44 -070014276
Jim Mattson4f350c62017-09-14 16:31:44 -070014277 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030014278}
14279
Nadav Har'El7c177932011-05-25 23:12:04 +030014280/*
Jan Kiszka42124922014-01-04 18:47:19 +010014281 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
14282 */
14283static void vmx_leave_nested(struct kvm_vcpu *vcpu)
14284{
Wanpeng Li2f707d92017-03-06 04:03:28 -080014285 if (is_guest_mode(vcpu)) {
14286 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010014287 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080014288 }
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020014289 free_nested(vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +030014290}
14291
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014292static int vmx_check_intercept(struct kvm_vcpu *vcpu,
14293 struct x86_instruction_info *info,
14294 enum x86_intercept_stage stage)
14295{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020014296 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
14297 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
14298
14299 /*
14300 * RDPID causes #UD if disabled through secondary execution controls.
14301 * Because it is marked as EmulateOnUD, we need to intercept it here.
14302 */
14303 if (info->intercept == x86_intercept_rdtscp &&
14304 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
14305 ctxt->exception.vector = UD_VECTOR;
14306 ctxt->exception.error_code_valid = false;
14307 return X86EMUL_PROPAGATE_FAULT;
14308 }
14309
14310 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014311 return X86EMUL_CONTINUE;
14312}
14313
Yunhong Jiang64672c92016-06-13 14:19:59 -070014314#ifdef CONFIG_X86_64
14315/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
14316static inline int u64_shl_div_u64(u64 a, unsigned int shift,
14317 u64 divisor, u64 *result)
14318{
14319 u64 low = a << shift, high = a >> (64 - shift);
14320
14321 /* To avoid the overflow on divq */
14322 if (high >= divisor)
14323 return 1;
14324
14325 /* Low hold the result, high hold rem which is discarded */
14326 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
14327 "rm" (divisor), "0" (low), "1" (high));
14328 *result = low;
14329
14330 return 0;
14331}
14332
14333static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
14334{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020014335 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080014336 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020014337
14338 if (kvm_mwait_in_guest(vcpu->kvm))
14339 return -EOPNOTSUPP;
14340
14341 vmx = to_vmx(vcpu);
14342 tscl = rdtsc();
14343 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
14344 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080014345 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
14346
14347 if (delta_tsc > lapic_timer_advance_cycles)
14348 delta_tsc -= lapic_timer_advance_cycles;
14349 else
14350 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014351
14352 /* Convert to host delta tsc if tsc scaling is enabled */
14353 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
14354 u64_shl_div_u64(delta_tsc,
14355 kvm_tsc_scaling_ratio_frac_bits,
14356 vcpu->arch.tsc_scaling_ratio,
14357 &delta_tsc))
14358 return -ERANGE;
14359
14360 /*
14361 * If the delta tsc can't fit in the 32 bit after the multi shift,
14362 * we can't use the preemption timer.
14363 * It's possible that it fits on later vmentries, but checking
14364 * on every vmentry is costly so we just use an hrtimer.
14365 */
14366 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
14367 return -ERANGE;
14368
14369 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070014370 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014371}
14372
14373static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
14374{
Sean Christophersonf459a702018-08-27 15:21:11 -070014375 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070014376}
14377#endif
14378
Paolo Bonzini48d89b92014-08-26 13:27:46 +020014379static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014380{
Wanpeng Lib31c1142018-03-12 04:53:04 -070014381 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020014382 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014383}
14384
Kai Huang843e4332015-01-28 10:54:28 +080014385static void vmx_slot_enable_log_dirty(struct kvm *kvm,
14386 struct kvm_memory_slot *slot)
14387{
14388 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
14389 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
14390}
14391
14392static void vmx_slot_disable_log_dirty(struct kvm *kvm,
14393 struct kvm_memory_slot *slot)
14394{
14395 kvm_mmu_slot_set_dirty(kvm, slot);
14396}
14397
14398static void vmx_flush_log_dirty(struct kvm *kvm)
14399{
14400 kvm_flush_pml_buffers(kvm);
14401}
14402
Bandan Dasc5f983f2017-05-05 15:25:14 -040014403static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
14404{
14405 struct vmcs12 *vmcs12;
14406 struct vcpu_vmx *vmx = to_vmx(vcpu);
14407 gpa_t gpa;
14408 struct page *page = NULL;
14409 u64 *pml_address;
14410
14411 if (is_guest_mode(vcpu)) {
14412 WARN_ON_ONCE(vmx->nested.pml_full);
14413
14414 /*
14415 * Check if PML is enabled for the nested guest.
14416 * Whether eptp bit 6 is set is already checked
14417 * as part of A/D emulation.
14418 */
14419 vmcs12 = get_vmcs12(vcpu);
14420 if (!nested_cpu_has_pml(vmcs12))
14421 return 0;
14422
Dan Carpenter47698862017-05-10 22:43:17 +030014423 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040014424 vmx->nested.pml_full = true;
14425 return 1;
14426 }
14427
14428 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
14429
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020014430 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
14431 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040014432 return 0;
14433
14434 pml_address = kmap(page);
14435 pml_address[vmcs12->guest_pml_index--] = gpa;
14436 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020014437 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040014438 }
14439
14440 return 0;
14441}
14442
Kai Huang843e4332015-01-28 10:54:28 +080014443static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
14444 struct kvm_memory_slot *memslot,
14445 gfn_t offset, unsigned long mask)
14446{
14447 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
14448}
14449
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014450static void __pi_post_block(struct kvm_vcpu *vcpu)
14451{
14452 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
14453 struct pi_desc old, new;
14454 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014455
14456 do {
14457 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014458 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
14459 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014460
14461 dest = cpu_physical_id(vcpu->cpu);
14462
14463 if (x2apic_enabled())
14464 new.ndst = dest;
14465 else
14466 new.ndst = (dest << 8) & 0xFF00;
14467
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014468 /* set 'NV' to 'notification vector' */
14469 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020014470 } while (cmpxchg64(&pi_desc->control, old.control,
14471 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014472
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014473 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
14474 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014475 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014476 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014477 vcpu->pre_pcpu = -1;
14478 }
14479}
14480
Feng Wuefc64402015-09-18 22:29:51 +080014481/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080014482 * This routine does the following things for vCPU which is going
14483 * to be blocked if VT-d PI is enabled.
14484 * - Store the vCPU to the wakeup list, so when interrupts happen
14485 * we can find the right vCPU to wake up.
14486 * - Change the Posted-interrupt descriptor as below:
14487 * 'NDST' <-- vcpu->pre_pcpu
14488 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
14489 * - If 'ON' is set during this process, which means at least one
14490 * interrupt is posted for this vCPU, we cannot block it, in
14491 * this case, return 1, otherwise, return 0.
14492 *
14493 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070014494static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014495{
Feng Wubf9f6ac2015-09-18 22:29:55 +080014496 unsigned int dest;
14497 struct pi_desc old, new;
14498 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
14499
14500 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014501 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14502 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080014503 return 0;
14504
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014505 WARN_ON(irqs_disabled());
14506 local_irq_disable();
14507 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
14508 vcpu->pre_pcpu = vcpu->cpu;
14509 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14510 list_add_tail(&vcpu->blocked_vcpu_list,
14511 &per_cpu(blocked_vcpu_on_cpu,
14512 vcpu->pre_pcpu));
14513 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
14514 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080014515
14516 do {
14517 old.control = new.control = pi_desc->control;
14518
Feng Wubf9f6ac2015-09-18 22:29:55 +080014519 WARN((pi_desc->sn == 1),
14520 "Warning: SN field of posted-interrupts "
14521 "is set before blocking\n");
14522
14523 /*
14524 * Since vCPU can be preempted during this process,
14525 * vcpu->cpu could be different with pre_pcpu, we
14526 * need to set pre_pcpu as the destination of wakeup
14527 * notification event, then we can find the right vCPU
14528 * to wakeup in wakeup handler if interrupts happen
14529 * when the vCPU is in blocked state.
14530 */
14531 dest = cpu_physical_id(vcpu->pre_pcpu);
14532
14533 if (x2apic_enabled())
14534 new.ndst = dest;
14535 else
14536 new.ndst = (dest << 8) & 0xFF00;
14537
14538 /* set 'NV' to 'wakeup vector' */
14539 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020014540 } while (cmpxchg64(&pi_desc->control, old.control,
14541 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014542
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014543 /* We should not block the vCPU if an interrupt is posted for it. */
14544 if (pi_test_on(pi_desc) == 1)
14545 __pi_post_block(vcpu);
14546
14547 local_irq_enable();
14548 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014549}
14550
Yunhong Jiangbc225122016-06-13 14:19:58 -070014551static int vmx_pre_block(struct kvm_vcpu *vcpu)
14552{
14553 if (pi_pre_block(vcpu))
14554 return 1;
14555
Yunhong Jiang64672c92016-06-13 14:19:59 -070014556 if (kvm_lapic_hv_timer_in_use(vcpu))
14557 kvm_lapic_switch_to_sw_timer(vcpu);
14558
Yunhong Jiangbc225122016-06-13 14:19:58 -070014559 return 0;
14560}
14561
14562static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014563{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014564 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014565 return;
14566
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014567 WARN_ON(irqs_disabled());
14568 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014569 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014570 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080014571}
14572
Yunhong Jiangbc225122016-06-13 14:19:58 -070014573static void vmx_post_block(struct kvm_vcpu *vcpu)
14574{
Yunhong Jiang64672c92016-06-13 14:19:59 -070014575 if (kvm_x86_ops->set_hv_timer)
14576 kvm_lapic_switch_to_hv_timer(vcpu);
14577
Yunhong Jiangbc225122016-06-13 14:19:58 -070014578 pi_post_block(vcpu);
14579}
14580
Feng Wubf9f6ac2015-09-18 22:29:55 +080014581/*
Feng Wuefc64402015-09-18 22:29:51 +080014582 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14583 *
14584 * @kvm: kvm
14585 * @host_irq: host irq of the interrupt
14586 * @guest_irq: gsi of the interrupt
14587 * @set: set or unset PI
14588 * returns 0 on success, < 0 on failure
14589 */
14590static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14591 uint32_t guest_irq, bool set)
14592{
14593 struct kvm_kernel_irq_routing_entry *e;
14594 struct kvm_irq_routing_table *irq_rt;
14595 struct kvm_lapic_irq irq;
14596 struct kvm_vcpu *vcpu;
14597 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014598 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080014599
14600 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014601 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14602 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080014603 return 0;
14604
14605 idx = srcu_read_lock(&kvm->irq_srcu);
14606 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014607 if (guest_irq >= irq_rt->nr_rt_entries ||
14608 hlist_empty(&irq_rt->map[guest_irq])) {
14609 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14610 guest_irq, irq_rt->nr_rt_entries);
14611 goto out;
14612 }
Feng Wuefc64402015-09-18 22:29:51 +080014613
14614 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14615 if (e->type != KVM_IRQ_ROUTING_MSI)
14616 continue;
14617 /*
14618 * VT-d PI cannot support posting multicast/broadcast
14619 * interrupts to a vCPU, we still use interrupt remapping
14620 * for these kind of interrupts.
14621 *
14622 * For lowest-priority interrupts, we only support
14623 * those with single CPU as the destination, e.g. user
14624 * configures the interrupts via /proc/irq or uses
14625 * irqbalance to make the interrupts single-CPU.
14626 *
14627 * We will support full lowest-priority interrupt later.
14628 */
14629
Radim Krčmář371313132016-07-12 22:09:27 +020014630 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014631 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14632 /*
14633 * Make sure the IRTE is in remapped mode if
14634 * we don't handle it in posted mode.
14635 */
14636 ret = irq_set_vcpu_affinity(host_irq, NULL);
14637 if (ret < 0) {
14638 printk(KERN_INFO
14639 "failed to back to remapped mode, irq: %u\n",
14640 host_irq);
14641 goto out;
14642 }
14643
Feng Wuefc64402015-09-18 22:29:51 +080014644 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014645 }
Feng Wuefc64402015-09-18 22:29:51 +080014646
14647 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14648 vcpu_info.vector = irq.vector;
14649
hu huajun2698d822018-04-11 15:16:40 +080014650 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014651 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14652
14653 if (set)
14654 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014655 else
Feng Wuefc64402015-09-18 22:29:51 +080014656 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014657
14658 if (ret < 0) {
14659 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14660 __func__);
14661 goto out;
14662 }
14663 }
14664
14665 ret = 0;
14666out:
14667 srcu_read_unlock(&kvm->irq_srcu, idx);
14668 return ret;
14669}
14670
Ashok Rajc45dcc72016-06-22 14:59:56 +080014671static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14672{
14673 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14674 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14675 FEATURE_CONTROL_LMCE;
14676 else
14677 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14678 ~FEATURE_CONTROL_LMCE;
14679}
14680
Ladi Prosek72d7b372017-10-11 16:54:41 +020014681static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14682{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014683 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14684 if (to_vmx(vcpu)->nested.nested_run_pending)
14685 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014686 return 1;
14687}
14688
Ladi Prosek0234bf82017-10-11 16:54:40 +020014689static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14690{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014691 struct vcpu_vmx *vmx = to_vmx(vcpu);
14692
14693 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14694 if (vmx->nested.smm.guest_mode)
14695 nested_vmx_vmexit(vcpu, -1, 0, 0);
14696
14697 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14698 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014699 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014700 return 0;
14701}
14702
14703static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14704{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014705 struct vcpu_vmx *vmx = to_vmx(vcpu);
14706 int ret;
14707
14708 if (vmx->nested.smm.vmxon) {
14709 vmx->nested.vmxon = true;
14710 vmx->nested.smm.vmxon = false;
14711 }
14712
14713 if (vmx->nested.smm.guest_mode) {
14714 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014715 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014716 vcpu->arch.hflags |= HF_SMM_MASK;
14717 if (ret)
14718 return ret;
14719
14720 vmx->nested.smm.guest_mode = false;
14721 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014722 return 0;
14723}
14724
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014725static int enable_smi_window(struct kvm_vcpu *vcpu)
14726{
14727 return 0;
14728}
14729
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014730static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
14731{
14732 struct vcpu_vmx *vmx = to_vmx(vcpu);
14733
14734 /*
14735 * In case we do two consecutive get/set_nested_state()s while L2 was
14736 * running hv_evmcs may end up not being mapped (we map it from
14737 * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
14738 * have vmcs12 if it is true.
14739 */
14740 return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
14741 vmx->nested.hv_evmcs;
14742}
14743
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014744static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14745 struct kvm_nested_state __user *user_kvm_nested_state,
14746 u32 user_data_size)
14747{
14748 struct vcpu_vmx *vmx;
14749 struct vmcs12 *vmcs12;
14750 struct kvm_nested_state kvm_state = {
14751 .flags = 0,
14752 .format = 0,
14753 .size = sizeof(kvm_state),
14754 .vmx.vmxon_pa = -1ull,
14755 .vmx.vmcs_pa = -1ull,
14756 };
14757
14758 if (!vcpu)
14759 return kvm_state.size + 2 * VMCS12_SIZE;
14760
14761 vmx = to_vmx(vcpu);
14762 vmcs12 = get_vmcs12(vcpu);
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014763
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014764 if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
14765 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014766
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014767 if (nested_vmx_allowed(vcpu) &&
14768 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14769 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14770 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14771
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014772 if (vmx_has_valid_vmcs12(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014773 kvm_state.size += VMCS12_SIZE;
14774
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014775 if (is_guest_mode(vcpu) &&
14776 nested_cpu_has_shadow_vmcs(vmcs12) &&
14777 vmcs12->vmcs_link_pointer != -1ull)
14778 kvm_state.size += VMCS12_SIZE;
14779 }
14780
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014781 if (vmx->nested.smm.vmxon)
14782 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14783
14784 if (vmx->nested.smm.guest_mode)
14785 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14786
14787 if (is_guest_mode(vcpu)) {
14788 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14789
14790 if (vmx->nested.nested_run_pending)
14791 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14792 }
14793 }
14794
14795 if (user_data_size < kvm_state.size)
14796 goto out;
14797
14798 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14799 return -EFAULT;
14800
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014801 if (!vmx_has_valid_vmcs12(vcpu))
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014802 goto out;
14803
14804 /*
14805 * When running L2, the authoritative vmcs12 state is in the
14806 * vmcs02. When running L1, the authoritative vmcs12 state is
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014807 * in the shadow or enlightened vmcs linked to vmcs01, unless
Vitaly Kuznetsov945679e2018-10-16 18:50:02 +020014808 * need_vmcs12_sync is set, in which case, the authoritative
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014809 * vmcs12 state is in the vmcs12 already.
14810 */
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014811 if (is_guest_mode(vcpu)) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014812 sync_vmcs12(vcpu, vmcs12);
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014813 } else if (!vmx->nested.need_vmcs12_sync) {
14814 if (vmx->nested.hv_evmcs)
14815 copy_enlightened_to_vmcs12(vmx);
14816 else if (enable_shadow_vmcs)
14817 copy_shadow_to_vmcs12(vmx);
14818 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014819
14820 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14821 return -EFAULT;
14822
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014823 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14824 vmcs12->vmcs_link_pointer != -1ull) {
14825 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14826 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14827 return -EFAULT;
14828 }
14829
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014830out:
14831 return kvm_state.size;
14832}
14833
14834static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14835 struct kvm_nested_state __user *user_kvm_nested_state,
14836 struct kvm_nested_state *kvm_state)
14837{
14838 struct vcpu_vmx *vmx = to_vmx(vcpu);
14839 struct vmcs12 *vmcs12;
14840 u32 exit_qual;
14841 int ret;
14842
14843 if (kvm_state->format != 0)
14844 return -EINVAL;
14845
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014846 if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
14847 nested_enable_evmcs(vcpu, NULL);
14848
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014849 if (!nested_vmx_allowed(vcpu))
14850 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14851
14852 if (kvm_state->vmx.vmxon_pa == -1ull) {
14853 if (kvm_state->vmx.smm.flags)
14854 return -EINVAL;
14855
14856 if (kvm_state->vmx.vmcs_pa != -1ull)
14857 return -EINVAL;
14858
14859 vmx_leave_nested(vcpu);
14860 return 0;
14861 }
14862
14863 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14864 return -EINVAL;
14865
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014866 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14867 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14868 return -EINVAL;
14869
14870 if (kvm_state->vmx.smm.flags &
14871 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14872 return -EINVAL;
14873
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014874 /*
14875 * SMM temporarily disables VMX, so we cannot be in guest mode,
14876 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14877 * must be zero.
14878 */
14879 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14880 return -EINVAL;
14881
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014882 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14883 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14884 return -EINVAL;
14885
14886 vmx_leave_nested(vcpu);
14887 if (kvm_state->vmx.vmxon_pa == -1ull)
14888 return 0;
14889
14890 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14891 ret = enter_vmx_operation(vcpu);
14892 if (ret)
14893 return ret;
14894
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020014895 /* Empty 'VMXON' state is permitted */
14896 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14897 return 0;
14898
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014899 if (kvm_state->vmx.vmcs_pa != -1ull) {
14900 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14901 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14902 return -EINVAL;
Vitaly Kuznetsova1b0c1c2018-10-16 18:50:07 +020014903
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +020014904 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14905 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
14906 /*
14907 * Sync eVMCS upon entry as we may not have
14908 * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
14909 */
14910 vmx->nested.need_vmcs12_sync = true;
14911 } else {
14912 return -EINVAL;
14913 }
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014914
14915 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14916 vmx->nested.smm.vmxon = true;
14917 vmx->nested.vmxon = false;
14918
14919 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14920 vmx->nested.smm.guest_mode = true;
14921 }
14922
14923 vmcs12 = get_vmcs12(vcpu);
14924 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14925 return -EFAULT;
14926
Liran Alon392b2f22018-06-23 02:35:01 +030014927 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014928 return -EINVAL;
14929
14930 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14931 return 0;
14932
14933 vmx->nested.nested_run_pending =
14934 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14935
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014936 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14937 vmcs12->vmcs_link_pointer != -1ull) {
14938 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14939 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14940 return -EINVAL;
14941
14942 if (copy_from_user(shadow_vmcs12,
14943 user_kvm_nested_state->data + VMCS12_SIZE,
14944 sizeof(*vmcs12)))
14945 return -EFAULT;
14946
14947 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14948 !shadow_vmcs12->hdr.shadow_vmcs)
14949 return -EINVAL;
14950 }
14951
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014952 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14953 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14954 return -EINVAL;
14955
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014956 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014957 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014958 if (ret)
14959 return -EINVAL;
14960
14961 return 0;
14962}
14963
Kees Cook404f6aa2016-08-08 16:29:06 -070014964static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014965 .cpu_has_kvm_support = cpu_has_kvm_support,
14966 .disabled_by_bios = vmx_disabled_by_bios,
14967 .hardware_setup = hardware_setup,
14968 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014969 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014970 .hardware_enable = hardware_enable,
14971 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014972 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014973 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014974
Wanpeng Lib31c1142018-03-12 04:53:04 -070014975 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014976 .vm_alloc = vmx_vm_alloc,
14977 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014978
Avi Kivity6aa8b732006-12-10 02:21:36 -080014979 .vcpu_create = vmx_create_vcpu,
14980 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014981 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014982
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014983 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014984 .vcpu_load = vmx_vcpu_load,
14985 .vcpu_put = vmx_vcpu_put,
14986
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014987 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014988 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014989 .get_msr = vmx_get_msr,
14990 .set_msr = vmx_set_msr,
14991 .get_segment_base = vmx_get_segment_base,
14992 .get_segment = vmx_get_segment,
14993 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014994 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014995 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014996 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014997 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014998 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014999 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015000 .set_cr3 = vmx_set_cr3,
15001 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015002 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015003 .get_idt = vmx_get_idt,
15004 .set_idt = vmx_set_idt,
15005 .get_gdt = vmx_get_gdt,
15006 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010015007 .get_dr6 = vmx_get_dr6,
15008 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030015009 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010015010 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030015011 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015012 .get_rflags = vmx_get_rflags,
15013 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080015014
Avi Kivity6aa8b732006-12-10 02:21:36 -080015015 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070015016 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015017
Avi Kivity6aa8b732006-12-10 02:21:36 -080015018 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020015019 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015020 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040015021 .set_interrupt_shadow = vmx_set_interrupt_shadow,
15022 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020015023 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030015024 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015025 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020015026 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030015027 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020015028 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015029 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010015030 .get_nmi_mask = vmx_get_nmi_mask,
15031 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015032 .enable_nmi_window = enable_nmi_window,
15033 .enable_irq_window = enable_irq_window,
15034 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040015035 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080015036 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030015037 .get_enable_apicv = vmx_get_enable_apicv,
15038 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080015039 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010015040 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080015041 .hwapic_irr_update = vmx_hwapic_irr_update,
15042 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030015043 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080015044 .sync_pir_to_irr = vmx_sync_pir_to_irr,
15045 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030015046
Izik Eiduscbc94022007-10-25 00:29:55 +020015047 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070015048 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080015049 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080015050 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030015051
Avi Kivity586f9602010-11-18 13:09:54 +020015052 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020015053
Sheng Yang17cc3932010-01-05 19:02:27 +080015054 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080015055
15056 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080015057
15058 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000015059 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020015060
15061 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080015062
15063 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100015064
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020015065 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100015066 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020015067
15068 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020015069
15070 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080015071 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000015072 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080015073 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020015074 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010015075
15076 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070015077 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020015078
15079 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080015080
15081 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
15082 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
15083 .flush_log_dirty = vmx_flush_log_dirty,
15084 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040015085 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020015086
Feng Wubf9f6ac2015-09-18 22:29:55 +080015087 .pre_block = vmx_pre_block,
15088 .post_block = vmx_post_block,
15089
Wei Huang25462f72015-06-19 15:45:05 +020015090 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080015091
15092 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070015093
15094#ifdef CONFIG_X86_64
15095 .set_hv_timer = vmx_set_hv_timer,
15096 .cancel_hv_timer = vmx_cancel_hv_timer,
15097#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080015098
15099 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020015100
Jim Mattson8fcc4b52018-07-10 11:27:20 +020015101 .get_nested_state = vmx_get_nested_state,
15102 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020015103 .get_vmcs12_pages = nested_get_vmcs12_pages,
15104
Ladi Prosek72d7b372017-10-11 16:54:41 +020015105 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020015106 .pre_enter_smm = vmx_pre_enter_smm,
15107 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020015108 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +020015109
15110 .nested_enable_evmcs = nested_enable_evmcs,
Avi Kivity6aa8b732006-12-10 02:21:36 -080015111};
15112
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020015113static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020015114{
15115 if (vmx_l1d_flush_pages) {
15116 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
15117 vmx_l1d_flush_pages = NULL;
15118 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020015119 /* Restore state so sysfs ignores VMX */
15120 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020015121}
15122
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015123static void vmx_exit(void)
15124{
15125#ifdef CONFIG_KEXEC_CORE
15126 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
15127 synchronize_rcu();
15128#endif
15129
15130 kvm_exit();
15131
15132#if IS_ENABLED(CONFIG_HYPERV)
15133 if (static_branch_unlikely(&enable_evmcs)) {
15134 int cpu;
15135 struct hv_vp_assist_page *vp_ap;
15136 /*
15137 * Reset everything to support using non-enlightened VMCS
15138 * access later (e.g. when we reload the module with
15139 * enlightened_vmcs=0)
15140 */
15141 for_each_online_cpu(cpu) {
15142 vp_ap = hv_get_vp_assist_page(cpu);
15143
15144 if (!vp_ap)
15145 continue;
15146
15147 vp_ap->current_nested_vmcs = 0;
15148 vp_ap->enlighten_vmentry = 0;
15149 }
15150
15151 static_branch_disable(&enable_evmcs);
15152 }
15153#endif
15154 vmx_cleanup_l1d_flush();
15155}
15156module_exit(vmx_exit);
15157
Avi Kivity6aa8b732006-12-10 02:21:36 -080015158static int __init vmx_init(void)
15159{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010015160 int r;
15161
15162#if IS_ENABLED(CONFIG_HYPERV)
15163 /*
15164 * Enlightened VMCS usage should be recommended and the host needs
15165 * to support eVMCS v1 or above. We can also disable eVMCS support
15166 * with module parameter.
15167 */
15168 if (enlightened_vmcs &&
15169 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
15170 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
15171 KVM_EVMCS_VERSION) {
15172 int cpu;
15173
15174 /* Check that we have assist pages on all online CPUs */
15175 for_each_online_cpu(cpu) {
15176 if (!hv_get_vp_assist_page(cpu)) {
15177 enlightened_vmcs = false;
15178 break;
15179 }
15180 }
15181
15182 if (enlightened_vmcs) {
15183 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
15184 static_branch_enable(&enable_evmcs);
15185 }
15186 } else {
15187 enlightened_vmcs = false;
15188 }
15189#endif
15190
15191 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015192 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030015193 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080015194 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080015195
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015196 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020015197 * Must be called after kvm_init() so enable_ept is properly set
15198 * up. Hand the parameter mitigation value in which was stored in
15199 * the pre module init parser. If no parameter was given, it will
15200 * contain 'auto' which will be turned into the default 'cond'
15201 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015202 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020015203 if (boot_cpu_has(X86_BUG_L1TF)) {
15204 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
15205 if (r) {
15206 vmx_exit();
15207 return r;
15208 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020015209 }
15210
Dave Young2965faa2015-09-09 15:38:55 -070015211#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080015212 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
15213 crash_vmclear_local_loaded_vmcss);
15214#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070015215 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080015216
He, Qingfdef3ad2007-04-30 09:45:24 +030015217 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080015218}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020015219module_init(vmx_init);