blob: a0c3992a5e882b2d203d0d844c5a24d07882dcc2 [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"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Sean Christopherson52017602018-09-26 09:23:57 -0700113static bool __read_mostly nested_early_check = 0;
114module_param(nested_early_check, bool, S_IRUGO);
115
Wanpeng Li20300092014-12-02 19:14:59 +0800116static u64 __read_mostly host_xss;
117
Kai Huang843e4332015-01-28 10:54:28 +0800118static bool __read_mostly enable_pml = 1;
119module_param_named(pml, enable_pml, bool, S_IRUGO);
120
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100121#define MSR_TYPE_R 1
122#define MSR_TYPE_W 2
123#define MSR_TYPE_RW 3
124
125#define MSR_BITMAP_MODE_X2APIC 1
126#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100127
Haozhong Zhang64903d62015-10-20 15:39:09 +0800128#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129
Yunhong Jiang64672c92016-06-13 14:19:59 -0700130/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
131static int __read_mostly cpu_preemption_timer_multi;
132static bool __read_mostly enable_preemption_timer = 1;
133#ifdef CONFIG_X86_64
134module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135#endif
136
Sean Christopherson3de63472018-07-13 08:42:30 -0700137#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800138#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
139#define KVM_VM_CR0_ALWAYS_ON \
140 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
141 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200142#define KVM_CR4_GUEST_OWNED_BITS \
143 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800144 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200145
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800146#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200147#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
148#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
149
Avi Kivity78ac8b42010-04-08 18:19:35 +0300150#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
151
Jan Kiszkaf4124502014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Babu Moger7fbc85a2018-03-16 16:37:22 -0400177static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
178module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800179
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400181static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400182module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
184/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400185static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200187
188/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
190module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200191
Avi Kivity83287ea422012-09-16 15:10:57 +0300192extern const ulong vmx_return;
Sean Christopherson52017602018-09-26 09:23:57 -0700193extern const ulong vmx_early_consistency_check_return;
Avi Kivity83287ea422012-09-16 15:10:57 +0300194
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200195static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200196static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200197static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200199/* Storage for pre module init parameter parsing */
200static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200201
202static const struct {
203 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200204 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200205} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200206 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
207 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
208 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
209 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
210 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
211 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200212};
213
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200214#define L1D_CACHE_ORDER 4
215static void *vmx_l1d_flush_pages;
216
217static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
218{
219 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200220 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200221
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200222 if (!enable_ept) {
223 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
224 return 0;
225 }
226
Yi Wangd806afa2018-08-16 13:42:39 +0800227 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
228 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200229
Yi Wangd806afa2018-08-16 13:42:39 +0800230 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
231 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
232 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
233 return 0;
234 }
235 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200236
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200237 /* If set to auto use the default l1tf mitigation method */
238 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
239 switch (l1tf_mitigation) {
240 case L1TF_MITIGATION_OFF:
241 l1tf = VMENTER_L1D_FLUSH_NEVER;
242 break;
243 case L1TF_MITIGATION_FLUSH_NOWARN:
244 case L1TF_MITIGATION_FLUSH:
245 case L1TF_MITIGATION_FLUSH_NOSMT:
246 l1tf = VMENTER_L1D_FLUSH_COND;
247 break;
248 case L1TF_MITIGATION_FULL:
249 case L1TF_MITIGATION_FULL_FORCE:
250 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
251 break;
252 }
253 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
254 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
255 }
256
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200257 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
258 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
259 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
260 if (!page)
261 return -ENOMEM;
262 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200263
264 /*
265 * Initialize each page with a different pattern in
266 * order to protect against KSM in the nested
267 * virtualization case.
268 */
269 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
270 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
271 PAGE_SIZE);
272 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200273 }
274
275 l1tf_vmx_mitigation = l1tf;
276
Thomas Gleixner895ae472018-07-13 16:23:22 +0200277 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
278 static_branch_enable(&vmx_l1d_should_flush);
279 else
280 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200281
Nicolai Stange427362a2018-07-21 22:25:00 +0200282 if (l1tf == VMENTER_L1D_FLUSH_COND)
283 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200284 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200285 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200286 return 0;
287}
288
289static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200290{
291 unsigned int i;
292
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200293 if (s) {
294 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200295 if (vmentry_l1d_param[i].for_parse &&
296 sysfs_streq(s, vmentry_l1d_param[i].option))
297 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200298 }
299 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200300 return -EINVAL;
301}
302
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200303static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
304{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200305 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200306
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200307 l1tf = vmentry_l1d_flush_parse(s);
308 if (l1tf < 0)
309 return l1tf;
310
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200311 if (!boot_cpu_has(X86_BUG_L1TF))
312 return 0;
313
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200314 /*
315 * Has vmx_init() run already? If not then this is the pre init
316 * parameter parsing. In that case just store the value and let
317 * vmx_init() do the proper setup after enable_ept has been
318 * established.
319 */
320 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
321 vmentry_l1d_flush_param = l1tf;
322 return 0;
323 }
324
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200325 mutex_lock(&vmx_l1d_flush_mutex);
326 ret = vmx_setup_l1d_flush(l1tf);
327 mutex_unlock(&vmx_l1d_flush_mutex);
328 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200329}
330
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200331static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
332{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200333 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
334 return sprintf(s, "???\n");
335
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200336 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200337}
338
339static const struct kernel_param_ops vmentry_l1d_flush_ops = {
340 .set = vmentry_l1d_flush_set,
341 .get = vmentry_l1d_flush_get,
342};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200343module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200344
Tianyu Lan877ad952018-07-19 08:40:23 +0000345enum ept_pointers_status {
346 EPT_POINTERS_CHECK = 0,
347 EPT_POINTERS_MATCH = 1,
348 EPT_POINTERS_MISMATCH = 2
349};
350
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700351struct kvm_vmx {
352 struct kvm kvm;
353
354 unsigned int tss_addr;
355 bool ept_identity_pagetable_done;
356 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000357
358 enum ept_pointers_status ept_pointers_match;
359 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700360};
361
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200362#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300363
Liran Alon392b2f22018-06-23 02:35:01 +0300364struct vmcs_hdr {
365 u32 revision_id:31;
366 u32 shadow_vmcs:1;
367};
368
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400369struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300370 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400371 u32 abort;
372 char data[0];
373};
374
Nadav Har'Eld462b812011-05-24 15:26:10 +0300375/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700376 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
377 * and whose values change infrequently, but are not constant. I.e. this is
378 * used as a write-through cache of the corresponding VMCS fields.
379 */
380struct vmcs_host_state {
381 unsigned long cr3; /* May not match real cr3 */
382 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700383 unsigned long gs_base;
384 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700385
386 u16 fs_sel, gs_sel, ldt_sel;
387#ifdef CONFIG_X86_64
388 u16 ds_sel, es_sel;
389#endif
390};
391
392/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300393 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
394 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
395 * loaded on this CPU (so we can clear them if the CPU goes down).
396 */
397struct loaded_vmcs {
398 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700399 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300400 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200401 bool launched;
402 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700403 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100404 /* Support for vnmi-less CPUs */
405 int soft_vnmi_blocked;
406 ktime_t entry_time;
407 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100408 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300409 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700410 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300411};
412
Avi Kivity26bb0982009-09-07 11:14:12 +0300413struct shared_msr_entry {
414 unsigned index;
415 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200416 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300417};
418
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300419/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300420 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
421 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
422 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
423 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
424 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
425 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600426 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300427 * underlying hardware which will be used to run L2.
428 * This structure is packed to ensure that its layout is identical across
429 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700430 *
431 * IMPORTANT: Changing the layout of existing fields in this structure
432 * will break save/restore compatibility with older kvm releases. When
433 * adding new fields, either use space in the reserved padding* arrays
434 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300435 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300436typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300437struct __packed vmcs12 {
438 /* According to the Intel spec, a VMCS region must start with the
439 * following two fields. Then follow implementation-specific data.
440 */
Liran Alon392b2f22018-06-23 02:35:01 +0300441 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300442 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300443
Nadav Har'El27d6c862011-05-25 23:06:59 +0300444 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
445 u32 padding[7]; /* room for future expansion */
446
Nadav Har'El22bd0352011-05-25 23:05:57 +0300447 u64 io_bitmap_a;
448 u64 io_bitmap_b;
449 u64 msr_bitmap;
450 u64 vm_exit_msr_store_addr;
451 u64 vm_exit_msr_load_addr;
452 u64 vm_entry_msr_load_addr;
453 u64 tsc_offset;
454 u64 virtual_apic_page_addr;
455 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800456 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300457 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800458 u64 eoi_exit_bitmap0;
459 u64 eoi_exit_bitmap1;
460 u64 eoi_exit_bitmap2;
461 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800462 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300463 u64 guest_physical_address;
464 u64 vmcs_link_pointer;
465 u64 guest_ia32_debugctl;
466 u64 guest_ia32_pat;
467 u64 guest_ia32_efer;
468 u64 guest_ia32_perf_global_ctrl;
469 u64 guest_pdptr0;
470 u64 guest_pdptr1;
471 u64 guest_pdptr2;
472 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100473 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300474 u64 host_ia32_pat;
475 u64 host_ia32_efer;
476 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700477 u64 vmread_bitmap;
478 u64 vmwrite_bitmap;
479 u64 vm_function_control;
480 u64 eptp_list_address;
481 u64 pml_address;
482 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300483 /*
484 * To allow migration of L1 (complete with its L2 guests) between
485 * machines of different natural widths (32 or 64 bit), we cannot have
486 * unsigned long fields with no explict size. We use u64 (aliased
487 * natural_width) instead. Luckily, x86 is little-endian.
488 */
489 natural_width cr0_guest_host_mask;
490 natural_width cr4_guest_host_mask;
491 natural_width cr0_read_shadow;
492 natural_width cr4_read_shadow;
493 natural_width cr3_target_value0;
494 natural_width cr3_target_value1;
495 natural_width cr3_target_value2;
496 natural_width cr3_target_value3;
497 natural_width exit_qualification;
498 natural_width guest_linear_address;
499 natural_width guest_cr0;
500 natural_width guest_cr3;
501 natural_width guest_cr4;
502 natural_width guest_es_base;
503 natural_width guest_cs_base;
504 natural_width guest_ss_base;
505 natural_width guest_ds_base;
506 natural_width guest_fs_base;
507 natural_width guest_gs_base;
508 natural_width guest_ldtr_base;
509 natural_width guest_tr_base;
510 natural_width guest_gdtr_base;
511 natural_width guest_idtr_base;
512 natural_width guest_dr7;
513 natural_width guest_rsp;
514 natural_width guest_rip;
515 natural_width guest_rflags;
516 natural_width guest_pending_dbg_exceptions;
517 natural_width guest_sysenter_esp;
518 natural_width guest_sysenter_eip;
519 natural_width host_cr0;
520 natural_width host_cr3;
521 natural_width host_cr4;
522 natural_width host_fs_base;
523 natural_width host_gs_base;
524 natural_width host_tr_base;
525 natural_width host_gdtr_base;
526 natural_width host_idtr_base;
527 natural_width host_ia32_sysenter_esp;
528 natural_width host_ia32_sysenter_eip;
529 natural_width host_rsp;
530 natural_width host_rip;
531 natural_width paddingl[8]; /* room for future expansion */
532 u32 pin_based_vm_exec_control;
533 u32 cpu_based_vm_exec_control;
534 u32 exception_bitmap;
535 u32 page_fault_error_code_mask;
536 u32 page_fault_error_code_match;
537 u32 cr3_target_count;
538 u32 vm_exit_controls;
539 u32 vm_exit_msr_store_count;
540 u32 vm_exit_msr_load_count;
541 u32 vm_entry_controls;
542 u32 vm_entry_msr_load_count;
543 u32 vm_entry_intr_info_field;
544 u32 vm_entry_exception_error_code;
545 u32 vm_entry_instruction_len;
546 u32 tpr_threshold;
547 u32 secondary_vm_exec_control;
548 u32 vm_instruction_error;
549 u32 vm_exit_reason;
550 u32 vm_exit_intr_info;
551 u32 vm_exit_intr_error_code;
552 u32 idt_vectoring_info_field;
553 u32 idt_vectoring_error_code;
554 u32 vm_exit_instruction_len;
555 u32 vmx_instruction_info;
556 u32 guest_es_limit;
557 u32 guest_cs_limit;
558 u32 guest_ss_limit;
559 u32 guest_ds_limit;
560 u32 guest_fs_limit;
561 u32 guest_gs_limit;
562 u32 guest_ldtr_limit;
563 u32 guest_tr_limit;
564 u32 guest_gdtr_limit;
565 u32 guest_idtr_limit;
566 u32 guest_es_ar_bytes;
567 u32 guest_cs_ar_bytes;
568 u32 guest_ss_ar_bytes;
569 u32 guest_ds_ar_bytes;
570 u32 guest_fs_ar_bytes;
571 u32 guest_gs_ar_bytes;
572 u32 guest_ldtr_ar_bytes;
573 u32 guest_tr_ar_bytes;
574 u32 guest_interruptibility_info;
575 u32 guest_activity_state;
576 u32 guest_sysenter_cs;
577 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100578 u32 vmx_preemption_timer_value;
579 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300580 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800581 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300582 u16 guest_es_selector;
583 u16 guest_cs_selector;
584 u16 guest_ss_selector;
585 u16 guest_ds_selector;
586 u16 guest_fs_selector;
587 u16 guest_gs_selector;
588 u16 guest_ldtr_selector;
589 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800590 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300591 u16 host_es_selector;
592 u16 host_cs_selector;
593 u16 host_ss_selector;
594 u16 host_ds_selector;
595 u16 host_fs_selector;
596 u16 host_gs_selector;
597 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700598 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300599};
600
601/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700602 * For save/restore compatibility, the vmcs12 field offsets must not change.
603 */
604#define CHECK_OFFSET(field, loc) \
605 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
606 "Offset of " #field " in struct vmcs12 has changed.")
607
608static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300609 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700610 CHECK_OFFSET(abort, 4);
611 CHECK_OFFSET(launch_state, 8);
612 CHECK_OFFSET(io_bitmap_a, 40);
613 CHECK_OFFSET(io_bitmap_b, 48);
614 CHECK_OFFSET(msr_bitmap, 56);
615 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
616 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
617 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
618 CHECK_OFFSET(tsc_offset, 88);
619 CHECK_OFFSET(virtual_apic_page_addr, 96);
620 CHECK_OFFSET(apic_access_addr, 104);
621 CHECK_OFFSET(posted_intr_desc_addr, 112);
622 CHECK_OFFSET(ept_pointer, 120);
623 CHECK_OFFSET(eoi_exit_bitmap0, 128);
624 CHECK_OFFSET(eoi_exit_bitmap1, 136);
625 CHECK_OFFSET(eoi_exit_bitmap2, 144);
626 CHECK_OFFSET(eoi_exit_bitmap3, 152);
627 CHECK_OFFSET(xss_exit_bitmap, 160);
628 CHECK_OFFSET(guest_physical_address, 168);
629 CHECK_OFFSET(vmcs_link_pointer, 176);
630 CHECK_OFFSET(guest_ia32_debugctl, 184);
631 CHECK_OFFSET(guest_ia32_pat, 192);
632 CHECK_OFFSET(guest_ia32_efer, 200);
633 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
634 CHECK_OFFSET(guest_pdptr0, 216);
635 CHECK_OFFSET(guest_pdptr1, 224);
636 CHECK_OFFSET(guest_pdptr2, 232);
637 CHECK_OFFSET(guest_pdptr3, 240);
638 CHECK_OFFSET(guest_bndcfgs, 248);
639 CHECK_OFFSET(host_ia32_pat, 256);
640 CHECK_OFFSET(host_ia32_efer, 264);
641 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
642 CHECK_OFFSET(vmread_bitmap, 280);
643 CHECK_OFFSET(vmwrite_bitmap, 288);
644 CHECK_OFFSET(vm_function_control, 296);
645 CHECK_OFFSET(eptp_list_address, 304);
646 CHECK_OFFSET(pml_address, 312);
647 CHECK_OFFSET(cr0_guest_host_mask, 344);
648 CHECK_OFFSET(cr4_guest_host_mask, 352);
649 CHECK_OFFSET(cr0_read_shadow, 360);
650 CHECK_OFFSET(cr4_read_shadow, 368);
651 CHECK_OFFSET(cr3_target_value0, 376);
652 CHECK_OFFSET(cr3_target_value1, 384);
653 CHECK_OFFSET(cr3_target_value2, 392);
654 CHECK_OFFSET(cr3_target_value3, 400);
655 CHECK_OFFSET(exit_qualification, 408);
656 CHECK_OFFSET(guest_linear_address, 416);
657 CHECK_OFFSET(guest_cr0, 424);
658 CHECK_OFFSET(guest_cr3, 432);
659 CHECK_OFFSET(guest_cr4, 440);
660 CHECK_OFFSET(guest_es_base, 448);
661 CHECK_OFFSET(guest_cs_base, 456);
662 CHECK_OFFSET(guest_ss_base, 464);
663 CHECK_OFFSET(guest_ds_base, 472);
664 CHECK_OFFSET(guest_fs_base, 480);
665 CHECK_OFFSET(guest_gs_base, 488);
666 CHECK_OFFSET(guest_ldtr_base, 496);
667 CHECK_OFFSET(guest_tr_base, 504);
668 CHECK_OFFSET(guest_gdtr_base, 512);
669 CHECK_OFFSET(guest_idtr_base, 520);
670 CHECK_OFFSET(guest_dr7, 528);
671 CHECK_OFFSET(guest_rsp, 536);
672 CHECK_OFFSET(guest_rip, 544);
673 CHECK_OFFSET(guest_rflags, 552);
674 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
675 CHECK_OFFSET(guest_sysenter_esp, 568);
676 CHECK_OFFSET(guest_sysenter_eip, 576);
677 CHECK_OFFSET(host_cr0, 584);
678 CHECK_OFFSET(host_cr3, 592);
679 CHECK_OFFSET(host_cr4, 600);
680 CHECK_OFFSET(host_fs_base, 608);
681 CHECK_OFFSET(host_gs_base, 616);
682 CHECK_OFFSET(host_tr_base, 624);
683 CHECK_OFFSET(host_gdtr_base, 632);
684 CHECK_OFFSET(host_idtr_base, 640);
685 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
686 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
687 CHECK_OFFSET(host_rsp, 664);
688 CHECK_OFFSET(host_rip, 672);
689 CHECK_OFFSET(pin_based_vm_exec_control, 744);
690 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
691 CHECK_OFFSET(exception_bitmap, 752);
692 CHECK_OFFSET(page_fault_error_code_mask, 756);
693 CHECK_OFFSET(page_fault_error_code_match, 760);
694 CHECK_OFFSET(cr3_target_count, 764);
695 CHECK_OFFSET(vm_exit_controls, 768);
696 CHECK_OFFSET(vm_exit_msr_store_count, 772);
697 CHECK_OFFSET(vm_exit_msr_load_count, 776);
698 CHECK_OFFSET(vm_entry_controls, 780);
699 CHECK_OFFSET(vm_entry_msr_load_count, 784);
700 CHECK_OFFSET(vm_entry_intr_info_field, 788);
701 CHECK_OFFSET(vm_entry_exception_error_code, 792);
702 CHECK_OFFSET(vm_entry_instruction_len, 796);
703 CHECK_OFFSET(tpr_threshold, 800);
704 CHECK_OFFSET(secondary_vm_exec_control, 804);
705 CHECK_OFFSET(vm_instruction_error, 808);
706 CHECK_OFFSET(vm_exit_reason, 812);
707 CHECK_OFFSET(vm_exit_intr_info, 816);
708 CHECK_OFFSET(vm_exit_intr_error_code, 820);
709 CHECK_OFFSET(idt_vectoring_info_field, 824);
710 CHECK_OFFSET(idt_vectoring_error_code, 828);
711 CHECK_OFFSET(vm_exit_instruction_len, 832);
712 CHECK_OFFSET(vmx_instruction_info, 836);
713 CHECK_OFFSET(guest_es_limit, 840);
714 CHECK_OFFSET(guest_cs_limit, 844);
715 CHECK_OFFSET(guest_ss_limit, 848);
716 CHECK_OFFSET(guest_ds_limit, 852);
717 CHECK_OFFSET(guest_fs_limit, 856);
718 CHECK_OFFSET(guest_gs_limit, 860);
719 CHECK_OFFSET(guest_ldtr_limit, 864);
720 CHECK_OFFSET(guest_tr_limit, 868);
721 CHECK_OFFSET(guest_gdtr_limit, 872);
722 CHECK_OFFSET(guest_idtr_limit, 876);
723 CHECK_OFFSET(guest_es_ar_bytes, 880);
724 CHECK_OFFSET(guest_cs_ar_bytes, 884);
725 CHECK_OFFSET(guest_ss_ar_bytes, 888);
726 CHECK_OFFSET(guest_ds_ar_bytes, 892);
727 CHECK_OFFSET(guest_fs_ar_bytes, 896);
728 CHECK_OFFSET(guest_gs_ar_bytes, 900);
729 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
730 CHECK_OFFSET(guest_tr_ar_bytes, 908);
731 CHECK_OFFSET(guest_interruptibility_info, 912);
732 CHECK_OFFSET(guest_activity_state, 916);
733 CHECK_OFFSET(guest_sysenter_cs, 920);
734 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
735 CHECK_OFFSET(vmx_preemption_timer_value, 928);
736 CHECK_OFFSET(virtual_processor_id, 960);
737 CHECK_OFFSET(posted_intr_nv, 962);
738 CHECK_OFFSET(guest_es_selector, 964);
739 CHECK_OFFSET(guest_cs_selector, 966);
740 CHECK_OFFSET(guest_ss_selector, 968);
741 CHECK_OFFSET(guest_ds_selector, 970);
742 CHECK_OFFSET(guest_fs_selector, 972);
743 CHECK_OFFSET(guest_gs_selector, 974);
744 CHECK_OFFSET(guest_ldtr_selector, 976);
745 CHECK_OFFSET(guest_tr_selector, 978);
746 CHECK_OFFSET(guest_intr_status, 980);
747 CHECK_OFFSET(host_es_selector, 982);
748 CHECK_OFFSET(host_cs_selector, 984);
749 CHECK_OFFSET(host_ss_selector, 986);
750 CHECK_OFFSET(host_ds_selector, 988);
751 CHECK_OFFSET(host_fs_selector, 990);
752 CHECK_OFFSET(host_gs_selector, 992);
753 CHECK_OFFSET(host_tr_selector, 994);
754 CHECK_OFFSET(guest_pml_index, 996);
755}
756
757/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300758 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
759 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
760 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700761 *
762 * IMPORTANT: Changing this value will break save/restore compatibility with
763 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300764 */
765#define VMCS12_REVISION 0x11e57ed0
766
767/*
768 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
769 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
770 * current implementation, 4K are reserved to avoid future complications.
771 */
772#define VMCS12_SIZE 0x1000
773
774/*
Jim Mattson5b157062017-12-22 12:11:12 -0800775 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
776 * supported VMCS12 field encoding.
777 */
778#define VMCS12_MAX_FIELD_INDEX 0x17
779
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100780struct nested_vmx_msrs {
781 /*
782 * We only store the "true" versions of the VMX capability MSRs. We
783 * generate the "non-true" versions by setting the must-be-1 bits
784 * according to the SDM.
785 */
786 u32 procbased_ctls_low;
787 u32 procbased_ctls_high;
788 u32 secondary_ctls_low;
789 u32 secondary_ctls_high;
790 u32 pinbased_ctls_low;
791 u32 pinbased_ctls_high;
792 u32 exit_ctls_low;
793 u32 exit_ctls_high;
794 u32 entry_ctls_low;
795 u32 entry_ctls_high;
796 u32 misc_low;
797 u32 misc_high;
798 u32 ept_caps;
799 u32 vpid_caps;
800 u64 basic;
801 u64 cr0_fixed0;
802 u64 cr0_fixed1;
803 u64 cr4_fixed0;
804 u64 cr4_fixed1;
805 u64 vmcs_enum;
806 u64 vmfunc_controls;
807};
808
Jim Mattson5b157062017-12-22 12:11:12 -0800809/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300810 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
811 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
812 */
813struct nested_vmx {
814 /* Has the level1 guest done vmxon? */
815 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400816 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400817 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300818
819 /* The guest-physical address of the current VMCS L1 keeps for L2 */
820 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700821 /*
822 * Cache of the guest's VMCS, existing outside of guest memory.
823 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700824 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700825 */
826 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300827 /*
Liran Alon61ada742018-06-23 02:35:08 +0300828 * Cache of the guest's shadow VMCS, existing outside of guest
829 * memory. Loaded from guest memory during VM entry. Flushed
830 * to guest memory during VM exit.
831 */
832 struct vmcs12 *cached_shadow_vmcs12;
833 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300834 * Indicates if the shadow vmcs must be updated with the
835 * data hold by vmcs12
836 */
837 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100838 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300839
Sean Christopherson9d6105b22018-09-26 09:23:51 -0700840 /*
841 * vmcs02 has been initialized, i.e. state that is constant for
842 * vmcs02 has been written to the backing VMCS. Initialization
843 * is delayed until L1 actually attempts to run a nested VM.
844 */
845 bool vmcs02_initialized;
846
Jim Mattson8d860bb2018-05-09 16:56:05 -0400847 bool change_vmcs01_virtual_apic_mode;
848
Nadav Har'El644d7112011-05-25 23:12:35 +0300849 /* L2 must run next, and mustn't decide to exit to L1. */
850 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600851
852 struct loaded_vmcs vmcs02;
853
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300854 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600855 * Guest pages referred to in the vmcs02 with host-physical
856 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300857 */
858 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800859 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800860 struct page *pi_desc_page;
861 struct pi_desc *pi_desc;
862 bool pi_pending;
863 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100864
865 struct hrtimer preemption_timer;
866 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200867
868 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
869 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300870 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800871
Wanpeng Li5c614b32015-10-13 09:18:36 -0700872 u16 vpid02;
873 u16 last_vpid;
874
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100875 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200876
877 /* SMM related state */
878 struct {
879 /* in VMX operation on SMM entry? */
880 bool vmxon;
881 /* in guest mode on SMM entry? */
882 bool guest_mode;
883 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300884};
885
Yang Zhang01e439b2013-04-11 19:25:12 +0800886#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800887#define POSTED_INTR_SN 1
888
Yang Zhang01e439b2013-04-11 19:25:12 +0800889/* Posted-Interrupt Descriptor */
890struct pi_desc {
891 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800892 union {
893 struct {
894 /* bit 256 - Outstanding Notification */
895 u16 on : 1,
896 /* bit 257 - Suppress Notification */
897 sn : 1,
898 /* bit 271:258 - Reserved */
899 rsvd_1 : 14;
900 /* bit 279:272 - Notification Vector */
901 u8 nv;
902 /* bit 287:280 - Reserved */
903 u8 rsvd_2;
904 /* bit 319:288 - Notification Destination */
905 u32 ndst;
906 };
907 u64 control;
908 };
909 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800910} __aligned(64);
911
Yang Zhanga20ed542013-04-11 19:25:15 +0800912static bool pi_test_and_set_on(struct pi_desc *pi_desc)
913{
914 return test_and_set_bit(POSTED_INTR_ON,
915 (unsigned long *)&pi_desc->control);
916}
917
918static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
919{
920 return test_and_clear_bit(POSTED_INTR_ON,
921 (unsigned long *)&pi_desc->control);
922}
923
924static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
925{
926 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
927}
928
Feng Wuebbfc762015-09-18 22:29:46 +0800929static inline void pi_clear_sn(struct pi_desc *pi_desc)
930{
931 return clear_bit(POSTED_INTR_SN,
932 (unsigned long *)&pi_desc->control);
933}
934
935static inline void pi_set_sn(struct pi_desc *pi_desc)
936{
937 return set_bit(POSTED_INTR_SN,
938 (unsigned long *)&pi_desc->control);
939}
940
Paolo Bonziniad361092016-09-20 16:15:05 +0200941static inline void pi_clear_on(struct pi_desc *pi_desc)
942{
943 clear_bit(POSTED_INTR_ON,
944 (unsigned long *)&pi_desc->control);
945}
946
Feng Wuebbfc762015-09-18 22:29:46 +0800947static inline int pi_test_on(struct pi_desc *pi_desc)
948{
949 return test_bit(POSTED_INTR_ON,
950 (unsigned long *)&pi_desc->control);
951}
952
953static inline int pi_test_sn(struct pi_desc *pi_desc)
954{
955 return test_bit(POSTED_INTR_SN,
956 (unsigned long *)&pi_desc->control);
957}
958
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400959struct vmx_msrs {
960 unsigned int nr;
961 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
962};
963
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400964struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000965 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300966 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300967 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100968 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300969 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200970 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200971 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300972 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400973 int nmsrs;
974 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800975 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400976#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300977 u64 msr_host_kernel_gs_base;
978 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100980
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100981 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100982 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100983
Gleb Natapov2961e8762013-11-25 15:37:13 +0200984 u32 vm_entry_controls_shadow;
985 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200986 u32 secondary_exec_control;
987
Nadav Har'Eld462b812011-05-24 15:26:10 +0300988 /*
989 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
990 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700991 * guest (L2), it points to a different VMCS. loaded_cpu_state points
992 * to the VMCS whose state is loaded into the CPU registers that only
993 * need to be switched when transitioning to/from the kernel; a NULL
994 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300995 */
996 struct loaded_vmcs vmcs01;
997 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700998 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300999 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +03001000 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001001 struct vmx_msrs guest;
1002 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001003 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001004
Avi Kivity9c8cba32007-11-22 11:42:59 +02001005 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001006 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001007 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001008 struct kvm_segment segs[8];
1009 } rmode;
1010 struct {
1011 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001012 struct kvm_save_segment {
1013 u16 selector;
1014 unsigned long base;
1015 u32 limit;
1016 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001017 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001018 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001019 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001020 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001021
Andi Kleena0861c02009-06-08 17:37:09 +08001022 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001023
Yang Zhang01e439b2013-04-11 19:25:12 +08001024 /* Posted interrupt descriptor */
1025 struct pi_desc pi_desc;
1026
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001027 /* Support for a guest hypervisor (nested VMX) */
1028 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001029
1030 /* Dynamic PLE window. */
1031 int ple_window;
1032 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001033
Sean Christophersond264ee02018-08-27 15:21:12 -07001034 bool req_immediate_exit;
1035
Kai Huang843e4332015-01-28 10:54:28 +08001036 /* Support for PML */
1037#define PML_ENTITY_NUM 512
1038 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001039
Yunhong Jiang64672c92016-06-13 14:19:59 -07001040 /* apic deadline value in host tsc */
1041 u64 hv_deadline_tsc;
1042
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001043 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001044
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001045 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001046
Wanpeng Li74c55932017-11-29 01:31:20 -08001047 unsigned long host_debugctlmsr;
1048
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001049 /*
1050 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1051 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1052 * in msr_ia32_feature_control_valid_bits.
1053 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001054 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001055 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001056 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001057};
1058
Avi Kivity2fb92db2011-04-27 19:42:18 +03001059enum segment_cache_field {
1060 SEG_FIELD_SEL = 0,
1061 SEG_FIELD_BASE = 1,
1062 SEG_FIELD_LIMIT = 2,
1063 SEG_FIELD_AR = 3,
1064
1065 SEG_FIELD_NR = 4
1066};
1067
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001068static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1069{
1070 return container_of(kvm, struct kvm_vmx, kvm);
1071}
1072
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001073static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1074{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001075 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001076}
1077
Feng Wuefc64402015-09-18 22:29:51 +08001078static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1079{
1080 return &(to_vmx(vcpu)->pi_desc);
1081}
1082
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001083#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001084#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001085#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1086#define FIELD64(number, name) \
1087 FIELD(number, name), \
1088 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001089
Abel Gordon4607c2d2013-04-18 14:35:55 +03001090
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001091static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001092#define SHADOW_FIELD_RO(x) x,
1093#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001094};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001095static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001096 ARRAY_SIZE(shadow_read_only_fields);
1097
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001098static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001099#define SHADOW_FIELD_RW(x) x,
1100#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001101};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001102static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001103 ARRAY_SIZE(shadow_read_write_fields);
1104
Mathias Krause772e0312012-08-30 01:30:19 +02001105static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001106 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001107 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001108 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1109 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1110 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1111 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1112 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1113 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1114 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1115 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001116 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001117 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001118 FIELD(HOST_ES_SELECTOR, host_es_selector),
1119 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1120 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1121 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1122 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1123 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1124 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1125 FIELD64(IO_BITMAP_A, io_bitmap_a),
1126 FIELD64(IO_BITMAP_B, io_bitmap_b),
1127 FIELD64(MSR_BITMAP, msr_bitmap),
1128 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1129 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1130 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001131 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001132 FIELD64(TSC_OFFSET, tsc_offset),
1133 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1134 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001135 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001136 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001137 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001138 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1139 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1140 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1141 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001142 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001143 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1144 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001145 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001146 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1147 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1148 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1149 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1150 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1151 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1152 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1153 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1154 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1155 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001156 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001157 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1158 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1159 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1160 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1161 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1162 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1163 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1164 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1165 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1166 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1167 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1168 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1169 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1170 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1171 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1172 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1173 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1174 FIELD(TPR_THRESHOLD, tpr_threshold),
1175 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1176 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1177 FIELD(VM_EXIT_REASON, vm_exit_reason),
1178 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1179 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1180 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1181 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1182 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1183 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1184 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1185 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1186 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1187 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1188 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1189 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1190 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1191 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1192 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1193 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1194 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1195 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1196 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1197 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1198 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1199 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1200 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1201 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1202 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1203 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1204 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1205 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001206 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001207 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1208 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1209 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1210 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1211 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1212 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1213 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1214 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1215 FIELD(EXIT_QUALIFICATION, exit_qualification),
1216 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1217 FIELD(GUEST_CR0, guest_cr0),
1218 FIELD(GUEST_CR3, guest_cr3),
1219 FIELD(GUEST_CR4, guest_cr4),
1220 FIELD(GUEST_ES_BASE, guest_es_base),
1221 FIELD(GUEST_CS_BASE, guest_cs_base),
1222 FIELD(GUEST_SS_BASE, guest_ss_base),
1223 FIELD(GUEST_DS_BASE, guest_ds_base),
1224 FIELD(GUEST_FS_BASE, guest_fs_base),
1225 FIELD(GUEST_GS_BASE, guest_gs_base),
1226 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1227 FIELD(GUEST_TR_BASE, guest_tr_base),
1228 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1229 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1230 FIELD(GUEST_DR7, guest_dr7),
1231 FIELD(GUEST_RSP, guest_rsp),
1232 FIELD(GUEST_RIP, guest_rip),
1233 FIELD(GUEST_RFLAGS, guest_rflags),
1234 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1235 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1236 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1237 FIELD(HOST_CR0, host_cr0),
1238 FIELD(HOST_CR3, host_cr3),
1239 FIELD(HOST_CR4, host_cr4),
1240 FIELD(HOST_FS_BASE, host_fs_base),
1241 FIELD(HOST_GS_BASE, host_gs_base),
1242 FIELD(HOST_TR_BASE, host_tr_base),
1243 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1244 FIELD(HOST_IDTR_BASE, host_idtr_base),
1245 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1246 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1247 FIELD(HOST_RSP, host_rsp),
1248 FIELD(HOST_RIP, host_rip),
1249};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001250
1251static inline short vmcs_field_to_offset(unsigned long field)
1252{
Dan Williams085331d2018-01-31 17:47:03 -08001253 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1254 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001255 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001256
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001257 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001258 return -ENOENT;
1259
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001260 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001261 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001262 return -ENOENT;
1263
Linus Torvalds15303ba2018-02-10 13:16:35 -08001264 index = array_index_nospec(index, size);
1265 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001266 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001267 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001268 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001269}
1270
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001271static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1272{
David Matlack4f2777b2016-07-13 17:16:37 -07001273 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001274}
1275
Liran Alon61ada742018-06-23 02:35:08 +03001276static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1277{
1278 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1279}
1280
Peter Feiner995f00a2017-06-30 17:26:32 -07001281static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001282static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001283static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001284static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001285static void vmx_set_segment(struct kvm_vcpu *vcpu,
1286 struct kvm_segment *var, int seg);
1287static void vmx_get_segment(struct kvm_vcpu *vcpu,
1288 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001289static bool guest_state_valid(struct kvm_vcpu *vcpu);
1290static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001291static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001292static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1293static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1294static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1295 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001296static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001297static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1298 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001299
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1301static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001302/*
1303 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1304 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1305 */
1306static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307
Feng Wubf9f6ac2015-09-18 22:29:55 +08001308/*
1309 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1310 * can find which vCPU should be waken up.
1311 */
1312static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1313static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1314
Radim Krčmář23611332016-09-29 22:41:33 +02001315enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001316 VMX_VMREAD_BITMAP,
1317 VMX_VMWRITE_BITMAP,
1318 VMX_BITMAP_NR
1319};
1320
1321static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1322
Radim Krčmář23611332016-09-29 22:41:33 +02001323#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1324#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001325
Avi Kivity110312c2010-12-21 12:54:20 +02001326static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001327static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001328
Sheng Yang2384d2b2008-01-17 15:14:33 +08001329static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1330static DEFINE_SPINLOCK(vmx_vpid_lock);
1331
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333 int size;
1334 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001335 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001337 u32 pin_based_exec_ctrl;
1338 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001339 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001340 u32 vmexit_ctrl;
1341 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001342 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001343} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344
Hannes Ederefff9e52008-11-28 17:02:06 +01001345static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001346 u32 ept;
1347 u32 vpid;
1348} vmx_capability;
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350#define VMX_SEGMENT_FIELD(seg) \
1351 [VCPU_SREG_##seg] = { \
1352 .selector = GUEST_##seg##_SELECTOR, \
1353 .base = GUEST_##seg##_BASE, \
1354 .limit = GUEST_##seg##_LIMIT, \
1355 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1356 }
1357
Mathias Krause772e0312012-08-30 01:30:19 +02001358static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 unsigned selector;
1360 unsigned base;
1361 unsigned limit;
1362 unsigned ar_bytes;
1363} kvm_vmx_segment_fields[] = {
1364 VMX_SEGMENT_FIELD(CS),
1365 VMX_SEGMENT_FIELD(DS),
1366 VMX_SEGMENT_FIELD(ES),
1367 VMX_SEGMENT_FIELD(FS),
1368 VMX_SEGMENT_FIELD(GS),
1369 VMX_SEGMENT_FIELD(SS),
1370 VMX_SEGMENT_FIELD(TR),
1371 VMX_SEGMENT_FIELD(LDTR),
1372};
1373
Avi Kivity26bb0982009-09-07 11:14:12 +03001374static u64 host_efer;
1375
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001376static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1377
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001378/*
Brian Gerst8c065852010-07-17 09:03:26 -04001379 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001380 * away by decrementing the array size.
1381 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001383#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001384 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001386 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001389DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1390
1391#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1392
1393#define KVM_EVMCS_VERSION 1
1394
1395#if IS_ENABLED(CONFIG_HYPERV)
1396static bool __read_mostly enlightened_vmcs = true;
1397module_param(enlightened_vmcs, bool, 0444);
1398
1399static inline void evmcs_write64(unsigned long field, u64 value)
1400{
1401 u16 clean_field;
1402 int offset = get_evmcs_offset(field, &clean_field);
1403
1404 if (offset < 0)
1405 return;
1406
1407 *(u64 *)((char *)current_evmcs + offset) = value;
1408
1409 current_evmcs->hv_clean_fields &= ~clean_field;
1410}
1411
1412static inline void evmcs_write32(unsigned long field, u32 value)
1413{
1414 u16 clean_field;
1415 int offset = get_evmcs_offset(field, &clean_field);
1416
1417 if (offset < 0)
1418 return;
1419
1420 *(u32 *)((char *)current_evmcs + offset) = value;
1421 current_evmcs->hv_clean_fields &= ~clean_field;
1422}
1423
1424static inline void evmcs_write16(unsigned long field, u16 value)
1425{
1426 u16 clean_field;
1427 int offset = get_evmcs_offset(field, &clean_field);
1428
1429 if (offset < 0)
1430 return;
1431
1432 *(u16 *)((char *)current_evmcs + offset) = value;
1433 current_evmcs->hv_clean_fields &= ~clean_field;
1434}
1435
1436static inline u64 evmcs_read64(unsigned long field)
1437{
1438 int offset = get_evmcs_offset(field, NULL);
1439
1440 if (offset < 0)
1441 return 0;
1442
1443 return *(u64 *)((char *)current_evmcs + offset);
1444}
1445
1446static inline u32 evmcs_read32(unsigned long field)
1447{
1448 int offset = get_evmcs_offset(field, NULL);
1449
1450 if (offset < 0)
1451 return 0;
1452
1453 return *(u32 *)((char *)current_evmcs + offset);
1454}
1455
1456static inline u16 evmcs_read16(unsigned long field)
1457{
1458 int offset = get_evmcs_offset(field, NULL);
1459
1460 if (offset < 0)
1461 return 0;
1462
1463 return *(u16 *)((char *)current_evmcs + offset);
1464}
1465
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001466static inline void evmcs_touch_msr_bitmap(void)
1467{
1468 if (unlikely(!current_evmcs))
1469 return;
1470
1471 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1472 current_evmcs->hv_clean_fields &=
1473 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1474}
1475
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001476static void evmcs_load(u64 phys_addr)
1477{
1478 struct hv_vp_assist_page *vp_ap =
1479 hv_get_vp_assist_page(smp_processor_id());
1480
1481 vp_ap->current_nested_vmcs = phys_addr;
1482 vp_ap->enlighten_vmentry = 1;
1483}
1484
1485static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1486{
1487 /*
1488 * Enlightened VMCSv1 doesn't support these:
1489 *
1490 * POSTED_INTR_NV = 0x00000002,
1491 * GUEST_INTR_STATUS = 0x00000810,
1492 * APIC_ACCESS_ADDR = 0x00002014,
1493 * POSTED_INTR_DESC_ADDR = 0x00002016,
1494 * EOI_EXIT_BITMAP0 = 0x0000201c,
1495 * EOI_EXIT_BITMAP1 = 0x0000201e,
1496 * EOI_EXIT_BITMAP2 = 0x00002020,
1497 * EOI_EXIT_BITMAP3 = 0x00002022,
1498 */
1499 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1501 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1502 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1503 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1504 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1505 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1506
1507 /*
1508 * GUEST_PML_INDEX = 0x00000812,
1509 * PML_ADDRESS = 0x0000200e,
1510 */
1511 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1512
1513 /* VM_FUNCTION_CONTROL = 0x00002018, */
1514 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1515
1516 /*
1517 * EPTP_LIST_ADDRESS = 0x00002024,
1518 * VMREAD_BITMAP = 0x00002026,
1519 * VMWRITE_BITMAP = 0x00002028,
1520 */
1521 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1522
1523 /*
1524 * TSC_MULTIPLIER = 0x00002032,
1525 */
1526 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1527
1528 /*
1529 * PLE_GAP = 0x00004020,
1530 * PLE_WINDOW = 0x00004022,
1531 */
1532 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1533
1534 /*
1535 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1536 */
1537 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1538
1539 /*
1540 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1541 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1542 */
1543 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1544 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1545
1546 /*
1547 * Currently unsupported in KVM:
1548 * GUEST_IA32_RTIT_CTL = 0x00002814,
1549 */
1550}
Tianyu Lan877ad952018-07-19 08:40:23 +00001551
1552/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1553static void check_ept_pointer_match(struct kvm *kvm)
1554{
1555 struct kvm_vcpu *vcpu;
1556 u64 tmp_eptp = INVALID_PAGE;
1557 int i;
1558
1559 kvm_for_each_vcpu(i, vcpu, kvm) {
1560 if (!VALID_PAGE(tmp_eptp)) {
1561 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1562 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1563 to_kvm_vmx(kvm)->ept_pointers_match
1564 = EPT_POINTERS_MISMATCH;
1565 return;
1566 }
1567 }
1568
1569 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1570}
1571
1572static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1573{
1574 int ret;
1575
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
1581 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1582 ret = -ENOTSUPP;
1583 goto out;
1584 }
1585
1586 ret = hyperv_flush_guest_mapping(
1587 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1588
1589out:
1590 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1591 return ret;
1592}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001593#else /* !IS_ENABLED(CONFIG_HYPERV) */
1594static inline void evmcs_write64(unsigned long field, u64 value) {}
1595static inline void evmcs_write32(unsigned long field, u32 value) {}
1596static inline void evmcs_write16(unsigned long field, u16 value) {}
1597static inline u64 evmcs_read64(unsigned long field) { return 0; }
1598static inline u32 evmcs_read32(unsigned long field) { return 0; }
1599static inline u16 evmcs_read16(unsigned long field) { return 0; }
1600static inline void evmcs_load(u64 phys_addr) {}
1601static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001602static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001603#endif /* IS_ENABLED(CONFIG_HYPERV) */
1604
Jan Kiszka5bb16012016-02-09 20:14:21 +01001605static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606{
1607 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1608 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001609 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1610}
1611
Jan Kiszka6f054852016-02-09 20:15:18 +01001612static inline bool is_debug(u32 intr_info)
1613{
1614 return is_exception_n(intr_info, DB_VECTOR);
1615}
1616
1617static inline bool is_breakpoint(u32 intr_info)
1618{
1619 return is_exception_n(intr_info, BP_VECTOR);
1620}
1621
Jan Kiszka5bb16012016-02-09 20:14:21 +01001622static inline bool is_page_fault(u32 intr_info)
1623{
1624 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625}
1626
Gui Jianfeng31299942010-03-15 17:29:09 +08001627static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001628{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001629 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001630}
1631
Liran Alon9e869482018-03-12 13:12:51 +02001632static inline bool is_gp_fault(u32 intr_info)
1633{
1634 return is_exception_n(intr_info, GP_VECTOR);
1635}
1636
Gui Jianfeng31299942010-03-15 17:29:09 +08001637static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001638{
1639 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1640 INTR_INFO_VALID_MASK)) ==
1641 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1642}
1643
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001644/* Undocumented: icebp/int1 */
1645static inline bool is_icebp(u32 intr_info)
1646{
1647 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1648 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1649}
1650
Gui Jianfeng31299942010-03-15 17:29:09 +08001651static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001652{
Sheng Yang04547152009-04-01 15:52:31 +08001653 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001654}
1655
Gui Jianfeng31299942010-03-15 17:29:09 +08001656static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001657{
Sheng Yang04547152009-04-01 15:52:31 +08001658 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001659}
1660
Paolo Bonzini35754c92015-07-29 12:05:37 +02001661static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001662{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001663 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001664}
1665
Gui Jianfeng31299942010-03-15 17:29:09 +08001666static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001667{
Sheng Yang04547152009-04-01 15:52:31 +08001668 return vmcs_config.cpu_based_exec_ctrl &
1669 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001670}
1671
Avi Kivity774ead32007-12-26 13:57:04 +02001672static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001673{
Sheng Yang04547152009-04-01 15:52:31 +08001674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1676}
1677
Yang Zhang8d146952013-01-25 10:18:50 +08001678static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1679{
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1682}
1683
Yang Zhang83d4c282013-01-25 10:18:49 +08001684static inline bool cpu_has_vmx_apic_register_virt(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1688}
1689
Yang Zhangc7c9c562013-01-25 10:18:51 +08001690static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1694}
1695
Sean Christopherson0b665d32018-08-14 09:33:34 -07001696static inline bool cpu_has_vmx_encls_vmexit(void)
1697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_ENCLS_EXITING;
1700}
1701
Yunhong Jiang64672c92016-06-13 14:19:59 -07001702/*
1703 * Comment's format: document - errata name - stepping - processor name.
1704 * Refer from
1705 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1706 */
1707static u32 vmx_preemption_cpu_tfms[] = {
1708/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17090x000206E6,
1710/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1711/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1712/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17130x00020652,
1714/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17150x00020655,
1716/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1717/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1718/*
1719 * 320767.pdf - AAP86 - B1 -
1720 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1721 */
17220x000106E5,
1723/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17240x000106A0,
1725/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17260x000106A1,
1727/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17280x000106A4,
1729 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1730 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1731 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17320x000106A5,
1733};
1734
1735static inline bool cpu_has_broken_vmx_preemption_timer(void)
1736{
1737 u32 eax = cpuid_eax(0x00000001), i;
1738
1739 /* Clear the reserved bits */
1740 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001741 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001742 if (eax == vmx_preemption_cpu_tfms[i])
1743 return true;
1744
1745 return false;
1746}
1747
1748static inline bool cpu_has_vmx_preemption_timer(void)
1749{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001750 return vmcs_config.pin_based_exec_ctrl &
1751 PIN_BASED_VMX_PREEMPTION_TIMER;
1752}
1753
Yang Zhang01e439b2013-04-11 19:25:12 +08001754static inline bool cpu_has_vmx_posted_intr(void)
1755{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001756 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1757 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001758}
1759
1760static inline bool cpu_has_vmx_apicv(void)
1761{
1762 return cpu_has_vmx_apic_register_virt() &&
1763 cpu_has_vmx_virtual_intr_delivery() &&
1764 cpu_has_vmx_posted_intr();
1765}
1766
Sheng Yang04547152009-04-01 15:52:31 +08001767static inline bool cpu_has_vmx_flexpriority(void)
1768{
1769 return cpu_has_vmx_tpr_shadow() &&
1770 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001771}
1772
Marcelo Tosattie7997942009-06-11 12:07:40 -03001773static inline bool cpu_has_vmx_ept_execute_only(void)
1774{
Gui Jianfeng31299942010-03-15 17:29:09 +08001775 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001776}
1777
Marcelo Tosattie7997942009-06-11 12:07:40 -03001778static inline bool cpu_has_vmx_ept_2m_page(void)
1779{
Gui Jianfeng31299942010-03-15 17:29:09 +08001780 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001781}
1782
Sheng Yang878403b2010-01-05 19:02:29 +08001783static inline bool cpu_has_vmx_ept_1g_page(void)
1784{
Gui Jianfeng31299942010-03-15 17:29:09 +08001785 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001786}
1787
Sheng Yang4bc9b982010-06-02 14:05:24 +08001788static inline bool cpu_has_vmx_ept_4levels(void)
1789{
1790 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1791}
1792
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001793static inline bool cpu_has_vmx_ept_mt_wb(void)
1794{
1795 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1796}
1797
Yu Zhang855feb62017-08-24 20:27:55 +08001798static inline bool cpu_has_vmx_ept_5levels(void)
1799{
1800 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1801}
1802
Xudong Hao83c3a332012-05-28 19:33:35 +08001803static inline bool cpu_has_vmx_ept_ad_bits(void)
1804{
1805 return vmx_capability.ept & VMX_EPT_AD_BIT;
1806}
1807
Gui Jianfeng31299942010-03-15 17:29:09 +08001808static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001809{
Gui Jianfeng31299942010-03-15 17:29:09 +08001810 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001811}
1812
Gui Jianfeng31299942010-03-15 17:29:09 +08001813static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001814{
Gui Jianfeng31299942010-03-15 17:29:09 +08001815 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001816}
1817
Liran Aloncd9a4912018-05-22 17:16:15 +03001818static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1819{
1820 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1821}
1822
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001823static inline bool cpu_has_vmx_invvpid_single(void)
1824{
1825 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1826}
1827
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001828static inline bool cpu_has_vmx_invvpid_global(void)
1829{
1830 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1831}
1832
Wanpeng Li08d839c2017-03-23 05:30:08 -07001833static inline bool cpu_has_vmx_invvpid(void)
1834{
1835 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1836}
1837
Gui Jianfeng31299942010-03-15 17:29:09 +08001838static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001839{
Sheng Yang04547152009-04-01 15:52:31 +08001840 return vmcs_config.cpu_based_2nd_exec_ctrl &
1841 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001842}
1843
Gui Jianfeng31299942010-03-15 17:29:09 +08001844static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001845{
1846 return vmcs_config.cpu_based_2nd_exec_ctrl &
1847 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1848}
1849
Gui Jianfeng31299942010-03-15 17:29:09 +08001850static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001851{
1852 return vmcs_config.cpu_based_2nd_exec_ctrl &
1853 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1854}
1855
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001856static inline bool cpu_has_vmx_basic_inout(void)
1857{
1858 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1859}
1860
Paolo Bonzini35754c92015-07-29 12:05:37 +02001861static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001862{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001863 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001864}
1865
Gui Jianfeng31299942010-03-15 17:29:09 +08001866static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001867{
Sheng Yang04547152009-04-01 15:52:31 +08001868 return vmcs_config.cpu_based_2nd_exec_ctrl &
1869 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001870}
1871
Gui Jianfeng31299942010-03-15 17:29:09 +08001872static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001873{
1874 return vmcs_config.cpu_based_2nd_exec_ctrl &
1875 SECONDARY_EXEC_RDTSCP;
1876}
1877
Mao, Junjiead756a12012-07-02 01:18:48 +00001878static inline bool cpu_has_vmx_invpcid(void)
1879{
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_ENABLE_INVPCID;
1882}
1883
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001884static inline bool cpu_has_virtual_nmis(void)
1885{
1886 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1887}
1888
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001889static inline bool cpu_has_vmx_wbinvd_exit(void)
1890{
1891 return vmcs_config.cpu_based_2nd_exec_ctrl &
1892 SECONDARY_EXEC_WBINVD_EXITING;
1893}
1894
Abel Gordonabc4fc52013-04-18 14:35:25 +03001895static inline bool cpu_has_vmx_shadow_vmcs(void)
1896{
1897 u64 vmx_msr;
1898 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1899 /* check if the cpu supports writing r/o exit information fields */
1900 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1901 return false;
1902
1903 return vmcs_config.cpu_based_2nd_exec_ctrl &
1904 SECONDARY_EXEC_SHADOW_VMCS;
1905}
1906
Kai Huang843e4332015-01-28 10:54:28 +08001907static inline bool cpu_has_vmx_pml(void)
1908{
1909 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1910}
1911
Haozhong Zhang64903d62015-10-20 15:39:09 +08001912static inline bool cpu_has_vmx_tsc_scaling(void)
1913{
1914 return vmcs_config.cpu_based_2nd_exec_ctrl &
1915 SECONDARY_EXEC_TSC_SCALING;
1916}
1917
Bandan Das2a499e42017-08-03 15:54:41 -04001918static inline bool cpu_has_vmx_vmfunc(void)
1919{
1920 return vmcs_config.cpu_based_2nd_exec_ctrl &
1921 SECONDARY_EXEC_ENABLE_VMFUNC;
1922}
1923
Sean Christopherson64f7a112018-04-30 10:01:06 -07001924static bool vmx_umip_emulated(void)
1925{
1926 return vmcs_config.cpu_based_2nd_exec_ctrl &
1927 SECONDARY_EXEC_DESC;
1928}
1929
Sheng Yang04547152009-04-01 15:52:31 +08001930static inline bool report_flexpriority(void)
1931{
1932 return flexpriority_enabled;
1933}
1934
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001935static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1936{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001937 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001938}
1939
Jim Mattsonf4160e42018-05-29 09:11:33 -07001940/*
1941 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1942 * to modify any valid field of the VMCS, or are the VM-exit
1943 * information fields read-only?
1944 */
1945static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1946{
1947 return to_vmx(vcpu)->nested.msrs.misc_low &
1948 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1949}
1950
Marc Orr04473782018-06-20 17:21:29 -07001951static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1952{
1953 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1954}
1955
1956static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1957{
1958 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1959 CPU_BASED_MONITOR_TRAP_FLAG;
1960}
1961
Liran Alonfa97d7d2018-07-18 14:07:59 +02001962static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1963{
1964 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1965 SECONDARY_EXEC_SHADOW_VMCS;
1966}
1967
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001968static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1969{
1970 return vmcs12->cpu_based_vm_exec_control & bit;
1971}
1972
1973static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1974{
1975 return (vmcs12->cpu_based_vm_exec_control &
1976 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1977 (vmcs12->secondary_vm_exec_control & bit);
1978}
1979
Jan Kiszkaf4124502014-03-07 20:03:13 +01001980static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1981{
1982 return vmcs12->pin_based_vm_exec_control &
1983 PIN_BASED_VMX_PREEMPTION_TIMER;
1984}
1985
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001986static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1987{
1988 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1989}
1990
1991static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1992{
1993 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1994}
1995
Nadav Har'El155a97a2013-08-05 11:07:16 +03001996static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1997{
1998 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1999}
2000
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002001static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2002{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002003 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002004}
2005
Bandan Dasc5f983f2017-05-05 15:25:14 -04002006static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2007{
2008 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2009}
2010
Wincy Vanf2b93282015-02-03 23:56:03 +08002011static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2012{
2013 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2014}
2015
Wanpeng Li5c614b32015-10-13 09:18:36 -07002016static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2017{
2018 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2019}
2020
Wincy Van82f0dd42015-02-03 23:57:18 +08002021static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2022{
2023 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2024}
2025
Wincy Van608406e2015-02-03 23:57:51 +08002026static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2027{
2028 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2029}
2030
Wincy Van705699a2015-02-03 23:58:17 +08002031static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2032{
2033 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2034}
2035
Bandan Das27c42a12017-08-03 15:54:42 -04002036static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2037{
2038 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2039}
2040
Bandan Das41ab9372017-08-03 15:54:43 -04002041static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2042{
2043 return nested_cpu_has_vmfunc(vmcs12) &&
2044 (vmcs12->vm_function_control &
2045 VMX_VMFUNC_EPTP_SWITCHING);
2046}
2047
Liran Alonf792d272018-06-23 02:35:05 +03002048static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2049{
2050 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2051}
2052
Jim Mattsonef85b672016-12-12 11:01:37 -08002053static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002054{
2055 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002056 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002057}
2058
Jan Kiszka533558b2014-01-04 18:47:20 +01002059static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2060 u32 exit_intr_info,
2061 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002062
Rusty Russell8b9cf982007-07-30 16:31:43 +10002063static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002064{
2065 int i;
2066
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002067 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002068 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002069 return i;
2070 return -1;
2071}
2072
Sheng Yang2384d2b2008-01-17 15:14:33 +08002073static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2074{
2075 struct {
2076 u64 vpid : 16;
2077 u64 rsvd : 48;
2078 u64 gva;
2079 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002080 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002081
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002082 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2083 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2084 : "memory");
2085 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002086}
2087
Sheng Yang14394422008-04-28 12:24:45 +08002088static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2089{
2090 struct {
2091 u64 eptp, gpa;
2092 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002093 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002094
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002095 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2096 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2097 : "memory");
2098 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002099}
2100
Avi Kivity26bb0982009-09-07 11:14:12 +03002101static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002102{
2103 int i;
2104
Rusty Russell8b9cf982007-07-30 16:31:43 +10002105 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002106 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002107 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002108 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002109}
2110
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111static void vmcs_clear(struct vmcs *vmcs)
2112{
2113 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002114 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002116 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2117 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2118 : "memory");
2119 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2121 vmcs, phys_addr);
2122}
2123
Nadav Har'Eld462b812011-05-24 15:26:10 +03002124static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2125{
2126 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002127 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2128 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002129 loaded_vmcs->cpu = -1;
2130 loaded_vmcs->launched = 0;
2131}
2132
Dongxiao Xu7725b892010-05-11 18:29:38 +08002133static void vmcs_load(struct vmcs *vmcs)
2134{
2135 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002136 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002137
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002138 if (static_branch_unlikely(&enable_evmcs))
2139 return evmcs_load(phys_addr);
2140
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002141 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2142 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2143 : "memory");
2144 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002145 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002146 vmcs, phys_addr);
2147}
2148
Dave Young2965faa2015-09-09 15:38:55 -07002149#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002150/*
2151 * This bitmap is used to indicate whether the vmclear
2152 * operation is enabled on all cpus. All disabled by
2153 * default.
2154 */
2155static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2156
2157static inline void crash_enable_local_vmclear(int cpu)
2158{
2159 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2160}
2161
2162static inline void crash_disable_local_vmclear(int cpu)
2163{
2164 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2165}
2166
2167static inline int crash_local_vmclear_enabled(int cpu)
2168{
2169 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2170}
2171
2172static void crash_vmclear_local_loaded_vmcss(void)
2173{
2174 int cpu = raw_smp_processor_id();
2175 struct loaded_vmcs *v;
2176
2177 if (!crash_local_vmclear_enabled(cpu))
2178 return;
2179
2180 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2181 loaded_vmcss_on_cpu_link)
2182 vmcs_clear(v->vmcs);
2183}
2184#else
2185static inline void crash_enable_local_vmclear(int cpu) { }
2186static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002187#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002188
Nadav Har'Eld462b812011-05-24 15:26:10 +03002189static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002191 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002192 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193
Nadav Har'Eld462b812011-05-24 15:26:10 +03002194 if (loaded_vmcs->cpu != cpu)
2195 return; /* vcpu migration can race with cpu offline */
2196 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002198 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002199 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002200
2201 /*
2202 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2203 * is before setting loaded_vmcs->vcpu to -1 which is done in
2204 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2205 * then adds the vmcs into percpu list before it is deleted.
2206 */
2207 smp_wmb();
2208
Nadav Har'Eld462b812011-05-24 15:26:10 +03002209 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002210 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211}
2212
Nadav Har'Eld462b812011-05-24 15:26:10 +03002213static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002214{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002215 int cpu = loaded_vmcs->cpu;
2216
2217 if (cpu != -1)
2218 smp_call_function_single(cpu,
2219 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002220}
2221
Junaid Shahidfaff8752018-06-29 13:10:05 -07002222static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2223{
2224 if (vpid == 0)
2225 return true;
2226
2227 if (cpu_has_vmx_invvpid_individual_addr()) {
2228 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2229 return true;
2230 }
2231
2232 return false;
2233}
2234
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002235static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002236{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002237 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002238 return;
2239
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002240 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002241 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002242}
2243
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002244static inline void vpid_sync_vcpu_global(void)
2245{
2246 if (cpu_has_vmx_invvpid_global())
2247 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2248}
2249
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002250static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002251{
2252 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002253 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002254 else
2255 vpid_sync_vcpu_global();
2256}
2257
Sheng Yang14394422008-04-28 12:24:45 +08002258static inline void ept_sync_global(void)
2259{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002260 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002261}
2262
2263static inline void ept_sync_context(u64 eptp)
2264{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002265 if (cpu_has_vmx_invept_context())
2266 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2267 else
2268 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002269}
2270
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002271static __always_inline void vmcs_check16(unsigned long field)
2272{
2273 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2274 "16-bit accessor invalid for 64-bit field");
2275 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2276 "16-bit accessor invalid for 64-bit high field");
2277 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2278 "16-bit accessor invalid for 32-bit high field");
2279 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2280 "16-bit accessor invalid for natural width field");
2281}
2282
2283static __always_inline void vmcs_check32(unsigned long field)
2284{
2285 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2286 "32-bit accessor invalid for 16-bit field");
2287 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2288 "32-bit accessor invalid for natural width field");
2289}
2290
2291static __always_inline void vmcs_check64(unsigned long field)
2292{
2293 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2294 "64-bit accessor invalid for 16-bit field");
2295 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2296 "64-bit accessor invalid for 64-bit high field");
2297 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2298 "64-bit accessor invalid for 32-bit field");
2299 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2300 "64-bit accessor invalid for natural width field");
2301}
2302
2303static __always_inline void vmcs_checkl(unsigned long field)
2304{
2305 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2306 "Natural width accessor invalid for 16-bit field");
2307 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2308 "Natural width accessor invalid for 64-bit field");
2309 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2310 "Natural width accessor invalid for 64-bit high field");
2311 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2312 "Natural width accessor invalid for 32-bit field");
2313}
2314
2315static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316{
Avi Kivity5e520e62011-05-15 10:13:12 -04002317 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318
Avi Kivity5e520e62011-05-15 10:13:12 -04002319 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2320 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 return value;
2322}
2323
Avi Kivity96304212011-05-15 10:13:13 -04002324static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002326 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002327 if (static_branch_unlikely(&enable_evmcs))
2328 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002329 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
2331
Avi Kivity96304212011-05-15 10:13:13 -04002332static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002334 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002335 if (static_branch_unlikely(&enable_evmcs))
2336 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002337 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338}
2339
Avi Kivity96304212011-05-15 10:13:13 -04002340static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002342 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002343 if (static_branch_unlikely(&enable_evmcs))
2344 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002345#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002346 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002348 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349#endif
2350}
2351
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002352static __always_inline unsigned long vmcs_readl(unsigned long field)
2353{
2354 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002355 if (static_branch_unlikely(&enable_evmcs))
2356 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002357 return __vmcs_readl(field);
2358}
2359
Avi Kivitye52de1b2007-01-05 16:36:56 -08002360static noinline void vmwrite_error(unsigned long field, unsigned long value)
2361{
2362 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2363 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2364 dump_stack();
2365}
2366
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002367static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002369 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002371 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2372 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002373 if (unlikely(error))
2374 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
2376
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002377static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002379 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002380 if (static_branch_unlikely(&enable_evmcs))
2381 return evmcs_write16(field, value);
2382
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002383 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384}
2385
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002388 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002389 if (static_branch_unlikely(&enable_evmcs))
2390 return evmcs_write32(field, value);
2391
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002392 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393}
2394
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002395static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002398 if (static_branch_unlikely(&enable_evmcs))
2399 return evmcs_write64(field, value);
2400
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002401 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002402#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405#endif
2406}
2407
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002408static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002409{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002410 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002411 if (static_branch_unlikely(&enable_evmcs))
2412 return evmcs_write64(field, value);
2413
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002414 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002415}
2416
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002417static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002418{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002419 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2420 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002421 if (static_branch_unlikely(&enable_evmcs))
2422 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2423
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002424 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2425}
2426
2427static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2428{
2429 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2430 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002431 if (static_branch_unlikely(&enable_evmcs))
2432 return evmcs_write32(field, evmcs_read32(field) | mask);
2433
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002434 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002435}
2436
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002437static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2438{
2439 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2440}
2441
Gleb Natapov2961e8762013-11-25 15:37:13 +02002442static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2443{
2444 vmcs_write32(VM_ENTRY_CONTROLS, val);
2445 vmx->vm_entry_controls_shadow = val;
2446}
2447
2448static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2449{
2450 if (vmx->vm_entry_controls_shadow != val)
2451 vm_entry_controls_init(vmx, val);
2452}
2453
2454static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2455{
2456 return vmx->vm_entry_controls_shadow;
2457}
2458
2459
2460static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2461{
2462 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2463}
2464
2465static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2466{
2467 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2468}
2469
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002470static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2471{
2472 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2473}
2474
Gleb Natapov2961e8762013-11-25 15:37:13 +02002475static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2476{
2477 vmcs_write32(VM_EXIT_CONTROLS, val);
2478 vmx->vm_exit_controls_shadow = val;
2479}
2480
2481static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2482{
2483 if (vmx->vm_exit_controls_shadow != val)
2484 vm_exit_controls_init(vmx, val);
2485}
2486
2487static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2488{
2489 return vmx->vm_exit_controls_shadow;
2490}
2491
2492
2493static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2494{
2495 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2496}
2497
2498static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2499{
2500 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2501}
2502
Avi Kivity2fb92db2011-04-27 19:42:18 +03002503static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2504{
2505 vmx->segment_cache.bitmask = 0;
2506}
2507
2508static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2509 unsigned field)
2510{
2511 bool ret;
2512 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2513
2514 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2515 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2516 vmx->segment_cache.bitmask = 0;
2517 }
2518 ret = vmx->segment_cache.bitmask & mask;
2519 vmx->segment_cache.bitmask |= mask;
2520 return ret;
2521}
2522
2523static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2524{
2525 u16 *p = &vmx->segment_cache.seg[seg].selector;
2526
2527 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2528 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2529 return *p;
2530}
2531
2532static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2533{
2534 ulong *p = &vmx->segment_cache.seg[seg].base;
2535
2536 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2537 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2538 return *p;
2539}
2540
2541static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2542{
2543 u32 *p = &vmx->segment_cache.seg[seg].limit;
2544
2545 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2546 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2547 return *p;
2548}
2549
2550static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2551{
2552 u32 *p = &vmx->segment_cache.seg[seg].ar;
2553
2554 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2555 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2556 return *p;
2557}
2558
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002559static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2560{
2561 u32 eb;
2562
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002563 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002564 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002565 /*
2566 * Guest access to VMware backdoor ports could legitimately
2567 * trigger #GP because of TSS I/O permission bitmap.
2568 * We intercept those #GP and allow access to them anyway
2569 * as VMware does.
2570 */
2571 if (enable_vmware_backdoor)
2572 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002573 if ((vcpu->guest_debug &
2574 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2575 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2576 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002577 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002578 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002579 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002580 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002581
2582 /* When we are running a nested L2 guest and L1 specified for it a
2583 * certain exception bitmap, we must trap the same exceptions and pass
2584 * them to L1. When running L2, we will only handle the exceptions
2585 * specified above if L1 did not want them.
2586 */
2587 if (is_guest_mode(vcpu))
2588 eb |= get_vmcs12(vcpu)->exception_bitmap;
2589
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002590 vmcs_write32(EXCEPTION_BITMAP, eb);
2591}
2592
Ashok Raj15d45072018-02-01 22:59:43 +01002593/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002594 * Check if MSR is intercepted for currently loaded MSR bitmap.
2595 */
2596static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2597{
2598 unsigned long *msr_bitmap;
2599 int f = sizeof(unsigned long);
2600
2601 if (!cpu_has_vmx_msr_bitmap())
2602 return true;
2603
2604 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2605
2606 if (msr <= 0x1fff) {
2607 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2608 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2609 msr &= 0x1fff;
2610 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2611 }
2612
2613 return true;
2614}
2615
2616/*
Ashok Raj15d45072018-02-01 22:59:43 +01002617 * Check if MSR is intercepted for L01 MSR bitmap.
2618 */
2619static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2620{
2621 unsigned long *msr_bitmap;
2622 int f = sizeof(unsigned long);
2623
2624 if (!cpu_has_vmx_msr_bitmap())
2625 return true;
2626
2627 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2628
2629 if (msr <= 0x1fff) {
2630 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2631 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2632 msr &= 0x1fff;
2633 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2634 }
2635
2636 return true;
2637}
2638
Gleb Natapov2961e8762013-11-25 15:37:13 +02002639static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2640 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002641{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002642 vm_entry_controls_clearbit(vmx, entry);
2643 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002644}
2645
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002646static int find_msr(struct vmx_msrs *m, unsigned int msr)
2647{
2648 unsigned int i;
2649
2650 for (i = 0; i < m->nr; ++i) {
2651 if (m->val[i].index == msr)
2652 return i;
2653 }
2654 return -ENOENT;
2655}
2656
Avi Kivity61d2ef22010-04-28 16:40:38 +03002657static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2658{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002659 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002660 struct msr_autoload *m = &vmx->msr_autoload;
2661
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002662 switch (msr) {
2663 case MSR_EFER:
2664 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002665 clear_atomic_switch_msr_special(vmx,
2666 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002667 VM_EXIT_LOAD_IA32_EFER);
2668 return;
2669 }
2670 break;
2671 case MSR_CORE_PERF_GLOBAL_CTRL:
2672 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002673 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002674 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2675 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2676 return;
2677 }
2678 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002679 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002680 i = find_msr(&m->guest, msr);
2681 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002682 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002683 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002684 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002685 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002686
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002687skip_guest:
2688 i = find_msr(&m->host, msr);
2689 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002690 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002691
2692 --m->host.nr;
2693 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002694 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002695}
2696
Gleb Natapov2961e8762013-11-25 15:37:13 +02002697static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2698 unsigned long entry, unsigned long exit,
2699 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2700 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002701{
2702 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07002703 if (host_val_vmcs != HOST_IA32_EFER)
2704 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002705 vm_entry_controls_setbit(vmx, entry);
2706 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002707}
2708
Avi Kivity61d2ef22010-04-28 16:40:38 +03002709static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002710 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002711{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002712 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002713 struct msr_autoload *m = &vmx->msr_autoload;
2714
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002715 switch (msr) {
2716 case MSR_EFER:
2717 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002718 add_atomic_switch_msr_special(vmx,
2719 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002720 VM_EXIT_LOAD_IA32_EFER,
2721 GUEST_IA32_EFER,
2722 HOST_IA32_EFER,
2723 guest_val, host_val);
2724 return;
2725 }
2726 break;
2727 case MSR_CORE_PERF_GLOBAL_CTRL:
2728 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002729 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002730 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2731 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2732 GUEST_IA32_PERF_GLOBAL_CTRL,
2733 HOST_IA32_PERF_GLOBAL_CTRL,
2734 guest_val, host_val);
2735 return;
2736 }
2737 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002738 case MSR_IA32_PEBS_ENABLE:
2739 /* PEBS needs a quiescent period after being disabled (to write
2740 * a record). Disabling PEBS through VMX MSR swapping doesn't
2741 * provide that period, so a CPU could write host's record into
2742 * guest's memory.
2743 */
2744 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002745 }
2746
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002747 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002748 if (!entry_only)
2749 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002750
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002751 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002752 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002753 "Can't add msr %x\n", msr);
2754 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002755 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002756 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002757 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002758 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002759 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002760 m->guest.val[i].index = msr;
2761 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002762
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002763 if (entry_only)
2764 return;
2765
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002766 if (j < 0) {
2767 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002768 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002769 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002770 m->host.val[j].index = msr;
2771 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002772}
2773
Avi Kivity92c0d902009-10-29 11:00:16 +02002774static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002775{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002776 u64 guest_efer = vmx->vcpu.arch.efer;
2777 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002778
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002779 if (!enable_ept) {
2780 /*
2781 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2782 * host CPUID is more efficient than testing guest CPUID
2783 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2784 */
2785 if (boot_cpu_has(X86_FEATURE_SMEP))
2786 guest_efer |= EFER_NX;
2787 else if (!(guest_efer & EFER_NX))
2788 ignore_bits |= EFER_NX;
2789 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002790
Avi Kivity51c6cf62007-08-29 03:48:05 +03002791 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002792 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002793 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002794 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002795#ifdef CONFIG_X86_64
2796 ignore_bits |= EFER_LMA | EFER_LME;
2797 /* SCE is meaningful only in long mode on Intel */
2798 if (guest_efer & EFER_LMA)
2799 ignore_bits &= ~(u64)EFER_SCE;
2800#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002801
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002802 /*
2803 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2804 * On CPUs that support "load IA32_EFER", always switch EFER
2805 * atomically, since it's faster than switching it manually.
2806 */
2807 if (cpu_has_load_ia32_efer ||
2808 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002809 if (!(guest_efer & EFER_LMA))
2810 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002811 if (guest_efer != host_efer)
2812 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002813 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002814 else
2815 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002816 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002817 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002818 clear_atomic_switch_msr(vmx, MSR_EFER);
2819
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002820 guest_efer &= ~ignore_bits;
2821 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002822
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002823 vmx->guest_msrs[efer_offset].data = guest_efer;
2824 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2825
2826 return true;
2827 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002828}
2829
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002830#ifdef CONFIG_X86_32
2831/*
2832 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2833 * VMCS rather than the segment table. KVM uses this helper to figure
2834 * out the current bases to poke them into the VMCS before entry.
2835 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836static unsigned long segment_base(u16 selector)
2837{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002838 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002839 unsigned long v;
2840
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002841 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842 return 0;
2843
Thomas Garnier45fc8752017-03-14 10:05:08 -07002844 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002845
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002846 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002847 u16 ldt_selector = kvm_read_ldt();
2848
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002849 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002850 return 0;
2851
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002852 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002853 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002854 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002855 return v;
2856}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002857#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002858
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002859static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002860{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002861 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002862 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002863#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002864 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002865#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002866 unsigned long fs_base, gs_base;
2867 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002868 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002869
Sean Christophersond264ee02018-08-27 15:21:12 -07002870 vmx->req_immediate_exit = false;
2871
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002872 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002873 return;
2874
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002875 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002876 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002877
Avi Kivity33ed6322007-05-02 16:54:03 +03002878 /*
2879 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2880 * allow segment selectors with cpl > 0 or ti == 1.
2881 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002882 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002883
2884#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002885 savesegment(ds, host_state->ds_sel);
2886 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002887
2888 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002889 if (likely(is_64bit_mm(current->mm))) {
2890 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002891 fs_sel = current->thread.fsindex;
2892 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002893 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002894 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002895 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002896 savesegment(fs, fs_sel);
2897 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002898 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002899 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002900 }
2901
Paolo Bonzini4679b612018-09-24 17:23:01 +02002902 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002903#else
Sean Christophersone368b872018-07-23 12:32:41 -07002904 savesegment(fs, fs_sel);
2905 savesegment(gs, gs_sel);
2906 fs_base = segment_base(fs_sel);
2907 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002908#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002909
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002910 if (unlikely(fs_sel != host_state->fs_sel)) {
2911 if (!(fs_sel & 7))
2912 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2913 else
2914 vmcs_write16(HOST_FS_SELECTOR, 0);
2915 host_state->fs_sel = fs_sel;
2916 }
2917 if (unlikely(gs_sel != host_state->gs_sel)) {
2918 if (!(gs_sel & 7))
2919 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2920 else
2921 vmcs_write16(HOST_GS_SELECTOR, 0);
2922 host_state->gs_sel = gs_sel;
2923 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002924 if (unlikely(fs_base != host_state->fs_base)) {
2925 vmcs_writel(HOST_FS_BASE, fs_base);
2926 host_state->fs_base = fs_base;
2927 }
2928 if (unlikely(gs_base != host_state->gs_base)) {
2929 vmcs_writel(HOST_GS_BASE, gs_base);
2930 host_state->gs_base = gs_base;
2931 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002932
Avi Kivity26bb0982009-09-07 11:14:12 +03002933 for (i = 0; i < vmx->save_nmsrs; ++i)
2934 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002935 vmx->guest_msrs[i].data,
2936 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002937}
2938
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002939static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002940{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002941 struct vmcs_host_state *host_state;
2942
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002943 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002944 return;
2945
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002946 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002947 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002948
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002949 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002950 vmx->loaded_cpu_state = NULL;
2951
Avi Kivityc8770e72010-11-11 12:37:26 +02002952#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002953 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002954#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002955 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2956 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002957#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002958 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002959#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002960 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002961#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002962 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002963 if (host_state->fs_sel & 7)
2964 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002965#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002966 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2967 loadsegment(ds, host_state->ds_sel);
2968 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002969 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002970#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002971 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002972#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002973 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002974#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002975 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002976}
2977
Sean Christopherson678e3152018-07-23 12:32:43 -07002978#ifdef CONFIG_X86_64
2979static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002980{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002981 preempt_disable();
2982 if (vmx->loaded_cpu_state)
2983 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2984 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002985 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002986}
2987
Sean Christopherson678e3152018-07-23 12:32:43 -07002988static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2989{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002990 preempt_disable();
2991 if (vmx->loaded_cpu_state)
2992 wrmsrl(MSR_KERNEL_GS_BASE, data);
2993 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002994 vmx->msr_guest_kernel_gs_base = data;
2995}
2996#endif
2997
Feng Wu28b835d2015-09-18 22:29:54 +08002998static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2999{
3000 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3001 struct pi_desc old, new;
3002 unsigned int dest;
3003
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003004 /*
3005 * In case of hot-plug or hot-unplug, we may have to undo
3006 * vmx_vcpu_pi_put even if there is no assigned device. And we
3007 * always keep PI.NDST up to date for simplicity: it makes the
3008 * code easier, and CPU migration is not a fast path.
3009 */
3010 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003011 return;
3012
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003013 /*
3014 * First handle the simple case where no cmpxchg is necessary; just
3015 * allow posting non-urgent interrupts.
3016 *
3017 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3018 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3019 * expects the VCPU to be on the blocked_vcpu_list that matches
3020 * PI.NDST.
3021 */
3022 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3023 vcpu->cpu == cpu) {
3024 pi_clear_sn(pi_desc);
3025 return;
3026 }
3027
3028 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003029 do {
3030 old.control = new.control = pi_desc->control;
3031
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003032 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003033
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003034 if (x2apic_enabled())
3035 new.ndst = dest;
3036 else
3037 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003038
Feng Wu28b835d2015-09-18 22:29:54 +08003039 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003040 } while (cmpxchg64(&pi_desc->control, old.control,
3041 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003042}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003043
Peter Feinerc95ba922016-08-17 09:36:47 -07003044static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3045{
3046 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3047 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3048}
3049
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050/*
3051 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3052 * vcpu mutex is already taken.
3053 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003054static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003056 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003057 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003059 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003060 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003061 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003062 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003063
3064 /*
3065 * Read loaded_vmcs->cpu should be before fetching
3066 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3067 * See the comments in __loaded_vmcs_clear().
3068 */
3069 smp_rmb();
3070
Nadav Har'Eld462b812011-05-24 15:26:10 +03003071 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3072 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003073 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003074 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003075 }
3076
3077 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3078 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3079 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003080 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003081 }
3082
3083 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003084 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003085 unsigned long sysenter_esp;
3086
3087 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003088
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 /*
3090 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003091 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003093 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003094 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003095 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003097 /*
3098 * VM exits change the host TR limit to 0x67 after a VM
3099 * exit. This is okay, since 0x67 covers everything except
3100 * the IO bitmap and have have code to handle the IO bitmap
3101 * being lost after a VM exit.
3102 */
3103 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3104
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3106 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003107
Nadav Har'Eld462b812011-05-24 15:26:10 +03003108 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 }
Feng Wu28b835d2015-09-18 22:29:54 +08003110
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003111 /* Setup TSC multiplier */
3112 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003113 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3114 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003115
Feng Wu28b835d2015-09-18 22:29:54 +08003116 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003117 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003118 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003119}
3120
3121static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3122{
3123 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3124
3125 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003126 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3127 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003128 return;
3129
3130 /* Set SN when the vCPU is preempted */
3131 if (vcpu->preempted)
3132 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133}
3134
3135static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3136{
Feng Wu28b835d2015-09-18 22:29:54 +08003137 vmx_vcpu_pi_put(vcpu);
3138
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003139 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140}
3141
Wanpeng Lif244dee2017-07-20 01:11:54 -07003142static bool emulation_required(struct kvm_vcpu *vcpu)
3143{
3144 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3145}
3146
Avi Kivityedcafe32009-12-30 18:07:40 +02003147static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3148
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003149/*
3150 * Return the cr0 value that a nested guest would read. This is a combination
3151 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3152 * its hypervisor (cr0_read_shadow).
3153 */
3154static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3155{
3156 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3157 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3158}
3159static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3160{
3161 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3162 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3163}
3164
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3166{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003167 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003168
Avi Kivity6de12732011-03-07 12:51:22 +02003169 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3170 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3171 rflags = vmcs_readl(GUEST_RFLAGS);
3172 if (to_vmx(vcpu)->rmode.vm86_active) {
3173 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3174 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3175 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3176 }
3177 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003178 }
Avi Kivity6de12732011-03-07 12:51:22 +02003179 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180}
3181
3182static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3183{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003184 unsigned long old_rflags = vmx_get_rflags(vcpu);
3185
Avi Kivity6de12732011-03-07 12:51:22 +02003186 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3187 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003188 if (to_vmx(vcpu)->rmode.vm86_active) {
3189 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003190 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003191 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003193
3194 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3195 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196}
3197
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003198static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003199{
3200 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3201 int ret = 0;
3202
3203 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003204 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003205 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003206 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003207
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003208 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003209}
3210
3211static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3212{
3213 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3214 u32 interruptibility = interruptibility_old;
3215
3216 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3217
Jan Kiszka48005f62010-02-19 19:38:07 +01003218 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003219 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003220 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003221 interruptibility |= GUEST_INTR_STATE_STI;
3222
3223 if ((interruptibility != interruptibility_old))
3224 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3225}
3226
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3228{
3229 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003231 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003233 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234
Glauber Costa2809f5d2009-05-12 16:21:05 -04003235 /* skipping an emulated instruction also counts */
3236 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237}
3238
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003239static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3240 unsigned long exit_qual)
3241{
3242 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3243 unsigned int nr = vcpu->arch.exception.nr;
3244 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3245
3246 if (vcpu->arch.exception.has_error_code) {
3247 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3248 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3249 }
3250
3251 if (kvm_exception_is_soft(nr))
3252 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3253 else
3254 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3255
3256 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3257 vmx_get_nmi_mask(vcpu))
3258 intr_info |= INTR_INFO_UNBLOCK_NMI;
3259
3260 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3261}
3262
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003263/*
3264 * KVM wants to inject page-faults which it got to the guest. This function
3265 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003266 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003267static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003268{
3269 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003270 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003271
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003272 if (nr == PF_VECTOR) {
3273 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003274 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003275 return 1;
3276 }
3277 /*
3278 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3279 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3280 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3281 * can be written only when inject_pending_event runs. This should be
3282 * conditional on a new capability---if the capability is disabled,
3283 * kvm_multiple_exception would write the ancillary information to
3284 * CR2 or DR6, for backwards ABI-compatibility.
3285 */
3286 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3287 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003288 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003289 return 1;
3290 }
3291 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003292 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003293 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003294 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003295 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3296 *exit_qual ^= DR6_RTM;
3297 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003298 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003299 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003300 return 1;
3301 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003302 }
3303
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003304 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003305}
3306
Wanpeng Licaa057a2018-03-12 04:53:03 -07003307static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3308{
3309 /*
3310 * Ensure that we clear the HLT state in the VMCS. We don't need to
3311 * explicitly skip the instruction because if the HLT state is set,
3312 * then the instruction is already executing and RIP has already been
3313 * advanced.
3314 */
3315 if (kvm_hlt_in_guest(vcpu->kvm) &&
3316 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3317 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3318}
3319
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003320static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003321{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003322 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003323 unsigned nr = vcpu->arch.exception.nr;
3324 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003325 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003326 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003327
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003328 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003329 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003330 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3331 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003332
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003333 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003334 int inc_eip = 0;
3335 if (kvm_exception_is_soft(nr))
3336 inc_eip = vcpu->arch.event_exit_inst_len;
3337 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003338 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003339 return;
3340 }
3341
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003342 WARN_ON_ONCE(vmx->emulation_required);
3343
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003344 if (kvm_exception_is_soft(nr)) {
3345 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3346 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003347 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3348 } else
3349 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3350
3351 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003352
3353 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003354}
3355
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003356static bool vmx_rdtscp_supported(void)
3357{
3358 return cpu_has_vmx_rdtscp();
3359}
3360
Mao, Junjiead756a12012-07-02 01:18:48 +00003361static bool vmx_invpcid_supported(void)
3362{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003363 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003364}
3365
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366/*
Eddie Donga75beee2007-05-17 18:55:15 +03003367 * Swap MSR entry in host/guest MSR entry array.
3368 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003369static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003370{
Avi Kivity26bb0982009-09-07 11:14:12 +03003371 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003372
3373 tmp = vmx->guest_msrs[to];
3374 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3375 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003376}
3377
3378/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003379 * Set up the vmcs to automatically save and restore system
3380 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3381 * mode, as fiddling with msrs is very expensive.
3382 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003383static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003384{
Avi Kivity26bb0982009-09-07 11:14:12 +03003385 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003386
Eddie Donga75beee2007-05-17 18:55:15 +03003387 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003388#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003389 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003390 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003391 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003392 move_msr_up(vmx, index, save_nmsrs++);
3393 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003394 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003395 move_msr_up(vmx, index, save_nmsrs++);
3396 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003397 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003398 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003399 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003400 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003401 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003402 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003403 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003404 * if efer.sce is enabled.
3405 */
Brian Gerst8c065852010-07-17 09:03:26 -04003406 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003407 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003408 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003409 }
Eddie Donga75beee2007-05-17 18:55:15 +03003410#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003411 index = __find_msr_index(vmx, MSR_EFER);
3412 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003413 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003414
Avi Kivity26bb0982009-09-07 11:14:12 +03003415 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003416
Yang Zhang8d146952013-01-25 10:18:50 +08003417 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003418 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003419}
3420
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003421static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003423 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003425 if (is_guest_mode(vcpu) &&
3426 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3427 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3428
3429 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430}
3431
3432/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003433 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003435static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003437 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003438 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003439 * We're here if L1 chose not to trap WRMSR to TSC. According
3440 * to the spec, this should set L1's TSC; The offset that L1
3441 * set for L2 remains unchanged, and still needs to be added
3442 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003443 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003444 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003445 /* recalculate vmcs02.TSC_OFFSET: */
3446 vmcs12 = get_vmcs12(vcpu);
3447 vmcs_write64(TSC_OFFSET, offset +
3448 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3449 vmcs12->tsc_offset : 0));
3450 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003451 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3452 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003453 vmcs_write64(TSC_OFFSET, offset);
3454 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455}
3456
Nadav Har'El801d3422011-05-25 23:02:23 +03003457/*
3458 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3459 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3460 * all guests if the "nested" module option is off, and can also be disabled
3461 * for a single guest by disabling its VMX cpuid bit.
3462 */
3463static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3464{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003465 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003466}
3467
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003469 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3470 * returned for the various VMX controls MSRs when nested VMX is enabled.
3471 * The same values should also be used to verify that vmcs12 control fields are
3472 * valid during nested entry from L1 to L2.
3473 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3474 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3475 * bit in the high half is on if the corresponding bit in the control field
3476 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003477 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003478static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003479{
Paolo Bonzini13893092018-02-26 13:40:09 +01003480 if (!nested) {
3481 memset(msrs, 0, sizeof(*msrs));
3482 return;
3483 }
3484
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003485 /*
3486 * Note that as a general rule, the high half of the MSRs (bits in
3487 * the control fields which may be 1) should be initialized by the
3488 * intersection of the underlying hardware's MSR (i.e., features which
3489 * can be supported) and the list of features we want to expose -
3490 * because they are known to be properly supported in our code.
3491 * Also, usually, the low half of the MSRs (bits which must be 1) can
3492 * be set to 0, meaning that L1 may turn off any of these bits. The
3493 * reason is that if one of these bits is necessary, it will appear
3494 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3495 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003496 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003497 * These rules have exceptions below.
3498 */
3499
3500 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003501 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003502 msrs->pinbased_ctls_low,
3503 msrs->pinbased_ctls_high);
3504 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003505 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003506 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003507 PIN_BASED_EXT_INTR_MASK |
3508 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003509 PIN_BASED_VIRTUAL_NMIS |
3510 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003511 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003512 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003513 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003514
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003515 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003516 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003517 msrs->exit_ctls_low,
3518 msrs->exit_ctls_high);
3519 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003520 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003521
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003522 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003523#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003524 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003525#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003526 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003527 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003528 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003529 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003530 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3531
Jan Kiszka2996fca2014-06-16 13:59:43 +02003532 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003533 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003534
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003535 /* entry controls */
3536 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 msrs->entry_ctls_low,
3538 msrs->entry_ctls_high);
3539 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003540 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003542#ifdef CONFIG_X86_64
3543 VM_ENTRY_IA32E_MODE |
3544#endif
3545 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003547 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003548
Jan Kiszka2996fca2014-06-16 13:59:43 +02003549 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003550 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003551
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003552 /* cpu-based controls */
3553 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003554 msrs->procbased_ctls_low,
3555 msrs->procbased_ctls_high);
3556 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003557 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003558 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003559 CPU_BASED_VIRTUAL_INTR_PENDING |
3560 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003561 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3562 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3563 CPU_BASED_CR3_STORE_EXITING |
3564#ifdef CONFIG_X86_64
3565 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3566#endif
3567 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003568 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3569 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3570 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3571 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003572 /*
3573 * We can allow some features even when not supported by the
3574 * hardware. For example, L1 can specify an MSR bitmap - and we
3575 * can use it to avoid exits to L1 - even when L0 runs L2
3576 * without MSR bitmaps.
3577 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003578 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003579 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003580 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003581
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003582 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003583 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003584 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3585
Paolo Bonzini80154d72017-08-24 13:55:35 +02003586 /*
3587 * secondary cpu-based controls. Do not include those that
3588 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3589 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003590 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003591 msrs->secondary_ctls_low,
3592 msrs->secondary_ctls_high);
3593 msrs->secondary_ctls_low = 0;
3594 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003595 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003596 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003597 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003598 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003599 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003600
Liran Alon32c7acf2018-06-23 02:35:11 +03003601 /*
3602 * We can emulate "VMCS shadowing," even if the hardware
3603 * doesn't support it.
3604 */
3605 msrs->secondary_ctls_high |=
3606 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003607
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003608 if (enable_ept) {
3609 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003611 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003613 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003614 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003616 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003617 msrs->ept_caps &= vmx_capability.ept;
3618 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003619 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3620 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003621 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003622 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003623 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003624 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003625 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003626 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003627
Bandan Das27c42a12017-08-03 15:54:42 -04003628 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003629 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003630 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003631 /*
3632 * Advertise EPTP switching unconditionally
3633 * since we emulate it
3634 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003635 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003636 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003637 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003638 }
3639
Paolo Bonzinief697a72016-03-18 16:58:38 +01003640 /*
3641 * Old versions of KVM use the single-context version without
3642 * checking for support, so declare that it is supported even
3643 * though it is treated as global context. The alternative is
3644 * not failing the single-context invvpid, and it is worse.
3645 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003646 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003648 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003650 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003651 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003652
Radim Krčmář0790ec12015-03-17 14:02:32 +01003653 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003654 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003655 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3656
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003657 if (flexpriority_enabled)
3658 msrs->secondary_ctls_high |=
3659 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3660
Jan Kiszkac18911a2013-03-13 16:06:41 +01003661 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003662 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 msrs->misc_low,
3664 msrs->misc_high);
3665 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3666 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003667 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003668 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003669 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003670 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003671
3672 /*
3673 * This MSR reports some information about VMX support. We
3674 * should return information about the VMX we emulate for the
3675 * guest, and the VMCS structure we give it - not about the
3676 * VMX support of the underlying hardware.
3677 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003679 VMCS12_REVISION |
3680 VMX_BASIC_TRUE_CTLS |
3681 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3682 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3683
3684 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003686
3687 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003688 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003689 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3690 * We picked the standard core2 setting.
3691 */
3692#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3693#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3695 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003696
3697 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003698 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3699 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003700
3701 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703}
3704
David Matlack38991522016-11-29 18:14:08 -08003705/*
3706 * if fixed0[i] == 1: val[i] must be 1
3707 * if fixed1[i] == 0: val[i] must be 0
3708 */
3709static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3710{
3711 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003712}
3713
3714static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3715{
David Matlack38991522016-11-29 18:14:08 -08003716 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717}
3718
3719static inline u64 vmx_control_msr(u32 low, u32 high)
3720{
3721 return low | ((u64)high << 32);
3722}
3723
David Matlack62cc6b9d2016-11-29 18:14:07 -08003724static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3725{
3726 superset &= mask;
3727 subset &= mask;
3728
3729 return (superset | subset) == superset;
3730}
3731
3732static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3733{
3734 const u64 feature_and_reserved =
3735 /* feature (except bit 48; see below) */
3736 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3737 /* reserved */
3738 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003739 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003740
3741 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3742 return -EINVAL;
3743
3744 /*
3745 * KVM does not emulate a version of VMX that constrains physical
3746 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3747 */
3748 if (data & BIT_ULL(48))
3749 return -EINVAL;
3750
3751 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3752 vmx_basic_vmcs_revision_id(data))
3753 return -EINVAL;
3754
3755 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3756 return -EINVAL;
3757
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003759 return 0;
3760}
3761
3762static int
3763vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3764{
3765 u64 supported;
3766 u32 *lowp, *highp;
3767
3768 switch (msr_index) {
3769 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003770 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3771 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003772 break;
3773 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 lowp = &vmx->nested.msrs.procbased_ctls_low;
3775 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003776 break;
3777 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 lowp = &vmx->nested.msrs.exit_ctls_low;
3779 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003780 break;
3781 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003782 lowp = &vmx->nested.msrs.entry_ctls_low;
3783 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003784 break;
3785 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003786 lowp = &vmx->nested.msrs.secondary_ctls_low;
3787 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003788 break;
3789 default:
3790 BUG();
3791 }
3792
3793 supported = vmx_control_msr(*lowp, *highp);
3794
3795 /* Check must-be-1 bits are still 1. */
3796 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3797 return -EINVAL;
3798
3799 /* Check must-be-0 bits are still 0. */
3800 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3801 return -EINVAL;
3802
3803 *lowp = data;
3804 *highp = data >> 32;
3805 return 0;
3806}
3807
3808static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3809{
3810 const u64 feature_and_reserved_bits =
3811 /* feature */
3812 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3813 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3814 /* reserved */
3815 GENMASK_ULL(13, 9) | BIT_ULL(31);
3816 u64 vmx_misc;
3817
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003818 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3819 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003820
3821 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3822 return -EINVAL;
3823
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003824 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003825 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3826 vmx_misc_preemption_timer_rate(data) !=
3827 vmx_misc_preemption_timer_rate(vmx_misc))
3828 return -EINVAL;
3829
3830 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3831 return -EINVAL;
3832
3833 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3834 return -EINVAL;
3835
3836 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3837 return -EINVAL;
3838
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003839 vmx->nested.msrs.misc_low = data;
3840 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003841
3842 /*
3843 * If L1 has read-only VM-exit information fields, use the
3844 * less permissive vmx_vmwrite_bitmap to specify write
3845 * permissions for the shadow VMCS.
3846 */
3847 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3848 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3849
David Matlack62cc6b9d2016-11-29 18:14:07 -08003850 return 0;
3851}
3852
3853static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3854{
3855 u64 vmx_ept_vpid_cap;
3856
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003857 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3858 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003859
3860 /* Every bit is either reserved or a feature bit. */
3861 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3862 return -EINVAL;
3863
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003864 vmx->nested.msrs.ept_caps = data;
3865 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003866 return 0;
3867}
3868
3869static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3870{
3871 u64 *msr;
3872
3873 switch (msr_index) {
3874 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003875 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003876 break;
3877 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003878 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003879 break;
3880 default:
3881 BUG();
3882 }
3883
3884 /*
3885 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3886 * must be 1 in the restored value.
3887 */
3888 if (!is_bitwise_subset(data, *msr, -1ULL))
3889 return -EINVAL;
3890
3891 *msr = data;
3892 return 0;
3893}
3894
3895/*
3896 * Called when userspace is restoring VMX MSRs.
3897 *
3898 * Returns 0 on success, non-0 otherwise.
3899 */
3900static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3901{
3902 struct vcpu_vmx *vmx = to_vmx(vcpu);
3903
Jim Mattsona943ac52018-05-29 09:11:32 -07003904 /*
3905 * Don't allow changes to the VMX capability MSRs while the vCPU
3906 * is in VMX operation.
3907 */
3908 if (vmx->nested.vmxon)
3909 return -EBUSY;
3910
David Matlack62cc6b9d2016-11-29 18:14:07 -08003911 switch (msr_index) {
3912 case MSR_IA32_VMX_BASIC:
3913 return vmx_restore_vmx_basic(vmx, data);
3914 case MSR_IA32_VMX_PINBASED_CTLS:
3915 case MSR_IA32_VMX_PROCBASED_CTLS:
3916 case MSR_IA32_VMX_EXIT_CTLS:
3917 case MSR_IA32_VMX_ENTRY_CTLS:
3918 /*
3919 * The "non-true" VMX capability MSRs are generated from the
3920 * "true" MSRs, so we do not support restoring them directly.
3921 *
3922 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3923 * should restore the "true" MSRs with the must-be-1 bits
3924 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3925 * DEFAULT SETTINGS".
3926 */
3927 return -EINVAL;
3928 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3929 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3930 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3931 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3932 case MSR_IA32_VMX_PROCBASED_CTLS2:
3933 return vmx_restore_control_msr(vmx, msr_index, data);
3934 case MSR_IA32_VMX_MISC:
3935 return vmx_restore_vmx_misc(vmx, data);
3936 case MSR_IA32_VMX_CR0_FIXED0:
3937 case MSR_IA32_VMX_CR4_FIXED0:
3938 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3939 case MSR_IA32_VMX_CR0_FIXED1:
3940 case MSR_IA32_VMX_CR4_FIXED1:
3941 /*
3942 * These MSRs are generated based on the vCPU's CPUID, so we
3943 * do not support restoring them directly.
3944 */
3945 return -EINVAL;
3946 case MSR_IA32_VMX_EPT_VPID_CAP:
3947 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3948 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003949 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003950 return 0;
3951 default:
3952 /*
3953 * The rest of the VMX capability MSRs do not support restore.
3954 */
3955 return -EINVAL;
3956 }
3957}
3958
Jan Kiszkacae50132014-01-04 18:47:22 +01003959/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003960static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003961{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003962 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003963 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003964 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003965 break;
3966 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3967 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003968 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003969 msrs->pinbased_ctls_low,
3970 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003971 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3972 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003973 break;
3974 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3975 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003976 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003977 msrs->procbased_ctls_low,
3978 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003979 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3980 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003981 break;
3982 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3983 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003984 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003985 msrs->exit_ctls_low,
3986 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003987 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3988 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003989 break;
3990 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3991 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003992 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003993 msrs->entry_ctls_low,
3994 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003995 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3996 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003999 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004000 msrs->misc_low,
4001 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004003 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 break;
4009 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004010 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004011 break;
4012 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004013 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
4015 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004016 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004017 break;
4018 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004019 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004020 msrs->secondary_ctls_low,
4021 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 break;
4023 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004024 *pdata = msrs->ept_caps |
4025 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004027 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004028 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004029 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004030 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004031 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004032 }
4033
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004034 return 0;
4035}
4036
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004037static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4038 uint64_t val)
4039{
4040 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4041
4042 return !(val & ~valid_bits);
4043}
4044
Tom Lendacky801e4592018-02-21 13:39:51 -06004045static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4046{
Paolo Bonzini13893092018-02-26 13:40:09 +01004047 switch (msr->index) {
4048 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4049 if (!nested)
4050 return 1;
4051 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4052 default:
4053 return 1;
4054 }
4055
4056 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004057}
4058
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004059/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060 * Reads an msr value (of 'msr_index') into 'pdata'.
4061 * Returns 0 on success, non-0 otherwise.
4062 * Assumes vcpu_load() was already called.
4063 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004064static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004066 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004067 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004070#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004072 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 break;
4074 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004075 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004077 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004078 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004079 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004080#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004082 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004083 case MSR_IA32_SPEC_CTRL:
4084 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004085 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4086 return 1;
4087
4088 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4089 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004090 case MSR_IA32_ARCH_CAPABILITIES:
4091 if (!msr_info->host_initiated &&
4092 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4093 return 1;
4094 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4095 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004097 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 break;
4099 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004100 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 break;
4102 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004103 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004105 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004106 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004107 (!msr_info->host_initiated &&
4108 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004109 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004110 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004111 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004112 case MSR_IA32_MCG_EXT_CTL:
4113 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004114 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004115 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004116 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004117 msr_info->data = vcpu->arch.mcg_ext_ctl;
4118 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004119 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004120 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004121 break;
4122 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4123 if (!nested_vmx_allowed(vcpu))
4124 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004125 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4126 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004127 case MSR_IA32_XSS:
4128 if (!vmx_xsaves_supported())
4129 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004130 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004131 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004132 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004133 if (!msr_info->host_initiated &&
4134 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004135 return 1;
4136 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004138 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004139 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004140 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004141 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004143 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 }
4145
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 return 0;
4147}
4148
Jan Kiszkacae50132014-01-04 18:47:22 +01004149static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4150
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151/*
4152 * Writes msr value into into the appropriate "register".
4153 * Returns 0 on success, non-0 otherwise.
4154 * Assumes vcpu_load() was already called.
4155 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004156static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004158 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004159 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004160 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004161 u32 msr_index = msr_info->index;
4162 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004163
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004165 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004166 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004167 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004168#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004170 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 vmcs_writel(GUEST_FS_BASE, data);
4172 break;
4173 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004174 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175 vmcs_writel(GUEST_GS_BASE, data);
4176 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004177 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004178 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004179 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180#endif
4181 case MSR_IA32_SYSENTER_CS:
4182 vmcs_write32(GUEST_SYSENTER_CS, data);
4183 break;
4184 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004185 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 break;
4187 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004188 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004190 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004191 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004192 (!msr_info->host_initiated &&
4193 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004194 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004195 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004196 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004198 vmcs_write64(GUEST_BNDCFGS, data);
4199 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004200 case MSR_IA32_SPEC_CTRL:
4201 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004202 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4203 return 1;
4204
4205 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004206 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004207 return 1;
4208
4209 vmx->spec_ctrl = data;
4210
4211 if (!data)
4212 break;
4213
4214 /*
4215 * For non-nested:
4216 * When it's written (to non-zero) for the first time, pass
4217 * it through.
4218 *
4219 * For nested:
4220 * The handling of the MSR bitmap for L2 guests is done in
4221 * nested_vmx_merge_msr_bitmap. We should not touch the
4222 * vmcs02.msr_bitmap here since it gets completely overwritten
4223 * in the merging. We update the vmcs01 here for L1 as well
4224 * since it will end up touching the MSR anyway now.
4225 */
4226 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4227 MSR_IA32_SPEC_CTRL,
4228 MSR_TYPE_RW);
4229 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004230 case MSR_IA32_PRED_CMD:
4231 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004232 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4233 return 1;
4234
4235 if (data & ~PRED_CMD_IBPB)
4236 return 1;
4237
4238 if (!data)
4239 break;
4240
4241 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4242
4243 /*
4244 * For non-nested:
4245 * When it's written (to non-zero) for the first time, pass
4246 * it through.
4247 *
4248 * For nested:
4249 * The handling of the MSR bitmap for L2 guests is done in
4250 * nested_vmx_merge_msr_bitmap. We should not touch the
4251 * vmcs02.msr_bitmap here since it gets completely overwritten
4252 * in the merging.
4253 */
4254 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4255 MSR_TYPE_W);
4256 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004257 case MSR_IA32_ARCH_CAPABILITIES:
4258 if (!msr_info->host_initiated)
4259 return 1;
4260 vmx->arch_capabilities = data;
4261 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004262 case MSR_IA32_CR_PAT:
4263 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004264 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4265 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004266 vmcs_write64(GUEST_IA32_PAT, data);
4267 vcpu->arch.pat = data;
4268 break;
4269 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004270 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004271 break;
Will Auldba904632012-11-29 12:42:50 -08004272 case MSR_IA32_TSC_ADJUST:
4273 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004274 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004275 case MSR_IA32_MCG_EXT_CTL:
4276 if ((!msr_info->host_initiated &&
4277 !(to_vmx(vcpu)->msr_ia32_feature_control &
4278 FEATURE_CONTROL_LMCE)) ||
4279 (data & ~MCG_EXT_CTL_LMCE_EN))
4280 return 1;
4281 vcpu->arch.mcg_ext_ctl = data;
4282 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004283 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004284 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004285 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004286 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4287 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004288 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004289 if (msr_info->host_initiated && data == 0)
4290 vmx_leave_nested(vcpu);
4291 break;
4292 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004293 if (!msr_info->host_initiated)
4294 return 1; /* they are read-only */
4295 if (!nested_vmx_allowed(vcpu))
4296 return 1;
4297 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004298 case MSR_IA32_XSS:
4299 if (!vmx_xsaves_supported())
4300 return 1;
4301 /*
4302 * The only supported bit as of Skylake is bit 8, but
4303 * it is not supported on KVM.
4304 */
4305 if (data != 0)
4306 return 1;
4307 vcpu->arch.ia32_xss = data;
4308 if (vcpu->arch.ia32_xss != host_xss)
4309 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004310 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004311 else
4312 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4313 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004314 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004315 if (!msr_info->host_initiated &&
4316 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004317 return 1;
4318 /* Check reserved bit, higher 32 bits should be zero */
4319 if ((data >> 32) != 0)
4320 return 1;
4321 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004323 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004324 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004325 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004326 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004327 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4328 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004329 ret = kvm_set_shared_msr(msr->index, msr->data,
4330 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004331 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004332 if (ret)
4333 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004334 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004335 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004337 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338 }
4339
Eddie Dong2cc51562007-05-21 07:28:09 +03004340 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341}
4342
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004343static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004345 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4346 switch (reg) {
4347 case VCPU_REGS_RSP:
4348 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4349 break;
4350 case VCPU_REGS_RIP:
4351 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4352 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004353 case VCPU_EXREG_PDPTR:
4354 if (enable_ept)
4355 ept_save_pdptrs(vcpu);
4356 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004357 default:
4358 break;
4359 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360}
4361
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362static __init int cpu_has_kvm_support(void)
4363{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004364 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}
4366
4367static __init int vmx_disabled_by_bios(void)
4368{
4369 u64 msr;
4370
4371 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004372 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004373 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004374 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4375 && tboot_enabled())
4376 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004377 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004378 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004379 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004380 && !tboot_enabled()) {
4381 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004382 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004383 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004384 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004385 /* launched w/o TXT and VMX disabled */
4386 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4387 && !tboot_enabled())
4388 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004389 }
4390
4391 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392}
4393
Dongxiao Xu7725b892010-05-11 18:29:38 +08004394static void kvm_cpu_vmxon(u64 addr)
4395{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004396 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004397 intel_pt_handle_vmx(1);
4398
Dongxiao Xu7725b892010-05-11 18:29:38 +08004399 asm volatile (ASM_VMX_VMXON_RAX
4400 : : "a"(&addr), "m"(addr)
4401 : "memory", "cc");
4402}
4403
Radim Krčmář13a34e02014-08-28 15:13:03 +02004404static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405{
4406 int cpu = raw_smp_processor_id();
4407 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004408 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004410 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004411 return -EBUSY;
4412
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004413 /*
4414 * This can happen if we hot-added a CPU but failed to allocate
4415 * VP assist page for it.
4416 */
4417 if (static_branch_unlikely(&enable_evmcs) &&
4418 !hv_get_vp_assist_page(cpu))
4419 return -EFAULT;
4420
Nadav Har'Eld462b812011-05-24 15:26:10 +03004421 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004422 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4423 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004424
4425 /*
4426 * Now we can enable the vmclear operation in kdump
4427 * since the loaded_vmcss_on_cpu list on this cpu
4428 * has been initialized.
4429 *
4430 * Though the cpu is not in VMX operation now, there
4431 * is no problem to enable the vmclear operation
4432 * for the loaded_vmcss_on_cpu list is empty!
4433 */
4434 crash_enable_local_vmclear(cpu);
4435
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004437
4438 test_bits = FEATURE_CONTROL_LOCKED;
4439 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4440 if (tboot_enabled())
4441 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4442
4443 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004445 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4446 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004447 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004448 if (enable_ept)
4449 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004450
4451 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452}
4453
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004455{
4456 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004457 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004458
Nadav Har'Eld462b812011-05-24 15:26:10 +03004459 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4460 loaded_vmcss_on_cpu_link)
4461 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004462}
4463
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004464
4465/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4466 * tricks.
4467 */
4468static void kvm_cpu_vmxoff(void)
4469{
4470 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004471
4472 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004473 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004474}
4475
Radim Krčmář13a34e02014-08-28 15:13:03 +02004476static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004478 vmclear_local_loaded_vmcss();
4479 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480}
4481
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004482static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004483 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484{
4485 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004486 u32 ctl = ctl_min | ctl_opt;
4487
4488 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4489
4490 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4491 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4492
4493 /* Ensure minimum (required) set of control bits are supported. */
4494 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004495 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004496
4497 *result = ctl;
4498 return 0;
4499}
4500
Avi Kivity110312c2010-12-21 12:54:20 +02004501static __init bool allow_1_setting(u32 msr, u32 ctl)
4502{
4503 u32 vmx_msr_low, vmx_msr_high;
4504
4505 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4506 return vmx_msr_high & ctl;
4507}
4508
Yang, Sheng002c7f72007-07-31 14:23:01 +03004509static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510{
4511 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004512 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004513 u32 _pin_based_exec_control = 0;
4514 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004515 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516 u32 _vmexit_control = 0;
4517 u32 _vmentry_control = 0;
4518
Paolo Bonzini13893092018-02-26 13:40:09 +01004519 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304520 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004521#ifdef CONFIG_X86_64
4522 CPU_BASED_CR8_LOAD_EXITING |
4523 CPU_BASED_CR8_STORE_EXITING |
4524#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004525 CPU_BASED_CR3_LOAD_EXITING |
4526 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004527 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004528 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004529 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004530 CPU_BASED_MWAIT_EXITING |
4531 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004532 CPU_BASED_INVLPG_EXITING |
4533 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004534
Sheng Yangf78e0e22007-10-29 09:40:42 +08004535 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004536 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004537 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004538 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4539 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004540 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004541#ifdef CONFIG_X86_64
4542 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4543 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4544 ~CPU_BASED_CR8_STORE_EXITING;
4545#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004546 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004547 min2 = 0;
4548 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004549 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004550 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004551 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004552 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004553 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004554 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004555 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004556 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004557 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004558 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004559 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004560 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004561 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004562 SECONDARY_EXEC_RDSEED_EXITING |
4563 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004564 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004565 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004566 SECONDARY_EXEC_ENABLE_VMFUNC |
4567 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004568 if (adjust_vmx_controls(min2, opt2,
4569 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004570 &_cpu_based_2nd_exec_control) < 0)
4571 return -EIO;
4572 }
4573#ifndef CONFIG_X86_64
4574 if (!(_cpu_based_2nd_exec_control &
4575 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4576 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4577#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004578
4579 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4580 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004581 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004582 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4583 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004584
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004585 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4586 &vmx_capability.ept, &vmx_capability.vpid);
4587
Sheng Yangd56f5462008-04-25 10:13:16 +08004588 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004589 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4590 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004591 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4592 CPU_BASED_CR3_STORE_EXITING |
4593 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004594 } else if (vmx_capability.ept) {
4595 vmx_capability.ept = 0;
4596 pr_warn_once("EPT CAP should not exist if not support "
4597 "1-setting enable EPT VM-execution control\n");
4598 }
4599 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4600 vmx_capability.vpid) {
4601 vmx_capability.vpid = 0;
4602 pr_warn_once("VPID CAP should not exist if not support "
4603 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004604 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004605
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004606 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004607#ifdef CONFIG_X86_64
4608 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4609#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004610 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004611 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004612 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4613 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004614 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004615
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004616 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4617 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4618 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004619 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4620 &_pin_based_exec_control) < 0)
4621 return -EIO;
4622
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004623 if (cpu_has_broken_vmx_preemption_timer())
4624 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004625 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004626 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004627 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4628
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004629 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004630 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4632 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004633 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004635 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004636
4637 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4638 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004639 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004640
4641#ifdef CONFIG_X86_64
4642 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4643 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645#endif
4646
4647 /* Require Write-Back (WB) memory type for VMCS accesses. */
4648 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004650
Yang, Sheng002c7f72007-07-31 14:23:01 +03004651 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004652 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004653 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004654
Liran Alon2307af12018-06-29 22:59:04 +03004655 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004656
Yang, Sheng002c7f72007-07-31 14:23:01 +03004657 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4658 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004659 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004660 vmcs_conf->vmexit_ctrl = _vmexit_control;
4661 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004662
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004663 if (static_branch_unlikely(&enable_evmcs))
4664 evmcs_sanitize_exec_ctrls(vmcs_conf);
4665
Avi Kivity110312c2010-12-21 12:54:20 +02004666 cpu_has_load_ia32_efer =
4667 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4668 VM_ENTRY_LOAD_IA32_EFER)
4669 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4670 VM_EXIT_LOAD_IA32_EFER);
4671
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004672 cpu_has_load_perf_global_ctrl =
4673 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4674 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4675 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4676 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4677
4678 /*
4679 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004680 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004681 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4682 *
4683 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4684 *
4685 * AAK155 (model 26)
4686 * AAP115 (model 30)
4687 * AAT100 (model 37)
4688 * BC86,AAY89,BD102 (model 44)
4689 * BA97 (model 46)
4690 *
4691 */
4692 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4693 switch (boot_cpu_data.x86_model) {
4694 case 26:
4695 case 30:
4696 case 37:
4697 case 44:
4698 case 46:
4699 cpu_has_load_perf_global_ctrl = false;
4700 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4701 "does not work properly. Using workaround\n");
4702 break;
4703 default:
4704 break;
4705 }
4706 }
4707
Borislav Petkov782511b2016-04-04 22:25:03 +02004708 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004709 rdmsrl(MSR_IA32_XSS, host_xss);
4710
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004711 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004712}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713
Liran Alon491a6032018-06-23 02:35:12 +03004714static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715{
4716 int node = cpu_to_node(cpu);
4717 struct page *pages;
4718 struct vmcs *vmcs;
4719
Vlastimil Babka96db8002015-09-08 15:03:50 -07004720 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721 if (!pages)
4722 return NULL;
4723 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004724 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004725
4726 /* KVM supports Enlightened VMCS v1 only */
4727 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004728 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004729 else
Liran Alon392b2f22018-06-23 02:35:01 +03004730 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004731
Liran Alon491a6032018-06-23 02:35:12 +03004732 if (shadow)
4733 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734 return vmcs;
4735}
4736
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737static void free_vmcs(struct vmcs *vmcs)
4738{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004739 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740}
4741
Nadav Har'Eld462b812011-05-24 15:26:10 +03004742/*
4743 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4744 */
4745static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4746{
4747 if (!loaded_vmcs->vmcs)
4748 return;
4749 loaded_vmcs_clear(loaded_vmcs);
4750 free_vmcs(loaded_vmcs->vmcs);
4751 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004752 if (loaded_vmcs->msr_bitmap)
4753 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004754 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004755}
4756
Liran Alon491a6032018-06-23 02:35:12 +03004757static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004758{
Liran Alon491a6032018-06-23 02:35:12 +03004759 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004760}
4761
4762static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4763{
Liran Alon491a6032018-06-23 02:35:12 +03004764 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004765 if (!loaded_vmcs->vmcs)
4766 return -ENOMEM;
4767
4768 loaded_vmcs->shadow_vmcs = NULL;
4769 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004770
4771 if (cpu_has_vmx_msr_bitmap()) {
4772 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4773 if (!loaded_vmcs->msr_bitmap)
4774 goto out_vmcs;
4775 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004776
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004777 if (IS_ENABLED(CONFIG_HYPERV) &&
4778 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004779 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4780 struct hv_enlightened_vmcs *evmcs =
4781 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4782
4783 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4784 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004785 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004786
4787 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4788
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004789 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004790
4791out_vmcs:
4792 free_loaded_vmcs(loaded_vmcs);
4793 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004794}
4795
Sam Ravnborg39959582007-06-01 00:47:13 -07004796static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797{
4798 int cpu;
4799
Zachary Amsden3230bb42009-09-29 11:38:37 -10004800 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004802 per_cpu(vmxarea, cpu) = NULL;
4803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804}
4805
Jim Mattsond37f4262017-12-22 12:12:16 -08004806enum vmcs_field_width {
4807 VMCS_FIELD_WIDTH_U16 = 0,
4808 VMCS_FIELD_WIDTH_U64 = 1,
4809 VMCS_FIELD_WIDTH_U32 = 2,
4810 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004811};
4812
Jim Mattsond37f4262017-12-22 12:12:16 -08004813static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004814{
4815 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004816 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004817 return (field >> 13) & 0x3 ;
4818}
4819
4820static inline int vmcs_field_readonly(unsigned long field)
4821{
4822 return (((field >> 10) & 0x3) == 1);
4823}
4824
Bandan Dasfe2b2012014-04-21 15:20:14 -04004825static void init_vmcs_shadow_fields(void)
4826{
4827 int i, j;
4828
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004829 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4830 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004831 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004832 (i + 1 == max_shadow_read_only_fields ||
4833 shadow_read_only_fields[i + 1] != field + 1))
4834 pr_err("Missing field from shadow_read_only_field %x\n",
4835 field + 1);
4836
4837 clear_bit(field, vmx_vmread_bitmap);
4838#ifdef CONFIG_X86_64
4839 if (field & 1)
4840 continue;
4841#endif
4842 if (j < i)
4843 shadow_read_only_fields[j] = field;
4844 j++;
4845 }
4846 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004847
4848 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004849 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004850 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004851 (i + 1 == max_shadow_read_write_fields ||
4852 shadow_read_write_fields[i + 1] != field + 1))
4853 pr_err("Missing field from shadow_read_write_field %x\n",
4854 field + 1);
4855
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004856 /*
4857 * PML and the preemption timer can be emulated, but the
4858 * processor cannot vmwrite to fields that don't exist
4859 * on bare metal.
4860 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004861 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004862 case GUEST_PML_INDEX:
4863 if (!cpu_has_vmx_pml())
4864 continue;
4865 break;
4866 case VMX_PREEMPTION_TIMER_VALUE:
4867 if (!cpu_has_vmx_preemption_timer())
4868 continue;
4869 break;
4870 case GUEST_INTR_STATUS:
4871 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004872 continue;
4873 break;
4874 default:
4875 break;
4876 }
4877
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004878 clear_bit(field, vmx_vmwrite_bitmap);
4879 clear_bit(field, vmx_vmread_bitmap);
4880#ifdef CONFIG_X86_64
4881 if (field & 1)
4882 continue;
4883#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004884 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004885 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004886 j++;
4887 }
4888 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004889}
4890
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891static __init int alloc_kvm_area(void)
4892{
4893 int cpu;
4894
Zachary Amsden3230bb42009-09-29 11:38:37 -10004895 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896 struct vmcs *vmcs;
4897
Liran Alon491a6032018-06-23 02:35:12 +03004898 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 if (!vmcs) {
4900 free_kvm_area();
4901 return -ENOMEM;
4902 }
4903
Liran Alon2307af12018-06-29 22:59:04 +03004904 /*
4905 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4906 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4907 * revision_id reported by MSR_IA32_VMX_BASIC.
4908 *
4909 * However, even though not explictly documented by
4910 * TLFS, VMXArea passed as VMXON argument should
4911 * still be marked with revision_id reported by
4912 * physical CPU.
4913 */
4914 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004915 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004916
Avi Kivity6aa8b732006-12-10 02:21:36 -08004917 per_cpu(vmxarea, cpu) = vmcs;
4918 }
4919 return 0;
4920}
4921
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004922static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004923 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004924{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004925 if (!emulate_invalid_guest_state) {
4926 /*
4927 * CS and SS RPL should be equal during guest entry according
4928 * to VMX spec, but in reality it is not always so. Since vcpu
4929 * is in the middle of the transition from real mode to
4930 * protected mode it is safe to assume that RPL 0 is a good
4931 * default value.
4932 */
4933 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004934 save->selector &= ~SEGMENT_RPL_MASK;
4935 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004936 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004938 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939}
4940
4941static void enter_pmode(struct kvm_vcpu *vcpu)
4942{
4943 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004945
Gleb Natapovd99e4152012-12-20 16:57:45 +02004946 /*
4947 * Update real mode segment cache. It may be not up-to-date if sement
4948 * register was written while vcpu was in a guest mode.
4949 */
4950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4951 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4952 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4953 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4954 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4955 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4956
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004957 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958
Avi Kivity2fb92db2011-04-27 19:42:18 +03004959 vmx_segment_cache_clear(vmx);
4960
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004961 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962
4963 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004964 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4965 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966 vmcs_writel(GUEST_RFLAGS, flags);
4967
Rusty Russell66aee912007-07-17 23:34:16 +10004968 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4969 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004970
4971 update_exception_bitmap(vcpu);
4972
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004973 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4974 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4975 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4976 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4977 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4978 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979}
4980
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004981static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982{
Mathias Krause772e0312012-08-30 01:30:19 +02004983 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004984 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004985
Gleb Natapovd99e4152012-12-20 16:57:45 +02004986 var.dpl = 0x3;
4987 if (seg == VCPU_SREG_CS)
4988 var.type = 0x3;
4989
4990 if (!emulate_invalid_guest_state) {
4991 var.selector = var.base >> 4;
4992 var.base = var.base & 0xffff0;
4993 var.limit = 0xffff;
4994 var.g = 0;
4995 var.db = 0;
4996 var.present = 1;
4997 var.s = 1;
4998 var.l = 0;
4999 var.unusable = 0;
5000 var.type = 0x3;
5001 var.avl = 0;
5002 if (save->base & 0xf)
5003 printk_once(KERN_WARNING "kvm: segment base is not "
5004 "paragraph aligned when entering "
5005 "protected mode (seg=%d)", seg);
5006 }
5007
5008 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005009 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005010 vmcs_write32(sf->limit, var.limit);
5011 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012}
5013
5014static void enter_rmode(struct kvm_vcpu *vcpu)
5015{
5016 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005017 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005018 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5022 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5023 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5024 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005025 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5026 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005027
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005028 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029
Gleb Natapov776e58e2011-03-13 12:34:27 +02005030 /*
5031 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005032 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005033 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005034 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005035 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5036 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005037
Avi Kivity2fb92db2011-04-27 19:42:18 +03005038 vmx_segment_cache_clear(vmx);
5039
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005040 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5043
5044 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005045 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005047 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048
5049 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005050 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 update_exception_bitmap(vcpu);
5052
Gleb Natapovd99e4152012-12-20 16:57:45 +02005053 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5054 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5055 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5056 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5057 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5058 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005059
Eddie Dong8668a3c2007-10-10 14:26:45 +08005060 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061}
5062
Amit Shah401d10d2009-02-20 22:53:37 +05305063static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5064{
5065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005066 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5067
5068 if (!msr)
5069 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305070
Avi Kivityf6801df2010-01-21 15:31:50 +02005071 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305072 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005073 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305074 msr->data = efer;
5075 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005076 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305077
5078 msr->data = efer & ~EFER_LME;
5079 }
5080 setup_msrs(vmx);
5081}
5082
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005083#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005084
5085static void enter_lmode(struct kvm_vcpu *vcpu)
5086{
5087 u32 guest_tr_ar;
5088
Avi Kivity2fb92db2011-04-27 19:42:18 +03005089 vmx_segment_cache_clear(to_vmx(vcpu));
5090
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005092 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005093 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5094 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005096 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5097 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005098 }
Avi Kivityda38f432010-07-06 11:30:49 +03005099 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100}
5101
5102static void exit_lmode(struct kvm_vcpu *vcpu)
5103{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005104 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005105 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106}
5107
5108#endif
5109
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005110static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5111 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005112{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005113 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005114 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005115 return;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005116 ept_sync_context(construct_eptp(vcpu,
5117 vcpu->arch.mmu->root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005118 } else {
5119 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005120 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005121}
5122
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005123static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005124{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005125 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005126}
5127
Junaid Shahidfaff8752018-06-29 13:10:05 -07005128static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5129{
5130 int vpid = to_vmx(vcpu)->vpid;
5131
5132 if (!vpid_sync_vcpu_addr(vpid, addr))
5133 vpid_sync_context(vpid);
5134
5135 /*
5136 * If VPIDs are not supported or enabled, then the above is a no-op.
5137 * But we don't really need a TLB flush in that case anyway, because
5138 * each VM entry/exit includes an implicit flush when VPID is 0.
5139 */
5140}
5141
Avi Kivitye8467fd2009-12-29 18:43:06 +02005142static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5143{
5144 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5145
5146 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5147 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5148}
5149
Avi Kivityaff48ba2010-12-05 18:56:11 +02005150static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5151{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005152 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005153 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5154 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5155}
5156
Anthony Liguori25c4c272007-04-27 09:29:21 +03005157static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005158{
Avi Kivityfc78f512009-12-07 12:16:48 +02005159 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5160
5161 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5162 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005163}
5164
Sheng Yang14394422008-04-28 12:24:45 +08005165static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5166{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005167 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5168
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005169 if (!test_bit(VCPU_EXREG_PDPTR,
5170 (unsigned long *)&vcpu->arch.regs_dirty))
5171 return;
5172
Sheng Yang14394422008-04-28 12:24:45 +08005173 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005174 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5175 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5176 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5177 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005178 }
5179}
5180
Avi Kivity8f5d5492009-05-31 18:41:29 +03005181static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5182{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005183 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5184
Avi Kivity8f5d5492009-05-31 18:41:29 +03005185 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005186 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5187 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5188 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5189 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005190 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005191
5192 __set_bit(VCPU_EXREG_PDPTR,
5193 (unsigned long *)&vcpu->arch.regs_avail);
5194 __set_bit(VCPU_EXREG_PDPTR,
5195 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005196}
5197
David Matlack38991522016-11-29 18:14:08 -08005198static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5199{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005200 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5201 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005202 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5203
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005204 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005205 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5206 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5207 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5208
5209 return fixed_bits_valid(val, fixed0, fixed1);
5210}
5211
5212static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5213{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005214 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5215 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005216
5217 return fixed_bits_valid(val, fixed0, fixed1);
5218}
5219
5220static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5221{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005222 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5223 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005224
5225 return fixed_bits_valid(val, fixed0, fixed1);
5226}
5227
5228/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5229#define nested_guest_cr4_valid nested_cr4_valid
5230#define nested_host_cr4_valid nested_cr4_valid
5231
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005232static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005233
5234static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5235 unsigned long cr0,
5236 struct kvm_vcpu *vcpu)
5237{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005238 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5239 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005240 if (!(cr0 & X86_CR0_PG)) {
5241 /* From paging/starting to nonpaging */
5242 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005243 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005244 (CPU_BASED_CR3_LOAD_EXITING |
5245 CPU_BASED_CR3_STORE_EXITING));
5246 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005247 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005248 } else if (!is_paging(vcpu)) {
5249 /* From nonpaging to paging */
5250 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005251 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005252 ~(CPU_BASED_CR3_LOAD_EXITING |
5253 CPU_BASED_CR3_STORE_EXITING));
5254 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005255 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005256 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005257
5258 if (!(cr0 & X86_CR0_WP))
5259 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005260}
5261
Avi Kivity6aa8b732006-12-10 02:21:36 -08005262static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5263{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005264 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005265 unsigned long hw_cr0;
5266
Sean Christopherson3de63472018-07-13 08:42:30 -07005267 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005268 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005269 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005270 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005271 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005272
Gleb Natapov218e7632013-01-21 15:36:45 +02005273 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5274 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275
Gleb Natapov218e7632013-01-21 15:36:45 +02005276 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5277 enter_rmode(vcpu);
5278 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005280#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005281 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005282 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005283 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005284 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005285 exit_lmode(vcpu);
5286 }
5287#endif
5288
Sean Christophersonb4d18512018-03-05 12:04:40 -08005289 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005290 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5291
Avi Kivity6aa8b732006-12-10 02:21:36 -08005292 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005293 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005294 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005295
5296 /* depends on vcpu->arch.cr0 to be set to a new value */
5297 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005298}
5299
Yu Zhang855feb62017-08-24 20:27:55 +08005300static int get_ept_level(struct kvm_vcpu *vcpu)
5301{
5302 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5303 return 5;
5304 return 4;
5305}
5306
Peter Feiner995f00a2017-06-30 17:26:32 -07005307static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005308{
Yu Zhang855feb62017-08-24 20:27:55 +08005309 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005310
Yu Zhang855feb62017-08-24 20:27:55 +08005311 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005312
Peter Feiner995f00a2017-06-30 17:26:32 -07005313 if (enable_ept_ad_bits &&
5314 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005315 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005316 eptp |= (root_hpa & PAGE_MASK);
5317
5318 return eptp;
5319}
5320
Avi Kivity6aa8b732006-12-10 02:21:36 -08005321static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5322{
Tianyu Lan877ad952018-07-19 08:40:23 +00005323 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005324 unsigned long guest_cr3;
5325 u64 eptp;
5326
5327 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005328 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005329 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005330 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005331
5332 if (kvm_x86_ops->tlb_remote_flush) {
5333 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5334 to_vmx(vcpu)->ept_pointer = eptp;
5335 to_kvm_vmx(kvm)->ept_pointers_match
5336 = EPT_POINTERS_CHECK;
5337 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5338 }
5339
Sean Christophersone90008d2018-03-05 12:04:37 -08005340 if (enable_unrestricted_guest || is_paging(vcpu) ||
5341 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005342 guest_cr3 = kvm_read_cr3(vcpu);
5343 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005344 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005345 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005346 }
5347
Sheng Yang14394422008-04-28 12:24:45 +08005348 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349}
5350
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005351static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005352{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005353 /*
5354 * Pass through host's Machine Check Enable value to hw_cr4, which
5355 * is in force while we are in guest mode. Do not let guests control
5356 * this bit, even if host CR4.MCE == 0.
5357 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005358 unsigned long hw_cr4;
5359
5360 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5361 if (enable_unrestricted_guest)
5362 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5363 else if (to_vmx(vcpu)->rmode.vm86_active)
5364 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5365 else
5366 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005367
Sean Christopherson64f7a112018-04-30 10:01:06 -07005368 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5369 if (cr4 & X86_CR4_UMIP) {
5370 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005371 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005372 hw_cr4 &= ~X86_CR4_UMIP;
5373 } else if (!is_guest_mode(vcpu) ||
5374 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5375 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5376 SECONDARY_EXEC_DESC);
5377 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005378
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005379 if (cr4 & X86_CR4_VMXE) {
5380 /*
5381 * To use VMXON (and later other VMX instructions), a guest
5382 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5383 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005384 * is here. We operate under the default treatment of SMM,
5385 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005386 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005387 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005388 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005389 }
David Matlack38991522016-11-29 18:14:08 -08005390
5391 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005392 return 1;
5393
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005394 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005395
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005396 if (!enable_unrestricted_guest) {
5397 if (enable_ept) {
5398 if (!is_paging(vcpu)) {
5399 hw_cr4 &= ~X86_CR4_PAE;
5400 hw_cr4 |= X86_CR4_PSE;
5401 } else if (!(cr4 & X86_CR4_PAE)) {
5402 hw_cr4 &= ~X86_CR4_PAE;
5403 }
5404 }
5405
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005406 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005407 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5408 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5409 * to be manually disabled when guest switches to non-paging
5410 * mode.
5411 *
5412 * If !enable_unrestricted_guest, the CPU is always running
5413 * with CR0.PG=1 and CR4 needs to be modified.
5414 * If enable_unrestricted_guest, the CPU automatically
5415 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005416 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005417 if (!is_paging(vcpu))
5418 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5419 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005420
Sheng Yang14394422008-04-28 12:24:45 +08005421 vmcs_writel(CR4_READ_SHADOW, cr4);
5422 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005423 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005424}
5425
Avi Kivity6aa8b732006-12-10 02:21:36 -08005426static void vmx_get_segment(struct kvm_vcpu *vcpu,
5427 struct kvm_segment *var, int seg)
5428{
Avi Kivitya9179492011-01-03 14:28:52 +02005429 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005430 u32 ar;
5431
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005432 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005433 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005434 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005435 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005436 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005437 var->base = vmx_read_guest_seg_base(vmx, seg);
5438 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5439 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005440 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005441 var->base = vmx_read_guest_seg_base(vmx, seg);
5442 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5443 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5444 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005445 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446 var->type = ar & 15;
5447 var->s = (ar >> 4) & 1;
5448 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005449 /*
5450 * Some userspaces do not preserve unusable property. Since usable
5451 * segment has to be present according to VMX spec we can use present
5452 * property to amend userspace bug by making unusable segment always
5453 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5454 * segment as unusable.
5455 */
5456 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457 var->avl = (ar >> 12) & 1;
5458 var->l = (ar >> 13) & 1;
5459 var->db = (ar >> 14) & 1;
5460 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005461}
5462
Avi Kivitya9179492011-01-03 14:28:52 +02005463static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5464{
Avi Kivitya9179492011-01-03 14:28:52 +02005465 struct kvm_segment s;
5466
5467 if (to_vmx(vcpu)->rmode.vm86_active) {
5468 vmx_get_segment(vcpu, &s, seg);
5469 return s.base;
5470 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005471 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005472}
5473
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005474static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005475{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005476 struct vcpu_vmx *vmx = to_vmx(vcpu);
5477
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005478 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005479 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005480 else {
5481 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005482 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005483 }
Avi Kivity69c73022011-03-07 15:26:44 +02005484}
5485
Avi Kivity653e3102007-05-07 10:55:37 +03005486static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005487{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005488 u32 ar;
5489
Avi Kivityf0495f92012-06-07 17:06:10 +03005490 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005491 ar = 1 << 16;
5492 else {
5493 ar = var->type & 15;
5494 ar |= (var->s & 1) << 4;
5495 ar |= (var->dpl & 3) << 5;
5496 ar |= (var->present & 1) << 7;
5497 ar |= (var->avl & 1) << 12;
5498 ar |= (var->l & 1) << 13;
5499 ar |= (var->db & 1) << 14;
5500 ar |= (var->g & 1) << 15;
5501 }
Avi Kivity653e3102007-05-07 10:55:37 +03005502
5503 return ar;
5504}
5505
5506static void vmx_set_segment(struct kvm_vcpu *vcpu,
5507 struct kvm_segment *var, int seg)
5508{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005509 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005510 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005511
Avi Kivity2fb92db2011-04-27 19:42:18 +03005512 vmx_segment_cache_clear(vmx);
5513
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005514 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5515 vmx->rmode.segs[seg] = *var;
5516 if (seg == VCPU_SREG_TR)
5517 vmcs_write16(sf->selector, var->selector);
5518 else if (var->s)
5519 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005520 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005521 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005522
Avi Kivity653e3102007-05-07 10:55:37 +03005523 vmcs_writel(sf->base, var->base);
5524 vmcs_write32(sf->limit, var->limit);
5525 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005526
5527 /*
5528 * Fix the "Accessed" bit in AR field of segment registers for older
5529 * qemu binaries.
5530 * IA32 arch specifies that at the time of processor reset the
5531 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005532 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005533 * state vmexit when "unrestricted guest" mode is turned on.
5534 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5535 * tree. Newer qemu binaries with that qemu fix would not need this
5536 * kvm hack.
5537 */
5538 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005539 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005540
Gleb Natapovf924d662012-12-12 19:10:55 +02005541 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005542
5543out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005544 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545}
5546
Avi Kivity6aa8b732006-12-10 02:21:36 -08005547static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5548{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005549 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550
5551 *db = (ar >> 14) & 1;
5552 *l = (ar >> 13) & 1;
5553}
5554
Gleb Natapov89a27f42010-02-16 10:51:48 +02005555static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005557 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5558 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559}
5560
Gleb Natapov89a27f42010-02-16 10:51:48 +02005561static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005562{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005563 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5564 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005565}
5566
Gleb Natapov89a27f42010-02-16 10:51:48 +02005567static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005568{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005569 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5570 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005571}
5572
Gleb Natapov89a27f42010-02-16 10:51:48 +02005573static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005574{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005575 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5576 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005577}
5578
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005579static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5580{
5581 struct kvm_segment var;
5582 u32 ar;
5583
5584 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005585 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005586 if (seg == VCPU_SREG_CS)
5587 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005588 ar = vmx_segment_access_rights(&var);
5589
5590 if (var.base != (var.selector << 4))
5591 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005592 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005593 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005594 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005595 return false;
5596
5597 return true;
5598}
5599
5600static bool code_segment_valid(struct kvm_vcpu *vcpu)
5601{
5602 struct kvm_segment cs;
5603 unsigned int cs_rpl;
5604
5605 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005606 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005607
Avi Kivity1872a3f2009-01-04 23:26:52 +02005608 if (cs.unusable)
5609 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005610 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005611 return false;
5612 if (!cs.s)
5613 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005614 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005615 if (cs.dpl > cs_rpl)
5616 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005617 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005618 if (cs.dpl != cs_rpl)
5619 return false;
5620 }
5621 if (!cs.present)
5622 return false;
5623
5624 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5625 return true;
5626}
5627
5628static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5629{
5630 struct kvm_segment ss;
5631 unsigned int ss_rpl;
5632
5633 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005634 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005635
Avi Kivity1872a3f2009-01-04 23:26:52 +02005636 if (ss.unusable)
5637 return true;
5638 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005639 return false;
5640 if (!ss.s)
5641 return false;
5642 if (ss.dpl != ss_rpl) /* DPL != RPL */
5643 return false;
5644 if (!ss.present)
5645 return false;
5646
5647 return true;
5648}
5649
5650static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5651{
5652 struct kvm_segment var;
5653 unsigned int rpl;
5654
5655 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005656 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005657
Avi Kivity1872a3f2009-01-04 23:26:52 +02005658 if (var.unusable)
5659 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005660 if (!var.s)
5661 return false;
5662 if (!var.present)
5663 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005664 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005665 if (var.dpl < rpl) /* DPL < RPL */
5666 return false;
5667 }
5668
5669 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5670 * rights flags
5671 */
5672 return true;
5673}
5674
5675static bool tr_valid(struct kvm_vcpu *vcpu)
5676{
5677 struct kvm_segment tr;
5678
5679 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5680
Avi Kivity1872a3f2009-01-04 23:26:52 +02005681 if (tr.unusable)
5682 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005683 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005684 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005685 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005686 return false;
5687 if (!tr.present)
5688 return false;
5689
5690 return true;
5691}
5692
5693static bool ldtr_valid(struct kvm_vcpu *vcpu)
5694{
5695 struct kvm_segment ldtr;
5696
5697 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5698
Avi Kivity1872a3f2009-01-04 23:26:52 +02005699 if (ldtr.unusable)
5700 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005701 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005702 return false;
5703 if (ldtr.type != 2)
5704 return false;
5705 if (!ldtr.present)
5706 return false;
5707
5708 return true;
5709}
5710
5711static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5712{
5713 struct kvm_segment cs, ss;
5714
5715 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5716 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5717
Nadav Amitb32a9912015-03-29 16:33:04 +03005718 return ((cs.selector & SEGMENT_RPL_MASK) ==
5719 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005720}
5721
5722/*
5723 * Check if guest state is valid. Returns true if valid, false if
5724 * not.
5725 * We assume that registers are always usable
5726 */
5727static bool guest_state_valid(struct kvm_vcpu *vcpu)
5728{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005729 if (enable_unrestricted_guest)
5730 return true;
5731
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005732 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005733 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005734 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5735 return false;
5736 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5737 return false;
5738 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5739 return false;
5740 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5741 return false;
5742 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5743 return false;
5744 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5745 return false;
5746 } else {
5747 /* protected mode guest state checks */
5748 if (!cs_ss_rpl_check(vcpu))
5749 return false;
5750 if (!code_segment_valid(vcpu))
5751 return false;
5752 if (!stack_segment_valid(vcpu))
5753 return false;
5754 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5755 return false;
5756 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5757 return false;
5758 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5759 return false;
5760 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5761 return false;
5762 if (!tr_valid(vcpu))
5763 return false;
5764 if (!ldtr_valid(vcpu))
5765 return false;
5766 }
5767 /* TODO:
5768 * - Add checks on RIP
5769 * - Add checks on RFLAGS
5770 */
5771
5772 return true;
5773}
5774
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005775static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5776{
5777 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5778}
5779
Mike Dayd77c26f2007-10-08 09:02:08 -04005780static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005781{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005782 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005783 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005784 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005785
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005786 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005787 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005788 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5789 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005790 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005791 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005792 r = kvm_write_guest_page(kvm, fn++, &data,
5793 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005794 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005795 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005796 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5797 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005798 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005799 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5800 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005801 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005802 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005803 r = kvm_write_guest_page(kvm, fn, &data,
5804 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5805 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005806out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005807 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005808 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005809}
5810
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005811static int init_rmode_identity_map(struct kvm *kvm)
5812{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005813 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005814 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005815 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005816 u32 tmp;
5817
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005818 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005819 mutex_lock(&kvm->slots_lock);
5820
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005821 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005822 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005823
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005824 if (!kvm_vmx->ept_identity_map_addr)
5825 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5826 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005827
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005828 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005829 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005830 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005831 goto out2;
5832
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005833 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005834 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5835 if (r < 0)
5836 goto out;
5837 /* Set up identity-mapping pagetable for EPT in real mode */
5838 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5839 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5840 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5841 r = kvm_write_guest_page(kvm, identity_map_pfn,
5842 &tmp, i * sizeof(tmp), sizeof(tmp));
5843 if (r < 0)
5844 goto out;
5845 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005846 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005847
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005848out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005849 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005850
5851out2:
5852 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005853 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005854}
5855
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856static void seg_setup(int seg)
5857{
Mathias Krause772e0312012-08-30 01:30:19 +02005858 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005859 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005860
5861 vmcs_write16(sf->selector, 0);
5862 vmcs_writel(sf->base, 0);
5863 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005864 ar = 0x93;
5865 if (seg == VCPU_SREG_CS)
5866 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005867
5868 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005869}
5870
Sheng Yangf78e0e22007-10-29 09:40:42 +08005871static int alloc_apic_access_page(struct kvm *kvm)
5872{
Xiao Guangrong44841412012-09-07 14:14:20 +08005873 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005874 int r = 0;
5875
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005876 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005877 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005878 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005879 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5880 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005881 if (r)
5882 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005883
Tang Chen73a6d942014-09-11 13:38:00 +08005884 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005885 if (is_error_page(page)) {
5886 r = -EFAULT;
5887 goto out;
5888 }
5889
Tang Chenc24ae0d2014-09-24 15:57:58 +08005890 /*
5891 * Do not pin the page in memory, so that memory hot-unplug
5892 * is able to migrate it.
5893 */
5894 put_page(page);
5895 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005896out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005897 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005898 return r;
5899}
5900
Wanpeng Li991e7a02015-09-16 17:30:05 +08005901static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005902{
5903 int vpid;
5904
Avi Kivity919818a2009-03-23 18:01:29 +02005905 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005906 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005907 spin_lock(&vmx_vpid_lock);
5908 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005909 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005910 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005911 else
5912 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005913 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005914 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005915}
5916
Wanpeng Li991e7a02015-09-16 17:30:05 +08005917static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005918{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005919 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005920 return;
5921 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005922 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005923 spin_unlock(&vmx_vpid_lock);
5924}
5925
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005926static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5927 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005928{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005929 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005930
5931 if (!cpu_has_vmx_msr_bitmap())
5932 return;
5933
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005934 if (static_branch_unlikely(&enable_evmcs))
5935 evmcs_touch_msr_bitmap();
5936
Sheng Yang25c5f222008-03-28 13:18:56 +08005937 /*
5938 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5939 * have the write-low and read-high bitmap offsets the wrong way round.
5940 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5941 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005942 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005943 if (type & MSR_TYPE_R)
5944 /* read-low */
5945 __clear_bit(msr, msr_bitmap + 0x000 / f);
5946
5947 if (type & MSR_TYPE_W)
5948 /* write-low */
5949 __clear_bit(msr, msr_bitmap + 0x800 / f);
5950
Sheng Yang25c5f222008-03-28 13:18:56 +08005951 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5952 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005953 if (type & MSR_TYPE_R)
5954 /* read-high */
5955 __clear_bit(msr, msr_bitmap + 0x400 / f);
5956
5957 if (type & MSR_TYPE_W)
5958 /* write-high */
5959 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5960
5961 }
5962}
5963
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005964static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5965 u32 msr, int type)
5966{
5967 int f = sizeof(unsigned long);
5968
5969 if (!cpu_has_vmx_msr_bitmap())
5970 return;
5971
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005972 if (static_branch_unlikely(&enable_evmcs))
5973 evmcs_touch_msr_bitmap();
5974
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005975 /*
5976 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5977 * have the write-low and read-high bitmap offsets the wrong way round.
5978 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5979 */
5980 if (msr <= 0x1fff) {
5981 if (type & MSR_TYPE_R)
5982 /* read-low */
5983 __set_bit(msr, msr_bitmap + 0x000 / f);
5984
5985 if (type & MSR_TYPE_W)
5986 /* write-low */
5987 __set_bit(msr, msr_bitmap + 0x800 / f);
5988
5989 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5990 msr &= 0x1fff;
5991 if (type & MSR_TYPE_R)
5992 /* read-high */
5993 __set_bit(msr, msr_bitmap + 0x400 / f);
5994
5995 if (type & MSR_TYPE_W)
5996 /* write-high */
5997 __set_bit(msr, msr_bitmap + 0xc00 / f);
5998
5999 }
6000}
6001
6002static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6003 u32 msr, int type, bool value)
6004{
6005 if (value)
6006 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6007 else
6008 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6009}
6010
Wincy Vanf2b93282015-02-03 23:56:03 +08006011/*
6012 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6013 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6014 */
6015static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6016 unsigned long *msr_bitmap_nested,
6017 u32 msr, int type)
6018{
6019 int f = sizeof(unsigned long);
6020
Wincy Vanf2b93282015-02-03 23:56:03 +08006021 /*
6022 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6023 * have the write-low and read-high bitmap offsets the wrong way round.
6024 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6025 */
6026 if (msr <= 0x1fff) {
6027 if (type & MSR_TYPE_R &&
6028 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6029 /* read-low */
6030 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6031
6032 if (type & MSR_TYPE_W &&
6033 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6034 /* write-low */
6035 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6036
6037 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6038 msr &= 0x1fff;
6039 if (type & MSR_TYPE_R &&
6040 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6041 /* read-high */
6042 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6043
6044 if (type & MSR_TYPE_W &&
6045 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6046 /* write-high */
6047 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6048
6049 }
6050}
6051
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006052static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006053{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006054 u8 mode = 0;
6055
6056 if (cpu_has_secondary_exec_ctrls() &&
6057 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6058 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6059 mode |= MSR_BITMAP_MODE_X2APIC;
6060 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6061 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6062 }
6063
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006064 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006065}
6066
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006067#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6068
6069static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6070 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006071{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006072 int msr;
6073
6074 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6075 unsigned word = msr / BITS_PER_LONG;
6076 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6077 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006078 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006079
6080 if (mode & MSR_BITMAP_MODE_X2APIC) {
6081 /*
6082 * TPR reads and writes can be virtualized even if virtual interrupt
6083 * delivery is not in use.
6084 */
6085 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6086 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6087 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6088 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6089 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6090 }
6091 }
6092}
6093
6094static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6095{
6096 struct vcpu_vmx *vmx = to_vmx(vcpu);
6097 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6098 u8 mode = vmx_msr_bitmap_mode(vcpu);
6099 u8 changed = mode ^ vmx->msr_bitmap_mode;
6100
6101 if (!changed)
6102 return;
6103
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006104 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6105 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6106
6107 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006108}
6109
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006110static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006111{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006112 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006113}
6114
David Matlackc9f04402017-08-01 14:00:40 -07006115static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6116{
6117 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6118 gfn_t gfn;
6119
6120 /*
6121 * Don't need to mark the APIC access page dirty; it is never
6122 * written to by the CPU during APIC virtualization.
6123 */
6124
6125 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6126 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6127 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6128 }
6129
6130 if (nested_cpu_has_posted_intr(vmcs12)) {
6131 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6132 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6133 }
6134}
6135
6136
David Hildenbrand6342c502017-01-25 11:58:58 +01006137static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006138{
6139 struct vcpu_vmx *vmx = to_vmx(vcpu);
6140 int max_irr;
6141 void *vapic_page;
6142 u16 status;
6143
David Matlackc9f04402017-08-01 14:00:40 -07006144 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6145 return;
Wincy Van705699a2015-02-03 23:58:17 +08006146
David Matlackc9f04402017-08-01 14:00:40 -07006147 vmx->nested.pi_pending = false;
6148 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6149 return;
Wincy Van705699a2015-02-03 23:58:17 +08006150
David Matlackc9f04402017-08-01 14:00:40 -07006151 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6152 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006153 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006154 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6155 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006156 kunmap(vmx->nested.virtual_apic_page);
6157
6158 status = vmcs_read16(GUEST_INTR_STATUS);
6159 if ((u8)max_irr > ((u8)status & 0xff)) {
6160 status &= ~0xff;
6161 status |= (u8)max_irr;
6162 vmcs_write16(GUEST_INTR_STATUS, status);
6163 }
6164 }
David Matlackc9f04402017-08-01 14:00:40 -07006165
6166 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006167}
6168
Paolo Bonzini7e712682018-10-03 13:44:26 +02006169static u8 vmx_get_rvi(void)
6170{
6171 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6172}
6173
Liran Alone6c67d82018-09-04 10:56:52 +03006174static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6175{
6176 struct vcpu_vmx *vmx = to_vmx(vcpu);
6177 void *vapic_page;
6178 u32 vppr;
6179 int rvi;
6180
6181 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6182 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6183 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6184 return false;
6185
Paolo Bonzini7e712682018-10-03 13:44:26 +02006186 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006187
6188 vapic_page = kmap(vmx->nested.virtual_apic_page);
6189 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6190 kunmap(vmx->nested.virtual_apic_page);
6191
6192 return ((rvi & 0xf0) > (vppr & 0xf0));
6193}
6194
Wincy Van06a55242017-04-28 13:13:59 +08006195static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6196 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006197{
6198#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006199 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6200
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006201 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006202 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006203 * The vector of interrupt to be delivered to vcpu had
6204 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006205 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006206 * Following cases will be reached in this block, and
6207 * we always send a notification event in all cases as
6208 * explained below.
6209 *
6210 * Case 1: vcpu keeps in non-root mode. Sending a
6211 * notification event posts the interrupt to vcpu.
6212 *
6213 * Case 2: vcpu exits to root mode and is still
6214 * runnable. PIR will be synced to vIRR before the
6215 * next vcpu entry. Sending a notification event in
6216 * this case has no effect, as vcpu is not in root
6217 * mode.
6218 *
6219 * Case 3: vcpu exits to root mode and is blocked.
6220 * vcpu_block() has already synced PIR to vIRR and
6221 * never blocks vcpu if vIRR is not cleared. Therefore,
6222 * a blocked vcpu here does not wait for any requested
6223 * interrupts in PIR, and sending a notification event
6224 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006225 */
Feng Wu28b835d2015-09-18 22:29:54 +08006226
Wincy Van06a55242017-04-28 13:13:59 +08006227 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006228 return true;
6229 }
6230#endif
6231 return false;
6232}
6233
Wincy Van705699a2015-02-03 23:58:17 +08006234static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6235 int vector)
6236{
6237 struct vcpu_vmx *vmx = to_vmx(vcpu);
6238
6239 if (is_guest_mode(vcpu) &&
6240 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006241 /*
6242 * If a posted intr is not recognized by hardware,
6243 * we will accomplish it in the next vmentry.
6244 */
6245 vmx->nested.pi_pending = true;
6246 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006247 /* the PIR and ON have been set by L1. */
6248 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6249 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006250 return 0;
6251 }
6252 return -1;
6253}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006254/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006255 * Send interrupt to vcpu via posted interrupt way.
6256 * 1. If target vcpu is running(non-root mode), send posted interrupt
6257 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6258 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6259 * interrupt from PIR in next vmentry.
6260 */
6261static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6262{
6263 struct vcpu_vmx *vmx = to_vmx(vcpu);
6264 int r;
6265
Wincy Van705699a2015-02-03 23:58:17 +08006266 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6267 if (!r)
6268 return;
6269
Yang Zhanga20ed542013-04-11 19:25:15 +08006270 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6271 return;
6272
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006273 /* If a previous notification has sent the IPI, nothing to do. */
6274 if (pi_test_and_set_on(&vmx->pi_desc))
6275 return;
6276
Wincy Van06a55242017-04-28 13:13:59 +08006277 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006278 kvm_vcpu_kick(vcpu);
6279}
6280
Avi Kivity6aa8b732006-12-10 02:21:36 -08006281/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006282 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6283 * will not change in the lifetime of the guest.
6284 * Note that host-state that does change is set elsewhere. E.g., host-state
6285 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6286 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006287static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006288{
6289 u32 low32, high32;
6290 unsigned long tmpl;
6291 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006292 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006293
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006294 cr0 = read_cr0();
6295 WARN_ON(cr0 & X86_CR0_TS);
6296 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006297
6298 /*
6299 * Save the most likely value for this task's CR3 in the VMCS.
6300 * We can't use __get_current_cr3_fast() because we're not atomic.
6301 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006302 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006303 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006304 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006305
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006306 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006307 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006308 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006309 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006310
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006311 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006312#ifdef CONFIG_X86_64
6313 /*
6314 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006315 * vmx_prepare_switch_to_host(), in case userspace uses
6316 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006317 */
6318 vmcs_write16(HOST_DS_SELECTOR, 0);
6319 vmcs_write16(HOST_ES_SELECTOR, 0);
6320#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006321 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6322 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006323#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006324 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6325 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6326
Juergen Gross87930012017-09-04 12:25:27 +02006327 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006328 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006329 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006330
Avi Kivity83287ea422012-09-16 15:10:57 +03006331 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006332
6333 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6334 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6335 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6336 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6337
6338 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6339 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6340 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6341 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07006342
6343 if (cpu_has_load_ia32_efer)
6344 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006345}
6346
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006347static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6348{
6349 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6350 if (enable_ept)
6351 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006352 if (is_guest_mode(&vmx->vcpu))
6353 vmx->vcpu.arch.cr4_guest_owned_bits &=
6354 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006355 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6356}
6357
Yang Zhang01e439b2013-04-11 19:25:12 +08006358static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6359{
6360 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6361
Andrey Smetanind62caab2015-11-10 15:36:33 +03006362 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006363 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006364
6365 if (!enable_vnmi)
6366 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6367
Yunhong Jiang64672c92016-06-13 14:19:59 -07006368 /* Enable the preemption timer dynamically */
6369 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006370 return pin_based_exec_ctrl;
6371}
6372
Andrey Smetanind62caab2015-11-10 15:36:33 +03006373static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6374{
6375 struct vcpu_vmx *vmx = to_vmx(vcpu);
6376
6377 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006378 if (cpu_has_secondary_exec_ctrls()) {
6379 if (kvm_vcpu_apicv_active(vcpu))
6380 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6381 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6382 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6383 else
6384 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6385 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6386 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6387 }
6388
6389 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006390 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006391}
6392
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006393static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6394{
6395 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006396
6397 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6398 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6399
Paolo Bonzini35754c92015-07-29 12:05:37 +02006400 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006401 exec_control &= ~CPU_BASED_TPR_SHADOW;
6402#ifdef CONFIG_X86_64
6403 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6404 CPU_BASED_CR8_LOAD_EXITING;
6405#endif
6406 }
6407 if (!enable_ept)
6408 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6409 CPU_BASED_CR3_LOAD_EXITING |
6410 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006411 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6412 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6413 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006414 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6415 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006416 return exec_control;
6417}
6418
Jim Mattson45ec3682017-08-23 16:32:04 -07006419static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006420{
Jim Mattson45ec3682017-08-23 16:32:04 -07006421 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006422 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006423}
6424
Jim Mattson75f4fc82017-08-23 16:32:03 -07006425static bool vmx_rdseed_supported(void)
6426{
6427 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006428 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006429}
6430
Paolo Bonzini80154d72017-08-24 13:55:35 +02006431static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006432{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006433 struct kvm_vcpu *vcpu = &vmx->vcpu;
6434
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006435 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006436
Paolo Bonzini80154d72017-08-24 13:55:35 +02006437 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006438 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6439 if (vmx->vpid == 0)
6440 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6441 if (!enable_ept) {
6442 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6443 enable_unrestricted_guest = 0;
6444 }
6445 if (!enable_unrestricted_guest)
6446 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006447 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006448 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006449 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006450 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6451 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006452 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006453
6454 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6455 * in vmx_set_cr4. */
6456 exec_control &= ~SECONDARY_EXEC_DESC;
6457
Abel Gordonabc4fc52013-04-18 14:35:25 +03006458 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6459 (handle_vmptrld).
6460 We can NOT enable shadow_vmcs here because we don't have yet
6461 a current VMCS12
6462 */
6463 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006464
6465 if (!enable_pml)
6466 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006467
Paolo Bonzini3db13482017-08-24 14:48:03 +02006468 if (vmx_xsaves_supported()) {
6469 /* Exposing XSAVES only when XSAVE is exposed */
6470 bool xsaves_enabled =
6471 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6472 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6473
6474 if (!xsaves_enabled)
6475 exec_control &= ~SECONDARY_EXEC_XSAVES;
6476
6477 if (nested) {
6478 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006479 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006480 SECONDARY_EXEC_XSAVES;
6481 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006482 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006483 ~SECONDARY_EXEC_XSAVES;
6484 }
6485 }
6486
Paolo Bonzini80154d72017-08-24 13:55:35 +02006487 if (vmx_rdtscp_supported()) {
6488 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6489 if (!rdtscp_enabled)
6490 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6491
6492 if (nested) {
6493 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006494 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006495 SECONDARY_EXEC_RDTSCP;
6496 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006497 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006498 ~SECONDARY_EXEC_RDTSCP;
6499 }
6500 }
6501
6502 if (vmx_invpcid_supported()) {
6503 /* Exposing INVPCID only when PCID is exposed */
6504 bool invpcid_enabled =
6505 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6506 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6507
6508 if (!invpcid_enabled) {
6509 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6510 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6511 }
6512
6513 if (nested) {
6514 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006515 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006516 SECONDARY_EXEC_ENABLE_INVPCID;
6517 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006518 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006519 ~SECONDARY_EXEC_ENABLE_INVPCID;
6520 }
6521 }
6522
Jim Mattson45ec3682017-08-23 16:32:04 -07006523 if (vmx_rdrand_supported()) {
6524 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6525 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006526 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006527
6528 if (nested) {
6529 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006530 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006531 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006532 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006533 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006534 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006535 }
6536 }
6537
Jim Mattson75f4fc82017-08-23 16:32:03 -07006538 if (vmx_rdseed_supported()) {
6539 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6540 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006541 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006542
6543 if (nested) {
6544 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006545 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006546 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006547 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006548 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006549 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006550 }
6551 }
6552
Paolo Bonzini80154d72017-08-24 13:55:35 +02006553 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006554}
6555
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006556static void ept_set_mmio_spte_mask(void)
6557{
6558 /*
6559 * EPT Misconfigurations can be generated if the value of bits 2:0
6560 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006561 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006562 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6563 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006564}
6565
Wanpeng Lif53cd632014-12-02 19:14:58 +08006566#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006567/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006568 * Sets up the vmcs for emulated real mode.
6569 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006570static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006571{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006572 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006573
Abel Gordon4607c2d2013-04-18 14:35:55 +03006574 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006575 /*
6576 * At vCPU creation, "VMWRITE to any supported field
6577 * in the VMCS" is supported, so use the more
6578 * permissive vmx_vmread_bitmap to specify both read
6579 * and write permissions for the shadow VMCS.
6580 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006581 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006582 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006583 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006584 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006585 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006586
Avi Kivity6aa8b732006-12-10 02:21:36 -08006587 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6588
Avi Kivity6aa8b732006-12-10 02:21:36 -08006589 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006590 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006591 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006592
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006593 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006594
Dan Williamsdfa169b2016-06-02 11:17:24 -07006595 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006596 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006597 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006598 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006599 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006600
Andrey Smetanind62caab2015-11-10 15:36:33 +03006601 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006602 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6603 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6604 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6605 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6606
6607 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006608
Li RongQing0bcf2612015-12-03 13:29:34 +08006609 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006610 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006611 }
6612
Wanpeng Lib31c1142018-03-12 04:53:04 -07006613 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006614 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006615 vmx->ple_window = ple_window;
6616 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006617 }
6618
Xiao Guangrongc3707952011-07-12 03:28:04 +08006619 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6620 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006621 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6622
Avi Kivity9581d442010-10-19 16:46:55 +02006623 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6624 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006625 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006626 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6627 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006628
Bandan Das2a499e42017-08-03 15:54:41 -04006629 if (cpu_has_vmx_vmfunc())
6630 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6631
Eddie Dong2cc51562007-05-21 07:28:09 +03006632 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6633 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006634 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006635 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006636 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637
Radim Krčmář74545702015-04-27 15:11:25 +02006638 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6639 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006640
Paolo Bonzini03916db2014-07-24 14:21:57 +02006641 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006642 u32 index = vmx_msr_index[i];
6643 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006644 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006645
6646 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6647 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006648 if (wrmsr_safe(index, data_low, data_high) < 0)
6649 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006650 vmx->guest_msrs[j].index = i;
6651 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006652 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006653 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006654 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006655
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006656 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006657
6658 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006659
6660 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006661 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006662
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006663 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6664 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6665
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006666 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006667
Wanpeng Lif53cd632014-12-02 19:14:58 +08006668 if (vmx_xsaves_supported())
6669 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6670
Peter Feiner4e595162016-07-07 14:49:58 -07006671 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006672 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6673 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6674 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006675
6676 if (cpu_has_vmx_encls_vmexit())
6677 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006678}
6679
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006680static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006681{
6682 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006683 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006684 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006685
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006686 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006687 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006688
Wanpeng Li518e7b92018-02-28 14:03:31 +08006689 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006690 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006691 kvm_set_cr8(vcpu, 0);
6692
6693 if (!init_event) {
6694 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6695 MSR_IA32_APICBASE_ENABLE;
6696 if (kvm_vcpu_is_reset_bsp(vcpu))
6697 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6698 apic_base_msr.host_initiated = true;
6699 kvm_set_apic_base(vcpu, &apic_base_msr);
6700 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006701
Avi Kivity2fb92db2011-04-27 19:42:18 +03006702 vmx_segment_cache_clear(vmx);
6703
Avi Kivity5706be02008-08-20 15:07:31 +03006704 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006705 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006706 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006707
6708 seg_setup(VCPU_SREG_DS);
6709 seg_setup(VCPU_SREG_ES);
6710 seg_setup(VCPU_SREG_FS);
6711 seg_setup(VCPU_SREG_GS);
6712 seg_setup(VCPU_SREG_SS);
6713
6714 vmcs_write16(GUEST_TR_SELECTOR, 0);
6715 vmcs_writel(GUEST_TR_BASE, 0);
6716 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6717 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6718
6719 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6720 vmcs_writel(GUEST_LDTR_BASE, 0);
6721 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6722 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6723
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006724 if (!init_event) {
6725 vmcs_write32(GUEST_SYSENTER_CS, 0);
6726 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6727 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6728 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6729 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006730
Wanpeng Lic37c2872017-11-20 14:52:21 -08006731 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006732 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006733
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006734 vmcs_writel(GUEST_GDTR_BASE, 0);
6735 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6736
6737 vmcs_writel(GUEST_IDTR_BASE, 0);
6738 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6739
Anthony Liguori443381a2010-12-06 10:53:38 -06006740 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006741 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006742 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006743 if (kvm_mpx_supported())
6744 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006745
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006746 setup_msrs(vmx);
6747
Avi Kivity6aa8b732006-12-10 02:21:36 -08006748 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6749
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006750 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006751 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006752 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006753 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006754 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006755 vmcs_write32(TPR_THRESHOLD, 0);
6756 }
6757
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006758 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006759
Sheng Yang2384d2b2008-01-17 15:14:33 +08006760 if (vmx->vpid != 0)
6761 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6762
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006763 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006764 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006765 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006766 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006767 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006768
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006769 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006770
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006771 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006772 if (init_event)
6773 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006774}
6775
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006776/*
6777 * In nested virtualization, check if L1 asked to exit on external interrupts.
6778 * For most existing hypervisors, this will always return true.
6779 */
6780static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6781{
6782 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6783 PIN_BASED_EXT_INTR_MASK;
6784}
6785
Bandan Das77b0f5d2014-04-19 18:17:45 -04006786/*
6787 * In nested virtualization, check if L1 has set
6788 * VM_EXIT_ACK_INTR_ON_EXIT
6789 */
6790static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6791{
6792 return get_vmcs12(vcpu)->vm_exit_controls &
6793 VM_EXIT_ACK_INTR_ON_EXIT;
6794}
6795
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006796static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6797{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006798 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006799}
6800
Jan Kiszkac9a79532014-03-07 20:03:15 +01006801static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006802{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006803 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6804 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006805}
6806
Jan Kiszkac9a79532014-03-07 20:03:15 +01006807static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006808{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006809 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006810 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006811 enable_irq_window(vcpu);
6812 return;
6813 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006814
Paolo Bonzini47c01522016-12-19 11:44:07 +01006815 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6816 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006817}
6818
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006819static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006820{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006821 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006822 uint32_t intr;
6823 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006824
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006825 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006826
Avi Kivityfa89a812008-09-01 15:57:51 +03006827 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006828 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006829 int inc_eip = 0;
6830 if (vcpu->arch.interrupt.soft)
6831 inc_eip = vcpu->arch.event_exit_inst_len;
6832 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006833 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006834 return;
6835 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006836 intr = irq | INTR_INFO_VALID_MASK;
6837 if (vcpu->arch.interrupt.soft) {
6838 intr |= INTR_TYPE_SOFT_INTR;
6839 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6840 vmx->vcpu.arch.event_exit_inst_len);
6841 } else
6842 intr |= INTR_TYPE_EXT_INTR;
6843 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006844
6845 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006846}
6847
Sheng Yangf08864b2008-05-15 18:23:25 +08006848static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6849{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006850 struct vcpu_vmx *vmx = to_vmx(vcpu);
6851
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006852 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006853 /*
6854 * Tracking the NMI-blocked state in software is built upon
6855 * finding the next open IRQ window. This, in turn, depends on
6856 * well-behaving guests: They have to keep IRQs disabled at
6857 * least as long as the NMI handler runs. Otherwise we may
6858 * cause NMI nesting, maybe breaking the guest. But as this is
6859 * highly unlikely, we can live with the residual risk.
6860 */
6861 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6862 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6863 }
6864
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006865 ++vcpu->stat.nmi_injections;
6866 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006867
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006868 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006869 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006870 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006871 return;
6872 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006873
Sheng Yangf08864b2008-05-15 18:23:25 +08006874 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6875 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006876
6877 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006878}
6879
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006880static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6881{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006882 struct vcpu_vmx *vmx = to_vmx(vcpu);
6883 bool masked;
6884
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006885 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006886 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006887 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006888 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006889 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6890 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6891 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006892}
6893
6894static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6895{
6896 struct vcpu_vmx *vmx = to_vmx(vcpu);
6897
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006898 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006899 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6900 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6901 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6902 }
6903 } else {
6904 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6905 if (masked)
6906 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6907 GUEST_INTR_STATE_NMI);
6908 else
6909 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6910 GUEST_INTR_STATE_NMI);
6911 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006912}
6913
Jan Kiszka2505dc92013-04-14 12:12:47 +02006914static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6915{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006916 if (to_vmx(vcpu)->nested.nested_run_pending)
6917 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006918
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006919 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006920 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6921 return 0;
6922
Jan Kiszka2505dc92013-04-14 12:12:47 +02006923 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6924 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6925 | GUEST_INTR_STATE_NMI));
6926}
6927
Gleb Natapov78646122009-03-23 12:12:11 +02006928static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6929{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006930 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6931 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006932 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6933 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006934}
6935
Izik Eiduscbc94022007-10-25 00:29:55 +02006936static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6937{
6938 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006939
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006940 if (enable_unrestricted_guest)
6941 return 0;
6942
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006943 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6944 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006945 if (ret)
6946 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006947 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006948 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006949}
6950
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006951static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6952{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006953 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006954 return 0;
6955}
6956
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006957static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006958{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006959 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006960 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006961 /*
6962 * Update instruction length as we may reinject the exception
6963 * from user space while in guest debugging mode.
6964 */
6965 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6966 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006967 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006968 return false;
6969 /* fall through */
6970 case DB_VECTOR:
6971 if (vcpu->guest_debug &
6972 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6973 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006974 /* fall through */
6975 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006976 case OF_VECTOR:
6977 case BR_VECTOR:
6978 case UD_VECTOR:
6979 case DF_VECTOR:
6980 case SS_VECTOR:
6981 case GP_VECTOR:
6982 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006983 return true;
6984 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006985 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006986 return false;
6987}
6988
6989static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6990 int vec, u32 err_code)
6991{
6992 /*
6993 * Instruction with address size override prefix opcode 0x67
6994 * Cause the #SS fault with 0 error code in VM86 mode.
6995 */
6996 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006997 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006998 if (vcpu->arch.halt_request) {
6999 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007000 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007001 }
7002 return 1;
7003 }
7004 return 0;
7005 }
7006
7007 /*
7008 * Forward all other exceptions that are valid in real mode.
7009 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7010 * the required debugging infrastructure rework.
7011 */
7012 kvm_queue_exception(vcpu, vec);
7013 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007014}
7015
Andi Kleena0861c02009-06-08 17:37:09 +08007016/*
7017 * Trigger machine check on the host. We assume all the MSRs are already set up
7018 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7019 * We pass a fake environment to the machine check handler because we want
7020 * the guest to be always treated like user space, no matter what context
7021 * it used internally.
7022 */
7023static void kvm_machine_check(void)
7024{
7025#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7026 struct pt_regs regs = {
7027 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7028 .flags = X86_EFLAGS_IF,
7029 };
7030
7031 do_machine_check(&regs, 0);
7032#endif
7033}
7034
Avi Kivity851ba692009-08-24 11:10:17 +03007035static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007036{
7037 /* already handled by vcpu_run */
7038 return 1;
7039}
7040
Avi Kivity851ba692009-08-24 11:10:17 +03007041static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007042{
Avi Kivity1155f762007-11-22 11:30:47 +02007043 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007044 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007045 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007046 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007047 u32 vect_info;
7048 enum emulation_result er;
7049
Avi Kivity1155f762007-11-22 11:30:47 +02007050 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007051 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007052
Andi Kleena0861c02009-06-08 17:37:09 +08007053 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007054 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007055
Jim Mattsonef85b672016-12-12 11:01:37 -08007056 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007057 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007058
Wanpeng Li082d06e2018-04-03 16:28:48 -07007059 if (is_invalid_opcode(intr_info))
7060 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007061
Avi Kivity6aa8b732006-12-10 02:21:36 -08007062 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007063 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007064 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007065
Liran Alon9e869482018-03-12 13:12:51 +02007066 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7067 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007068 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007069 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7070 if (er == EMULATE_USER_EXIT)
7071 return 0;
7072 else if (er != EMULATE_DONE)
7073 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7074 return 1;
7075 }
7076
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007077 /*
7078 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7079 * MMIO, it is better to report an internal error.
7080 * See the comments in vmx_handle_exit.
7081 */
7082 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7083 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7084 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7085 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007086 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007087 vcpu->run->internal.data[0] = vect_info;
7088 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007089 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007090 return 0;
7091 }
7092
Avi Kivity6aa8b732006-12-10 02:21:36 -08007093 if (is_page_fault(intr_info)) {
7094 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007095 /* EPT won't cause page fault directly */
7096 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007097 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007098 }
7099
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007100 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007101
7102 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7103 return handle_rmode_exception(vcpu, ex_no, error_code);
7104
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007105 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007106 case AC_VECTOR:
7107 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7108 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007109 case DB_VECTOR:
7110 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7111 if (!(vcpu->guest_debug &
7112 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007113 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007114 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007115 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007116 skip_emulated_instruction(vcpu);
7117
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007118 kvm_queue_exception(vcpu, DB_VECTOR);
7119 return 1;
7120 }
7121 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7122 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7123 /* fall through */
7124 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007125 /*
7126 * Update instruction length as we may reinject #BP from
7127 * user space while in guest debugging mode. Reading it for
7128 * #DB as well causes no harm, it is not used in that case.
7129 */
7130 vmx->vcpu.arch.event_exit_inst_len =
7131 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007132 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007133 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007134 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7135 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007136 break;
7137 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007138 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7139 kvm_run->ex.exception = ex_no;
7140 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007141 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007143 return 0;
7144}
7145
Avi Kivity851ba692009-08-24 11:10:17 +03007146static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007148 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007149 return 1;
7150}
7151
Avi Kivity851ba692009-08-24 11:10:17 +03007152static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007153{
Avi Kivity851ba692009-08-24 11:10:17 +03007154 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007155 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007156 return 0;
7157}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007158
Avi Kivity851ba692009-08-24 11:10:17 +03007159static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007160{
He, Qingbfdaab02007-09-12 14:18:28 +08007161 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007162 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007163 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007164
He, Qingbfdaab02007-09-12 14:18:28 +08007165 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007166 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007167
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007168 ++vcpu->stat.io_exits;
7169
Sean Christopherson432baf62018-03-08 08:57:26 -08007170 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007171 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007172
7173 port = exit_qualification >> 16;
7174 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007175 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007176
Sean Christophersondca7f122018-03-08 08:57:27 -08007177 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007178}
7179
Ingo Molnar102d8322007-02-19 14:37:47 +02007180static void
7181vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7182{
7183 /*
7184 * Patch in the VMCALL instruction:
7185 */
7186 hypercall[0] = 0x0f;
7187 hypercall[1] = 0x01;
7188 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007189}
7190
Guo Chao0fa06072012-06-28 15:16:19 +08007191/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007192static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7193{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007194 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007195 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7196 unsigned long orig_val = val;
7197
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007198 /*
7199 * We get here when L2 changed cr0 in a way that did not change
7200 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007201 * but did change L0 shadowed bits. So we first calculate the
7202 * effective cr0 value that L1 would like to write into the
7203 * hardware. It consists of the L2-owned bits from the new
7204 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007205 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007206 val = (val & ~vmcs12->cr0_guest_host_mask) |
7207 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7208
David Matlack38991522016-11-29 18:14:08 -08007209 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007210 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007211
7212 if (kvm_set_cr0(vcpu, val))
7213 return 1;
7214 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007215 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007216 } else {
7217 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007218 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007219 return 1;
David Matlack38991522016-11-29 18:14:08 -08007220
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007221 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007222 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007223}
7224
7225static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7226{
7227 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007228 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7229 unsigned long orig_val = val;
7230
7231 /* analogously to handle_set_cr0 */
7232 val = (val & ~vmcs12->cr4_guest_host_mask) |
7233 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7234 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007235 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007236 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007237 return 0;
7238 } else
7239 return kvm_set_cr4(vcpu, val);
7240}
7241
Paolo Bonzini0367f202016-07-12 10:44:55 +02007242static int handle_desc(struct kvm_vcpu *vcpu)
7243{
7244 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007245 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007246}
7247
Avi Kivity851ba692009-08-24 11:10:17 +03007248static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007249{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007250 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007251 int cr;
7252 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007253 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007254 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007255
He, Qingbfdaab02007-09-12 14:18:28 +08007256 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007257 cr = exit_qualification & 15;
7258 reg = (exit_qualification >> 8) & 15;
7259 switch ((exit_qualification >> 4) & 3) {
7260 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007261 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007262 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007263 switch (cr) {
7264 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007265 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007266 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007267 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007268 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007269 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007270 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007271 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007272 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007273 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007274 case 8: {
7275 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007276 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007277 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007278 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007279 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007280 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007281 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007282 return ret;
7283 /*
7284 * TODO: we might be squashing a
7285 * KVM_GUESTDBG_SINGLESTEP-triggered
7286 * KVM_EXIT_DEBUG here.
7287 */
Avi Kivity851ba692009-08-24 11:10:17 +03007288 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007289 return 0;
7290 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007291 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007293 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007294 WARN_ONCE(1, "Guest should always own CR0.TS");
7295 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007296 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007297 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007298 case 1: /*mov from cr*/
7299 switch (cr) {
7300 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007301 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007302 val = kvm_read_cr3(vcpu);
7303 kvm_register_write(vcpu, reg, val);
7304 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007305 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007306 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007307 val = kvm_get_cr8(vcpu);
7308 kvm_register_write(vcpu, reg, val);
7309 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007310 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007311 }
7312 break;
7313 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007314 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007315 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007316 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007317
Kyle Huey6affcbe2016-11-29 12:40:40 -08007318 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007319 default:
7320 break;
7321 }
Avi Kivity851ba692009-08-24 11:10:17 +03007322 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007323 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007324 (int)(exit_qualification >> 4) & 3, cr);
7325 return 0;
7326}
7327
Avi Kivity851ba692009-08-24 11:10:17 +03007328static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007329{
He, Qingbfdaab02007-09-12 14:18:28 +08007330 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007331 int dr, dr7, reg;
7332
7333 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7334 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7335
7336 /* First, if DR does not exist, trigger UD */
7337 if (!kvm_require_dr(vcpu, dr))
7338 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007339
Jan Kiszkaf2483412010-01-20 18:20:20 +01007340 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007341 if (!kvm_require_cpl(vcpu, 0))
7342 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007343 dr7 = vmcs_readl(GUEST_DR7);
7344 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007345 /*
7346 * As the vm-exit takes precedence over the debug trap, we
7347 * need to emulate the latter, either for the host or the
7348 * guest debugging itself.
7349 */
7350 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007351 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007352 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007353 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007354 vcpu->run->debug.arch.exception = DB_VECTOR;
7355 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007356 return 0;
7357 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007358 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007359 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007360 kvm_queue_exception(vcpu, DB_VECTOR);
7361 return 1;
7362 }
7363 }
7364
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007365 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007366 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7367 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007368
7369 /*
7370 * No more DR vmexits; force a reload of the debug registers
7371 * and reenter on this instruction. The next vmexit will
7372 * retrieve the full state of the debug registers.
7373 */
7374 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7375 return 1;
7376 }
7377
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007378 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7379 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007380 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007381
7382 if (kvm_get_dr(vcpu, dr, &val))
7383 return 1;
7384 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007385 } else
Nadav Amit57773922014-06-18 17:19:23 +03007386 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007387 return 1;
7388
Kyle Huey6affcbe2016-11-29 12:40:40 -08007389 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007390}
7391
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007392static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7393{
7394 return vcpu->arch.dr6;
7395}
7396
7397static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7398{
7399}
7400
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007401static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7402{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007403 get_debugreg(vcpu->arch.db[0], 0);
7404 get_debugreg(vcpu->arch.db[1], 1);
7405 get_debugreg(vcpu->arch.db[2], 2);
7406 get_debugreg(vcpu->arch.db[3], 3);
7407 get_debugreg(vcpu->arch.dr6, 6);
7408 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7409
7410 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007411 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007412}
7413
Gleb Natapov020df072010-04-13 10:05:23 +03007414static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7415{
7416 vmcs_writel(GUEST_DR7, val);
7417}
7418
Avi Kivity851ba692009-08-24 11:10:17 +03007419static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007420{
Kyle Huey6a908b62016-11-29 12:40:37 -08007421 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007422}
7423
Avi Kivity851ba692009-08-24 11:10:17 +03007424static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007425{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007426 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007427 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007428
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007429 msr_info.index = ecx;
7430 msr_info.host_initiated = false;
7431 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007432 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007433 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007434 return 1;
7435 }
7436
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007437 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007438
Avi Kivity6aa8b732006-12-10 02:21:36 -08007439 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007440 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7441 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007442 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007443}
7444
Avi Kivity851ba692009-08-24 11:10:17 +03007445static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007446{
Will Auld8fe8ab42012-11-29 12:42:12 -08007447 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007448 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7449 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7450 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007451
Will Auld8fe8ab42012-11-29 12:42:12 -08007452 msr.data = data;
7453 msr.index = ecx;
7454 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007455 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007456 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007457 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007458 return 1;
7459 }
7460
Avi Kivity59200272010-01-25 19:47:02 +02007461 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007462 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007463}
7464
Avi Kivity851ba692009-08-24 11:10:17 +03007465static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007466{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007467 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007468 return 1;
7469}
7470
Avi Kivity851ba692009-08-24 11:10:17 +03007471static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007472{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007473 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7474 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007475
Avi Kivity3842d132010-07-27 12:30:24 +03007476 kvm_make_request(KVM_REQ_EVENT, vcpu);
7477
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007478 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007479 return 1;
7480}
7481
Avi Kivity851ba692009-08-24 11:10:17 +03007482static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007483{
Avi Kivityd3bef152007-06-05 15:53:05 +03007484 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007485}
7486
Avi Kivity851ba692009-08-24 11:10:17 +03007487static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007488{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007489 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007490}
7491
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007492static int handle_invd(struct kvm_vcpu *vcpu)
7493{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007494 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007495}
7496
Avi Kivity851ba692009-08-24 11:10:17 +03007497static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007498{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007499 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007500
7501 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007502 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007503}
7504
Avi Kivityfee84b02011-11-10 14:57:25 +02007505static int handle_rdpmc(struct kvm_vcpu *vcpu)
7506{
7507 int err;
7508
7509 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007510 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007511}
7512
Avi Kivity851ba692009-08-24 11:10:17 +03007513static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007514{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007515 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007516}
7517
Dexuan Cui2acf9232010-06-10 11:27:12 +08007518static int handle_xsetbv(struct kvm_vcpu *vcpu)
7519{
7520 u64 new_bv = kvm_read_edx_eax(vcpu);
7521 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7522
7523 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007524 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007525 return 1;
7526}
7527
Wanpeng Lif53cd632014-12-02 19:14:58 +08007528static int handle_xsaves(struct kvm_vcpu *vcpu)
7529{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007530 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007531 WARN(1, "this should never happen\n");
7532 return 1;
7533}
7534
7535static int handle_xrstors(struct kvm_vcpu *vcpu)
7536{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007537 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007538 WARN(1, "this should never happen\n");
7539 return 1;
7540}
7541
Avi Kivity851ba692009-08-24 11:10:17 +03007542static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007543{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007544 if (likely(fasteoi)) {
7545 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7546 int access_type, offset;
7547
7548 access_type = exit_qualification & APIC_ACCESS_TYPE;
7549 offset = exit_qualification & APIC_ACCESS_OFFSET;
7550 /*
7551 * Sane guest uses MOV to write EOI, with written value
7552 * not cared. So make a short-circuit here by avoiding
7553 * heavy instruction emulation.
7554 */
7555 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7556 (offset == APIC_EOI)) {
7557 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007558 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007559 }
7560 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007561 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007562}
7563
Yang Zhangc7c9c562013-01-25 10:18:51 +08007564static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7565{
7566 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7567 int vector = exit_qualification & 0xff;
7568
7569 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7570 kvm_apic_set_eoi_accelerated(vcpu, vector);
7571 return 1;
7572}
7573
Yang Zhang83d4c282013-01-25 10:18:49 +08007574static int handle_apic_write(struct kvm_vcpu *vcpu)
7575{
7576 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7577 u32 offset = exit_qualification & 0xfff;
7578
7579 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7580 kvm_apic_write_nodecode(vcpu, offset);
7581 return 1;
7582}
7583
Avi Kivity851ba692009-08-24 11:10:17 +03007584static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007585{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007586 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007587 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007588 bool has_error_code = false;
7589 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007590 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007591 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007592
7593 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007594 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007595 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007596
7597 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7598
7599 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007600 if (reason == TASK_SWITCH_GATE && idt_v) {
7601 switch (type) {
7602 case INTR_TYPE_NMI_INTR:
7603 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007604 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007605 break;
7606 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007607 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007608 kvm_clear_interrupt_queue(vcpu);
7609 break;
7610 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007611 if (vmx->idt_vectoring_info &
7612 VECTORING_INFO_DELIVER_CODE_MASK) {
7613 has_error_code = true;
7614 error_code =
7615 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7616 }
7617 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007618 case INTR_TYPE_SOFT_EXCEPTION:
7619 kvm_clear_exception_queue(vcpu);
7620 break;
7621 default:
7622 break;
7623 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007624 }
Izik Eidus37817f22008-03-24 23:14:53 +02007625 tss_selector = exit_qualification;
7626
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007627 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7628 type != INTR_TYPE_EXT_INTR &&
7629 type != INTR_TYPE_NMI_INTR))
7630 skip_emulated_instruction(vcpu);
7631
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007632 if (kvm_task_switch(vcpu, tss_selector,
7633 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7634 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007635 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7636 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7637 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007638 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007639 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007640
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007641 /*
7642 * TODO: What about debug traps on tss switch?
7643 * Are we supposed to inject them and update dr6?
7644 */
7645
7646 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007647}
7648
Avi Kivity851ba692009-08-24 11:10:17 +03007649static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007650{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007651 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007652 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007653 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007654
Sheng Yangf9c617f2009-03-25 10:08:52 +08007655 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007656
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007657 /*
7658 * EPT violation happened while executing iret from NMI,
7659 * "blocked by NMI" bit has to be set before next VM entry.
7660 * There are errata that may cause this bit to not be set:
7661 * AAK134, BY25.
7662 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007663 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007664 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007665 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007666 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7667
Sheng Yang14394422008-04-28 12:24:45 +08007668 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007669 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007670
Junaid Shahid27959a42016-12-06 16:46:10 -08007671 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007672 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007673 ? PFERR_USER_MASK : 0;
7674 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007675 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007676 ? PFERR_WRITE_MASK : 0;
7677 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007678 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007679 ? PFERR_FETCH_MASK : 0;
7680 /* ept page table entry is present? */
7681 error_code |= (exit_qualification &
7682 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7683 EPT_VIOLATION_EXECUTABLE))
7684 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007685
Paolo Bonzinieebed242016-11-28 14:39:58 +01007686 error_code |= (exit_qualification & 0x100) != 0 ?
7687 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007688
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007689 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007690 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007691}
7692
Avi Kivity851ba692009-08-24 11:10:17 +03007693static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007694{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007695 gpa_t gpa;
7696
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007697 /*
7698 * A nested guest cannot optimize MMIO vmexits, because we have an
7699 * nGPA here instead of the required GPA.
7700 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007701 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007702 if (!is_guest_mode(vcpu) &&
7703 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007704 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007705 /*
7706 * Doing kvm_skip_emulated_instruction() depends on undefined
7707 * behavior: Intel's manual doesn't mandate
7708 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7709 * occurs and while on real hardware it was observed to be set,
7710 * other hypervisors (namely Hyper-V) don't set it, we end up
7711 * advancing IP with some random value. Disable fast mmio when
7712 * running nested and keep it for real hardware in hope that
7713 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7714 */
7715 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7716 return kvm_skip_emulated_instruction(vcpu);
7717 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007718 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007719 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007720 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007721
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007722 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007723}
7724
Avi Kivity851ba692009-08-24 11:10:17 +03007725static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007726{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007727 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007728 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7729 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007730 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007731 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007732
7733 return 1;
7734}
7735
Mohammed Gamal80ced182009-09-01 12:48:18 +02007736static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007737{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007738 struct vcpu_vmx *vmx = to_vmx(vcpu);
7739 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007740 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007741 u32 cpu_exec_ctrl;
7742 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007743 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007744
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007745 /*
7746 * We should never reach the point where we are emulating L2
7747 * due to invalid guest state as that means we incorrectly
7748 * allowed a nested VMEntry with an invalid vmcs12.
7749 */
7750 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7751
Avi Kivity49e9d552010-09-19 14:34:08 +02007752 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7753 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007754
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007755 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007756 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007757 return handle_interrupt_window(&vmx->vcpu);
7758
Radim Krčmář72875d82017-04-26 22:32:19 +02007759 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007760 return 1;
7761
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007762 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007763
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007764 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007765 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007766 ret = 0;
7767 goto out;
7768 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007769
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007770 if (err != EMULATE_DONE)
7771 goto emulation_error;
7772
7773 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7774 vcpu->arch.exception.pending)
7775 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007776
Gleb Natapov8d76c492013-05-08 18:38:44 +03007777 if (vcpu->arch.halt_request) {
7778 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007779 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007780 goto out;
7781 }
7782
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007783 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007784 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007785 if (need_resched())
7786 schedule();
7787 }
7788
Mohammed Gamal80ced182009-09-01 12:48:18 +02007789out:
7790 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007791
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007792emulation_error:
7793 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7794 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7795 vcpu->run->internal.ndata = 0;
7796 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007797}
7798
7799static void grow_ple_window(struct kvm_vcpu *vcpu)
7800{
7801 struct vcpu_vmx *vmx = to_vmx(vcpu);
7802 int old = vmx->ple_window;
7803
Babu Mogerc8e88712018-03-16 16:37:24 -04007804 vmx->ple_window = __grow_ple_window(old, ple_window,
7805 ple_window_grow,
7806 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007807
7808 if (vmx->ple_window != old)
7809 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007810
7811 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007812}
7813
7814static void shrink_ple_window(struct kvm_vcpu *vcpu)
7815{
7816 struct vcpu_vmx *vmx = to_vmx(vcpu);
7817 int old = vmx->ple_window;
7818
Babu Mogerc8e88712018-03-16 16:37:24 -04007819 vmx->ple_window = __shrink_ple_window(old, ple_window,
7820 ple_window_shrink,
7821 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007822
7823 if (vmx->ple_window != old)
7824 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007825
7826 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007827}
7828
7829/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007830 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7831 */
7832static void wakeup_handler(void)
7833{
7834 struct kvm_vcpu *vcpu;
7835 int cpu = smp_processor_id();
7836
7837 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7838 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7839 blocked_vcpu_list) {
7840 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7841
7842 if (pi_test_on(pi_desc) == 1)
7843 kvm_vcpu_kick(vcpu);
7844 }
7845 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7846}
7847
Peng Haoe01bca22018-04-07 05:47:32 +08007848static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007849{
7850 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7851 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7852 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7853 0ull, VMX_EPT_EXECUTABLE_MASK,
7854 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007855 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007856
7857 ept_set_mmio_spte_mask();
7858 kvm_enable_tdp();
7859}
7860
Tiejun Chenf2c76482014-10-28 10:14:47 +08007861static __init int hardware_setup(void)
7862{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007863 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007864 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007865
7866 rdmsrl_safe(MSR_EFER, &host_efer);
7867
7868 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7869 kvm_define_shared_msr(i, vmx_msr_index[i]);
7870
Radim Krčmář23611332016-09-29 22:41:33 +02007871 for (i = 0; i < VMX_BITMAP_NR; i++) {
7872 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7873 if (!vmx_bitmap[i])
7874 goto out;
7875 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007876
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007877 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7878 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7879
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007880 if (setup_vmcs_config(&vmcs_config) < 0) {
7881 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007882 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007883 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007884
7885 if (boot_cpu_has(X86_FEATURE_NX))
7886 kvm_enable_efer_bits(EFER_NX);
7887
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007888 if (boot_cpu_has(X86_FEATURE_MPX)) {
7889 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7890 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7891 }
7892
Wanpeng Li08d839c2017-03-23 05:30:08 -07007893 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7894 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007895 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007896
Tiejun Chenf2c76482014-10-28 10:14:47 +08007897 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007898 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007899 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007900 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007901 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007902
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007903 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007904 enable_ept_ad_bits = 0;
7905
Wanpeng Li8ad81822017-10-09 15:51:53 -07007906 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007907 enable_unrestricted_guest = 0;
7908
Paolo Bonziniad15a292015-01-30 16:18:49 +01007909 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007910 flexpriority_enabled = 0;
7911
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007912 if (!cpu_has_virtual_nmis())
7913 enable_vnmi = 0;
7914
Paolo Bonziniad15a292015-01-30 16:18:49 +01007915 /*
7916 * set_apic_access_page_addr() is used to reload apic access
7917 * page upon invalidation. No need to do anything if not
7918 * using the APIC_ACCESS_ADDR VMCS field.
7919 */
7920 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007921 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007922
7923 if (!cpu_has_vmx_tpr_shadow())
7924 kvm_x86_ops->update_cr8_intercept = NULL;
7925
7926 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7927 kvm_disable_largepages();
7928
Tianyu Lan877ad952018-07-19 08:40:23 +00007929#if IS_ENABLED(CONFIG_HYPERV)
7930 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7931 && enable_ept)
7932 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7933#endif
7934
Wanpeng Li0f107682017-09-28 18:06:24 -07007935 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007936 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007937 ple_window = 0;
7938 ple_window_grow = 0;
7939 ple_window_max = 0;
7940 ple_window_shrink = 0;
7941 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007942
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007943 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007944 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007945 kvm_x86_ops->sync_pir_to_irr = NULL;
7946 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007947
Haozhong Zhang64903d62015-10-20 15:39:09 +08007948 if (cpu_has_vmx_tsc_scaling()) {
7949 kvm_has_tsc_control = true;
7950 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7951 kvm_tsc_scaling_ratio_frac_bits = 48;
7952 }
7953
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007954 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7955
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007956 if (enable_ept)
7957 vmx_enable_tdp();
7958 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007959 kvm_disable_tdp();
7960
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007961 if (!nested) {
7962 kvm_x86_ops->get_nested_state = NULL;
7963 kvm_x86_ops->set_nested_state = NULL;
7964 }
7965
Kai Huang843e4332015-01-28 10:54:28 +08007966 /*
7967 * Only enable PML when hardware supports PML feature, and both EPT
7968 * and EPT A/D bit features are enabled -- PML depends on them to work.
7969 */
7970 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7971 enable_pml = 0;
7972
7973 if (!enable_pml) {
7974 kvm_x86_ops->slot_enable_log_dirty = NULL;
7975 kvm_x86_ops->slot_disable_log_dirty = NULL;
7976 kvm_x86_ops->flush_log_dirty = NULL;
7977 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7978 }
7979
Sean Christophersond264ee02018-08-27 15:21:12 -07007980 if (!cpu_has_vmx_preemption_timer())
7981 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7982
Yunhong Jiang64672c92016-06-13 14:19:59 -07007983 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7984 u64 vmx_msr;
7985
7986 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7987 cpu_preemption_timer_multi =
7988 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7989 } else {
7990 kvm_x86_ops->set_hv_timer = NULL;
7991 kvm_x86_ops->cancel_hv_timer = NULL;
7992 }
7993
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007994 if (!cpu_has_vmx_shadow_vmcs())
7995 enable_shadow_vmcs = 0;
7996 if (enable_shadow_vmcs)
7997 init_vmcs_shadow_fields();
7998
Feng Wubf9f6ac2015-09-18 22:29:55 +08007999 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01008000 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008001
Ashok Rajc45dcc72016-06-22 14:59:56 +08008002 kvm_mce_cap_supported |= MCG_LMCE_P;
8003
Tiejun Chenf2c76482014-10-28 10:14:47 +08008004 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008005
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008006out:
Radim Krčmář23611332016-09-29 22:41:33 +02008007 for (i = 0; i < VMX_BITMAP_NR; i++)
8008 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008009
8010 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008011}
8012
8013static __exit void hardware_unsetup(void)
8014{
Radim Krčmář23611332016-09-29 22:41:33 +02008015 int i;
8016
8017 for (i = 0; i < VMX_BITMAP_NR; i++)
8018 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008019
Tiejun Chenf2c76482014-10-28 10:14:47 +08008020 free_kvm_area();
8021}
8022
Avi Kivity6aa8b732006-12-10 02:21:36 -08008023/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008024 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8025 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8026 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008027static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008028{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008029 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008030 grow_ple_window(vcpu);
8031
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008032 /*
8033 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8034 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8035 * never set PAUSE_EXITING and just set PLE if supported,
8036 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8037 */
8038 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008039 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008040}
8041
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008042static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008043{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008044 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008045}
8046
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008047static int handle_mwait(struct kvm_vcpu *vcpu)
8048{
8049 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8050 return handle_nop(vcpu);
8051}
8052
Jim Mattson45ec3682017-08-23 16:32:04 -07008053static int handle_invalid_op(struct kvm_vcpu *vcpu)
8054{
8055 kvm_queue_exception(vcpu, UD_VECTOR);
8056 return 1;
8057}
8058
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008059static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8060{
8061 return 1;
8062}
8063
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008064static int handle_monitor(struct kvm_vcpu *vcpu)
8065{
8066 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8067 return handle_nop(vcpu);
8068}
8069
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008070/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008071 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008072 * set the success or error code of an emulated VMX instruction (as specified
8073 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
8074 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008075 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008076static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008077{
8078 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8079 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8080 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008081 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008082}
8083
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008084static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008085{
8086 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8087 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8088 X86_EFLAGS_SF | X86_EFLAGS_OF))
8089 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008090 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008091}
8092
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008093static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
8094 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008095{
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008096 /*
8097 * failValid writes the error number to the current VMCS, which
8098 * can't be done if there isn't a current VMCS.
8099 */
8100 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8101 return nested_vmx_failInvalid(vcpu);
8102
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008103 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8104 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8105 X86_EFLAGS_SF | X86_EFLAGS_OF))
8106 | X86_EFLAGS_ZF);
8107 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8108 /*
8109 * We don't need to force a shadow sync because
8110 * VM_INSTRUCTION_ERROR is not shadowed
8111 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008112 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008113}
Abel Gordon145c28d2013-04-18 14:36:55 +03008114
Wincy Vanff651cb2014-12-11 08:52:58 +03008115static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8116{
8117 /* TODO: not to reset guest simply here. */
8118 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008119 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008120}
8121
Jan Kiszkaf4124502014-03-07 20:03:13 +01008122static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8123{
8124 struct vcpu_vmx *vmx =
8125 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8126
8127 vmx->nested.preemption_timer_expired = true;
8128 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8129 kvm_vcpu_kick(&vmx->vcpu);
8130
8131 return HRTIMER_NORESTART;
8132}
8133
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008134/*
Bandan Das19677e32014-05-06 02:19:15 -04008135 * Decode the memory-address operand of a vmx instruction, as recorded on an
8136 * exit caused by such an instruction (run by a guest hypervisor).
8137 * On success, returns 0. When the operand is invalid, returns 1 and throws
8138 * #UD or #GP.
8139 */
8140static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8141 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008142 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008143{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008144 gva_t off;
8145 bool exn;
8146 struct kvm_segment s;
8147
Bandan Das19677e32014-05-06 02:19:15 -04008148 /*
8149 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8150 * Execution", on an exit, vmx_instruction_info holds most of the
8151 * addressing components of the operand. Only the displacement part
8152 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8153 * For how an actual address is calculated from all these components,
8154 * refer to Vol. 1, "Operand Addressing".
8155 */
8156 int scaling = vmx_instruction_info & 3;
8157 int addr_size = (vmx_instruction_info >> 7) & 7;
8158 bool is_reg = vmx_instruction_info & (1u << 10);
8159 int seg_reg = (vmx_instruction_info >> 15) & 7;
8160 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8161 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8162 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8163 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8164
8165 if (is_reg) {
8166 kvm_queue_exception(vcpu, UD_VECTOR);
8167 return 1;
8168 }
8169
8170 /* Addr = segment_base + offset */
8171 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008172 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008173 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008174 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008175 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008176 off += kvm_register_read(vcpu, index_reg)<<scaling;
8177 vmx_get_segment(vcpu, &s, seg_reg);
8178 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008179
8180 if (addr_size == 1) /* 32 bit */
8181 *ret &= 0xffffffff;
8182
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008183 /* Checks for #GP/#SS exceptions. */
8184 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008185 if (is_long_mode(vcpu)) {
8186 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8187 * non-canonical form. This is the only check on the memory
8188 * destination for long mode!
8189 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008190 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008191 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008192 /* Protected mode: apply checks for segment validity in the
8193 * following order:
8194 * - segment type check (#GP(0) may be thrown)
8195 * - usability check (#GP(0)/#SS(0))
8196 * - limit check (#GP(0)/#SS(0))
8197 */
8198 if (wr)
8199 /* #GP(0) if the destination operand is located in a
8200 * read-only data segment or any code segment.
8201 */
8202 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8203 else
8204 /* #GP(0) if the source operand is located in an
8205 * execute-only code segment
8206 */
8207 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008208 if (exn) {
8209 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8210 return 1;
8211 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008212 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8213 */
8214 exn = (s.unusable != 0);
8215 /* Protected mode: #GP(0)/#SS(0) if the memory
8216 * operand is outside the segment limit.
8217 */
8218 exn = exn || (off + sizeof(u64) > s.limit);
8219 }
8220 if (exn) {
8221 kvm_queue_exception_e(vcpu,
8222 seg_reg == VCPU_SREG_SS ?
8223 SS_VECTOR : GP_VECTOR,
8224 0);
8225 return 1;
8226 }
8227
Bandan Das19677e32014-05-06 02:19:15 -04008228 return 0;
8229}
8230
Radim Krčmářcbf71272017-05-19 15:48:51 +02008231static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008232{
8233 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008234 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008235
8236 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008237 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008238 return 1;
8239
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008240 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008241 kvm_inject_page_fault(vcpu, &e);
8242 return 1;
8243 }
8244
Bandan Das3573e222014-05-06 02:19:16 -04008245 return 0;
8246}
8247
Liran Alonabfc52c2018-06-23 02:35:13 +03008248/*
8249 * Allocate a shadow VMCS and associate it with the currently loaded
8250 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8251 * VMCS is also VMCLEARed, so that it is ready for use.
8252 */
8253static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8254{
8255 struct vcpu_vmx *vmx = to_vmx(vcpu);
8256 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8257
8258 /*
8259 * We should allocate a shadow vmcs for vmcs01 only when L1
8260 * executes VMXON and free it when L1 executes VMXOFF.
8261 * As it is invalid to execute VMXON twice, we shouldn't reach
8262 * here when vmcs01 already have an allocated shadow vmcs.
8263 */
8264 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8265
8266 if (!loaded_vmcs->shadow_vmcs) {
8267 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8268 if (loaded_vmcs->shadow_vmcs)
8269 vmcs_clear(loaded_vmcs->shadow_vmcs);
8270 }
8271 return loaded_vmcs->shadow_vmcs;
8272}
8273
Jim Mattsone29acc52016-11-30 12:03:43 -08008274static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8275{
8276 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008277 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008278
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008279 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8280 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008281 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008282
8283 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8284 if (!vmx->nested.cached_vmcs12)
8285 goto out_cached_vmcs12;
8286
Liran Alon61ada742018-06-23 02:35:08 +03008287 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8288 if (!vmx->nested.cached_shadow_vmcs12)
8289 goto out_cached_shadow_vmcs12;
8290
Liran Alonabfc52c2018-06-23 02:35:13 +03008291 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8292 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008293
Jim Mattsone29acc52016-11-30 12:03:43 -08008294 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8295 HRTIMER_MODE_REL_PINNED);
8296 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8297
Roman Kagan63aff652018-07-19 21:59:07 +03008298 vmx->nested.vpid02 = allocate_vpid();
8299
Sean Christopherson9d6105b22018-09-26 09:23:51 -07008300 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008301 vmx->nested.vmxon = true;
8302 return 0;
8303
8304out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008305 kfree(vmx->nested.cached_shadow_vmcs12);
8306
8307out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008308 kfree(vmx->nested.cached_vmcs12);
8309
8310out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008311 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008312
Jim Mattsonde3a0022017-11-27 17:22:25 -06008313out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008314 return -ENOMEM;
8315}
8316
Bandan Das3573e222014-05-06 02:19:16 -04008317/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008318 * Emulate the VMXON instruction.
8319 * Currently, we just remember that VMX is active, and do not save or even
8320 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8321 * do not currently need to store anything in that guest-allocated memory
8322 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8323 * argument is different from the VMXON pointer (which the spec says they do).
8324 */
8325static int handle_vmon(struct kvm_vcpu *vcpu)
8326{
Jim Mattsone29acc52016-11-30 12:03:43 -08008327 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008328 gpa_t vmptr;
8329 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008330 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008331 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8332 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008333
Jim Mattson70f3aac2017-04-26 08:53:46 -07008334 /*
8335 * The Intel VMX Instruction Reference lists a bunch of bits that are
8336 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8337 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8338 * Otherwise, we should fail with #UD. But most faulting conditions
8339 * have already been checked by hardware, prior to the VM-exit for
8340 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8341 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008342 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008343 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008344 kvm_queue_exception(vcpu, UD_VECTOR);
8345 return 1;
8346 }
8347
Felix Wilhelm727ba742018-06-11 09:43:44 +02008348 /* CPL=0 must be checked manually. */
8349 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008350 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008351 return 1;
8352 }
8353
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008354 if (vmx->nested.vmxon)
8355 return nested_vmx_failValid(vcpu,
8356 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008357
Haozhong Zhang3b840802016-06-22 14:59:54 +08008358 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008359 != VMXON_NEEDED_FEATURES) {
8360 kvm_inject_gp(vcpu, 0);
8361 return 1;
8362 }
8363
Radim Krčmářcbf71272017-05-19 15:48:51 +02008364 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008365 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008366
8367 /*
8368 * SDM 3: 24.11.5
8369 * The first 4 bytes of VMXON region contain the supported
8370 * VMCS revision identifier
8371 *
8372 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8373 * which replaces physical address width with 32
8374 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008375 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8376 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008377
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008378 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008379 if (is_error_page(page))
8380 return nested_vmx_failInvalid(vcpu);
8381
Radim Krčmářcbf71272017-05-19 15:48:51 +02008382 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8383 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008384 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008385 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008386 }
8387 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008388 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008389
8390 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008391 ret = enter_vmx_operation(vcpu);
8392 if (ret)
8393 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008394
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008395 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008396}
8397
8398/*
8399 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8400 * for running VMX instructions (except VMXON, whose prerequisites are
8401 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008402 * Note that many of these exceptions have priority over VM exits, so they
8403 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008404 */
8405static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8406{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008407 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008408 kvm_queue_exception(vcpu, UD_VECTOR);
8409 return 0;
8410 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008411
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008412 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008413 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008414 return 0;
8415 }
8416
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008417 return 1;
8418}
8419
David Matlack8ca44e82017-08-01 14:00:39 -07008420static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8421{
8422 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8423 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8424}
8425
Abel Gordone7953d72013-04-18 14:37:55 +03008426static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8427{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008428 if (vmx->nested.current_vmptr == -1ull)
8429 return;
8430
Abel Gordon012f83c2013-04-18 14:39:25 +03008431 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008432 /* copy to memory all shadowed fields in case
8433 they were modified */
8434 copy_shadow_to_vmcs12(vmx);
8435 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008436 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008437 }
Wincy Van705699a2015-02-03 23:58:17 +08008438 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008439
8440 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008441 kvm_vcpu_write_guest_page(&vmx->vcpu,
8442 vmx->nested.current_vmptr >> PAGE_SHIFT,
8443 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008444
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008445 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008446}
8447
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008448/*
8449 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8450 * just stops using VMX.
8451 */
8452static void free_nested(struct vcpu_vmx *vmx)
8453{
Wanpeng Lib7455822017-11-22 14:04:00 -08008454 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008455 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008456
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008457 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008458 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008459 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008460 vmx->nested.posted_intr_nv = -1;
8461 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008462 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008463 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008464 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8465 free_vmcs(vmx->vmcs01.shadow_vmcs);
8466 vmx->vmcs01.shadow_vmcs = NULL;
8467 }
David Matlack4f2777b2016-07-13 17:16:37 -07008468 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008469 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008470 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008471 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008472 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008473 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008474 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008475 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008476 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008477 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008478 }
Wincy Van705699a2015-02-03 23:58:17 +08008479 if (vmx->nested.pi_desc_page) {
8480 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008481 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008482 vmx->nested.pi_desc_page = NULL;
8483 vmx->nested.pi_desc = NULL;
8484 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008485
Jim Mattsonde3a0022017-11-27 17:22:25 -06008486 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008487}
8488
8489/* Emulate the VMXOFF instruction */
8490static int handle_vmoff(struct kvm_vcpu *vcpu)
8491{
8492 if (!nested_vmx_check_permission(vcpu))
8493 return 1;
8494 free_nested(to_vmx(vcpu));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008495 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008496}
8497
Nadav Har'El27d6c862011-05-25 23:06:59 +03008498/* Emulate the VMCLEAR instruction */
8499static int handle_vmclear(struct kvm_vcpu *vcpu)
8500{
8501 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008502 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008503 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008504
8505 if (!nested_vmx_check_permission(vcpu))
8506 return 1;
8507
Radim Krčmářcbf71272017-05-19 15:48:51 +02008508 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008509 return 1;
8510
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008511 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8512 return nested_vmx_failValid(vcpu,
8513 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008514
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008515 if (vmptr == vmx->nested.vmxon_ptr)
8516 return nested_vmx_failValid(vcpu,
8517 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008518
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008519 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008520 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008521
Jim Mattson587d7e722017-03-02 12:41:48 -08008522 kvm_vcpu_write_guest(vcpu,
8523 vmptr + offsetof(struct vmcs12, launch_state),
8524 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008525
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008526 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008527}
8528
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008529static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8530
8531/* Emulate the VMLAUNCH instruction */
8532static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8533{
8534 return nested_vmx_run(vcpu, true);
8535}
8536
8537/* Emulate the VMRESUME instruction */
8538static int handle_vmresume(struct kvm_vcpu *vcpu)
8539{
8540
8541 return nested_vmx_run(vcpu, false);
8542}
8543
Nadav Har'El49f705c2011-05-25 23:08:30 +03008544/*
8545 * Read a vmcs12 field. Since these can have varying lengths and we return
8546 * one type, we chose the biggest type (u64) and zero-extend the return value
8547 * to that size. Note that the caller, handle_vmread, might need to use only
8548 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8549 * 64-bit fields are to be returned).
8550 */
Liran Alone2536742018-06-23 02:35:02 +03008551static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008552 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553{
8554 short offset = vmcs_field_to_offset(field);
8555 char *p;
8556
8557 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008558 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559
Liran Alone2536742018-06-23 02:35:02 +03008560 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008561
Jim Mattsond37f4262017-12-22 12:12:16 -08008562 switch (vmcs_field_width(field)) {
8563 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008565 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008566 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008569 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008570 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008571 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008572 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008573 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008574 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008575 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008576 WARN_ON(1);
8577 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008578 }
8579}
8580
Abel Gordon20b97fe2013-04-18 14:36:25 +03008581
Liran Alone2536742018-06-23 02:35:02 +03008582static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008583 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008584 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008585 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008586 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008587 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008588
Jim Mattsond37f4262017-12-22 12:12:16 -08008589 switch (vmcs_field_width(field)) {
8590 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008591 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008592 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008593 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008595 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008596 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008597 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008598 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008599 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008600 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008601 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008602 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008603 WARN_ON(1);
8604 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008605 }
8606
8607}
8608
Jim Mattsonf4160e42018-05-29 09:11:33 -07008609/*
8610 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8611 * they have been modified by the L1 guest. Note that the "read-only"
8612 * VM-exit information fields are actually writable if the vCPU is
8613 * configured to support "VMWRITE to any supported field in the VMCS."
8614 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008615static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8616{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008617 const u16 *fields[] = {
8618 shadow_read_write_fields,
8619 shadow_read_only_fields
8620 };
8621 const int max_fields[] = {
8622 max_shadow_read_write_fields,
8623 max_shadow_read_only_fields
8624 };
8625 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008626 unsigned long field;
8627 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008628 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008629
Jan Kiszka282da872014-10-08 18:05:39 +02008630 preempt_disable();
8631
Abel Gordon16f5b902013-04-18 14:38:25 +03008632 vmcs_load(shadow_vmcs);
8633
Jim Mattsonf4160e42018-05-29 09:11:33 -07008634 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8635 for (i = 0; i < max_fields[q]; i++) {
8636 field = fields[q][i];
8637 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008638 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008639 }
8640 /*
8641 * Skip the VM-exit information fields if they are read-only.
8642 */
8643 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8644 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008645 }
8646
8647 vmcs_clear(shadow_vmcs);
8648 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008649
8650 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008651}
8652
Abel Gordonc3114422013-04-18 14:38:55 +03008653static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8654{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008655 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008656 shadow_read_write_fields,
8657 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008658 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008659 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008660 max_shadow_read_write_fields,
8661 max_shadow_read_only_fields
8662 };
8663 int i, q;
8664 unsigned long field;
8665 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008666 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008667
8668 vmcs_load(shadow_vmcs);
8669
Mathias Krausec2bae892013-06-26 20:36:21 +02008670 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008671 for (i = 0; i < max_fields[q]; i++) {
8672 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008673 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008674 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008675 }
8676 }
8677
8678 vmcs_clear(shadow_vmcs);
8679 vmcs_load(vmx->loaded_vmcs->vmcs);
8680}
8681
Nadav Har'El49f705c2011-05-25 23:08:30 +03008682static int handle_vmread(struct kvm_vcpu *vcpu)
8683{
8684 unsigned long field;
8685 u64 field_value;
8686 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8687 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8688 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008689 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008690
Kyle Hueyeb277562016-11-29 12:40:39 -08008691 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008692 return 1;
8693
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008694 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8695 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008696
Liran Alon6d894f42018-06-23 02:35:09 +03008697 if (!is_guest_mode(vcpu))
8698 vmcs12 = get_vmcs12(vcpu);
8699 else {
8700 /*
8701 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8702 * to shadowed-field sets the ALU flags for VMfailInvalid.
8703 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008704 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8705 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008706 vmcs12 = get_shadow_vmcs12(vcpu);
8707 }
8708
Nadav Har'El49f705c2011-05-25 23:08:30 +03008709 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008710 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008711 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008712 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
8713 return nested_vmx_failValid(vcpu,
8714 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8715
Nadav Har'El49f705c2011-05-25 23:08:30 +03008716 /*
8717 * Now copy part of this value to register or memory, as requested.
8718 * Note that the number of bits actually copied is 32 or 64 depending
8719 * on the guest's mode (32 or 64 bit), not on the given field's length.
8720 */
8721 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008722 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008723 field_value);
8724 } else {
8725 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008726 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008727 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008728 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008729 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8730 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008731 }
8732
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008733 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008734}
8735
8736
8737static int handle_vmwrite(struct kvm_vcpu *vcpu)
8738{
8739 unsigned long field;
8740 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008741 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008742 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8743 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008744
Nadav Har'El49f705c2011-05-25 23:08:30 +03008745 /* The value to write might be 32 or 64 bits, depending on L1's long
8746 * mode, and eventually we need to write that into a field of several
8747 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008748 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008749 * bits into the vmcs12 field.
8750 */
8751 u64 field_value = 0;
8752 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008753 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008754
Kyle Hueyeb277562016-11-29 12:40:39 -08008755 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008756 return 1;
8757
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008758 if (vmx->nested.current_vmptr == -1ull)
8759 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008760
Nadav Har'El49f705c2011-05-25 23:08:30 +03008761 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008762 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008763 (((vmx_instruction_info) >> 3) & 0xf));
8764 else {
8765 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008766 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008767 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008768 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8769 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008770 kvm_inject_page_fault(vcpu, &e);
8771 return 1;
8772 }
8773 }
8774
8775
Nadav Amit27e6fb52014-06-18 17:19:26 +03008776 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008777 /*
8778 * If the vCPU supports "VMWRITE to any supported field in the
8779 * VMCS," then the "read-only" fields are actually read/write.
8780 */
8781 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008782 !nested_cpu_has_vmwrite_any_field(vcpu))
8783 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008784 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008785
Liran Alon6d894f42018-06-23 02:35:09 +03008786 if (!is_guest_mode(vcpu))
8787 vmcs12 = get_vmcs12(vcpu);
8788 else {
8789 /*
8790 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8791 * to shadowed-field sets the ALU flags for VMfailInvalid.
8792 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008793 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8794 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008795 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008796 }
8797
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008798 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
8799 return nested_vmx_failValid(vcpu,
8800 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008801
Liran Alon6d894f42018-06-23 02:35:09 +03008802 /*
8803 * Do not track vmcs12 dirty-state if in guest-mode
8804 * as we actually dirty shadow vmcs12 instead of vmcs12.
8805 */
8806 if (!is_guest_mode(vcpu)) {
8807 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008808#define SHADOW_FIELD_RW(x) case x:
8809#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008810 /*
8811 * The fields that can be updated by L1 without a vmexit are
8812 * always updated in the vmcs02, the others go down the slow
8813 * path of prepare_vmcs02.
8814 */
8815 break;
8816 default:
8817 vmx->nested.dirty_vmcs12 = true;
8818 break;
8819 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008820 }
8821
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008822 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008823}
8824
Jim Mattsona8bc2842016-11-30 12:03:44 -08008825static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8826{
8827 vmx->nested.current_vmptr = vmptr;
8828 if (enable_shadow_vmcs) {
8829 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8830 SECONDARY_EXEC_SHADOW_VMCS);
8831 vmcs_write64(VMCS_LINK_POINTER,
8832 __pa(vmx->vmcs01.shadow_vmcs));
8833 vmx->nested.sync_shadow_vmcs = true;
8834 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008835 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008836}
8837
Nadav Har'El63846662011-05-25 23:07:29 +03008838/* Emulate the VMPTRLD instruction */
8839static int handle_vmptrld(struct kvm_vcpu *vcpu)
8840{
8841 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008842 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008843
8844 if (!nested_vmx_check_permission(vcpu))
8845 return 1;
8846
Radim Krčmářcbf71272017-05-19 15:48:51 +02008847 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008848 return 1;
8849
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008850 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8851 return nested_vmx_failValid(vcpu,
8852 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008853
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008854 if (vmptr == vmx->nested.vmxon_ptr)
8855 return nested_vmx_failValid(vcpu,
8856 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008857
Nadav Har'El63846662011-05-25 23:07:29 +03008858 if (vmx->nested.current_vmptr != vmptr) {
8859 struct vmcs12 *new_vmcs12;
8860 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008861 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008862 if (is_error_page(page))
8863 return nested_vmx_failInvalid(vcpu);
8864
Nadav Har'El63846662011-05-25 23:07:29 +03008865 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008866 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008867 (new_vmcs12->hdr.shadow_vmcs &&
8868 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008869 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008870 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008871 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03008872 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03008873 }
Nadav Har'El63846662011-05-25 23:07:29 +03008874
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008875 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008876 /*
8877 * Load VMCS12 from guest memory since it is not already
8878 * cached.
8879 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008880 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8881 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008882 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008883
Jim Mattsona8bc2842016-11-30 12:03:44 -08008884 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008885 }
8886
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008887 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008888}
8889
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008890/* Emulate the VMPTRST instruction */
8891static int handle_vmptrst(struct kvm_vcpu *vcpu)
8892{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008893 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8894 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8895 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008896 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008897 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008898
8899 if (!nested_vmx_check_permission(vcpu))
8900 return 1;
8901
Sean Christopherson0a06d422018-07-19 10:31:00 -07008902 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008903 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008904 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008905 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8906 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008907 kvm_inject_page_fault(vcpu, &e);
8908 return 1;
8909 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008910 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008911}
8912
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008913/* Emulate the INVEPT instruction */
8914static int handle_invept(struct kvm_vcpu *vcpu)
8915{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008916 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008917 u32 vmx_instruction_info, types;
8918 unsigned long type;
8919 gva_t gva;
8920 struct x86_exception e;
8921 struct {
8922 u64 eptp, gpa;
8923 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008924
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008925 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008926 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008927 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008928 kvm_queue_exception(vcpu, UD_VECTOR);
8929 return 1;
8930 }
8931
8932 if (!nested_vmx_check_permission(vcpu))
8933 return 1;
8934
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008935 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008936 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008937
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008938 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008939
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008940 if (type >= 32 || !(types & (1 << type)))
8941 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008942 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008943
8944 /* According to the Intel VMX instruction reference, the memory
8945 * operand is read even if it isn't needed (e.g., for type==global)
8946 */
8947 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008948 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008949 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008950 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008951 kvm_inject_page_fault(vcpu, &e);
8952 return 1;
8953 }
8954
8955 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008956 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008957 /*
8958 * TODO: track mappings and invalidate
8959 * single context requests appropriately
8960 */
8961 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008962 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008963 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008964 break;
8965 default:
8966 BUG_ON(1);
8967 break;
8968 }
8969
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008970 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008971}
8972
Liran Alon3d5bdae2018-10-08 23:42:18 +03008973static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
8974{
8975 struct vcpu_vmx *vmx = to_vmx(vcpu);
8976
8977 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
8978}
8979
Petr Matouseka642fc32014-09-23 20:22:30 +02008980static int handle_invvpid(struct kvm_vcpu *vcpu)
8981{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008982 struct vcpu_vmx *vmx = to_vmx(vcpu);
8983 u32 vmx_instruction_info;
8984 unsigned long type, types;
8985 gva_t gva;
8986 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008987 struct {
8988 u64 vpid;
8989 u64 gla;
8990 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03008991 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008992
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008993 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008994 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008995 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008996 kvm_queue_exception(vcpu, UD_VECTOR);
8997 return 1;
8998 }
8999
9000 if (!nested_vmx_check_permission(vcpu))
9001 return 1;
9002
9003 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9004 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9005
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009006 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009007 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009008
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009009 if (type >= 32 || !(types & (1 << type)))
9010 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009011 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009012
9013 /* according to the intel vmx instruction reference, the memory
9014 * operand is read even if it isn't needed (e.g., for type==global)
9015 */
9016 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9017 vmx_instruction_info, false, &gva))
9018 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009019 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009020 kvm_inject_page_fault(vcpu, &e);
9021 return 1;
9022 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009023 if (operand.vpid >> 16)
9024 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009025 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009026
Liran Alon3d5bdae2018-10-08 23:42:18 +03009027 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009028 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009029 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009030 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009031 is_noncanonical_address(operand.gla, vcpu))
9032 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009033 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03009034 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009035 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009036 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009037 } else
Liran Alon327c0722018-10-08 23:42:19 +03009038 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009039 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009040 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009041 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009042 if (!operand.vpid)
9043 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009044 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03009045 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009046 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009047 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009048 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009049 break;
9050 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009051 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009052 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009053 }
9054
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009055 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009056}
9057
Junaid Shahideb4b2482018-06-27 14:59:14 -07009058static int handle_invpcid(struct kvm_vcpu *vcpu)
9059{
9060 u32 vmx_instruction_info;
9061 unsigned long type;
9062 bool pcid_enabled;
9063 gva_t gva;
9064 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009065 unsigned i;
9066 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009067 struct {
9068 u64 pcid;
9069 u64 gla;
9070 } operand;
9071
9072 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9073 kvm_queue_exception(vcpu, UD_VECTOR);
9074 return 1;
9075 }
9076
9077 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9078 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9079
9080 if (type > 3) {
9081 kvm_inject_gp(vcpu, 0);
9082 return 1;
9083 }
9084
9085 /* According to the Intel instruction reference, the memory operand
9086 * is read even if it isn't needed (e.g., for type==all)
9087 */
9088 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9089 vmx_instruction_info, false, &gva))
9090 return 1;
9091
9092 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9093 kvm_inject_page_fault(vcpu, &e);
9094 return 1;
9095 }
9096
9097 if (operand.pcid >> 12 != 0) {
9098 kvm_inject_gp(vcpu, 0);
9099 return 1;
9100 }
9101
9102 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9103
9104 switch (type) {
9105 case INVPCID_TYPE_INDIV_ADDR:
9106 if ((!pcid_enabled && (operand.pcid != 0)) ||
9107 is_noncanonical_address(operand.gla, vcpu)) {
9108 kvm_inject_gp(vcpu, 0);
9109 return 1;
9110 }
9111 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9112 return kvm_skip_emulated_instruction(vcpu);
9113
9114 case INVPCID_TYPE_SINGLE_CTXT:
9115 if (!pcid_enabled && (operand.pcid != 0)) {
9116 kvm_inject_gp(vcpu, 0);
9117 return 1;
9118 }
9119
9120 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9121 kvm_mmu_sync_roots(vcpu);
9122 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9123 }
9124
Junaid Shahidb94742c2018-06-27 14:59:20 -07009125 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009126 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07009127 == operand.pcid)
9128 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009129
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02009130 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009131 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009132 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009133 * given PCID, then nothing needs to be done here because a
9134 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009135 */
9136
9137 return kvm_skip_emulated_instruction(vcpu);
9138
9139 case INVPCID_TYPE_ALL_NON_GLOBAL:
9140 /*
9141 * Currently, KVM doesn't mark global entries in the shadow
9142 * page tables, so a non-global flush just degenerates to a
9143 * global flush. If needed, we could optimize this later by
9144 * keeping track of global entries in shadow page tables.
9145 */
9146
9147 /* fall-through */
9148 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9149 kvm_mmu_unload(vcpu);
9150 return kvm_skip_emulated_instruction(vcpu);
9151
9152 default:
9153 BUG(); /* We have already checked above that type <= 3 */
9154 }
9155}
9156
Kai Huang843e4332015-01-28 10:54:28 +08009157static int handle_pml_full(struct kvm_vcpu *vcpu)
9158{
9159 unsigned long exit_qualification;
9160
9161 trace_kvm_pml_full(vcpu->vcpu_id);
9162
9163 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9164
9165 /*
9166 * PML buffer FULL happened while executing iret from NMI,
9167 * "blocked by NMI" bit has to be set before next VM entry.
9168 */
9169 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009170 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009171 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9172 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9173 GUEST_INTR_STATE_NMI);
9174
9175 /*
9176 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9177 * here.., and there's no userspace involvement needed for PML.
9178 */
9179 return 1;
9180}
9181
Yunhong Jiang64672c92016-06-13 14:19:59 -07009182static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9183{
Sean Christophersond264ee02018-08-27 15:21:12 -07009184 if (!to_vmx(vcpu)->req_immediate_exit)
9185 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009186 return 1;
9187}
9188
Bandan Das41ab9372017-08-03 15:54:43 -04009189static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9190{
9191 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009192 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9193
9194 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009195 switch (address & VMX_EPTP_MT_MASK) {
9196 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009197 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009198 return false;
9199 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009200 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009201 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009202 return false;
9203 break;
9204 default:
9205 return false;
9206 }
9207
David Hildenbrandbb97a012017-08-10 23:15:28 +02009208 /* only 4 levels page-walk length are valid */
9209 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009210 return false;
9211
9212 /* Reserved bits should not be set */
9213 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9214 return false;
9215
9216 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009217 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009218 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009219 return false;
9220 }
9221
9222 return true;
9223}
9224
9225static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9226 struct vmcs12 *vmcs12)
9227{
9228 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9229 u64 address;
9230 bool accessed_dirty;
9231 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9232
9233 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9234 !nested_cpu_has_ept(vmcs12))
9235 return 1;
9236
9237 if (index >= VMFUNC_EPTP_ENTRIES)
9238 return 1;
9239
9240
9241 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9242 &address, index * 8, 8))
9243 return 1;
9244
David Hildenbrandbb97a012017-08-10 23:15:28 +02009245 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009246
9247 /*
9248 * If the (L2) guest does a vmfunc to the currently
9249 * active ept pointer, we don't have to do anything else
9250 */
9251 if (vmcs12->ept_pointer != address) {
9252 if (!valid_ept_address(vcpu, address))
9253 return 1;
9254
9255 kvm_mmu_unload(vcpu);
9256 mmu->ept_ad = accessed_dirty;
9257 mmu->base_role.ad_disabled = !accessed_dirty;
9258 vmcs12->ept_pointer = address;
9259 /*
9260 * TODO: Check what's the correct approach in case
9261 * mmu reload fails. Currently, we just let the next
9262 * reload potentially fail
9263 */
9264 kvm_mmu_reload(vcpu);
9265 }
9266
9267 return 0;
9268}
9269
Bandan Das2a499e42017-08-03 15:54:41 -04009270static int handle_vmfunc(struct kvm_vcpu *vcpu)
9271{
Bandan Das27c42a12017-08-03 15:54:42 -04009272 struct vcpu_vmx *vmx = to_vmx(vcpu);
9273 struct vmcs12 *vmcs12;
9274 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9275
9276 /*
9277 * VMFUNC is only supported for nested guests, but we always enable the
9278 * secondary control for simplicity; for non-nested mode, fake that we
9279 * didn't by injecting #UD.
9280 */
9281 if (!is_guest_mode(vcpu)) {
9282 kvm_queue_exception(vcpu, UD_VECTOR);
9283 return 1;
9284 }
9285
9286 vmcs12 = get_vmcs12(vcpu);
9287 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9288 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009289
9290 switch (function) {
9291 case 0:
9292 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9293 goto fail;
9294 break;
9295 default:
9296 goto fail;
9297 }
9298 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009299
9300fail:
9301 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9302 vmcs_read32(VM_EXIT_INTR_INFO),
9303 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009304 return 1;
9305}
9306
Sean Christopherson0b665d32018-08-14 09:33:34 -07009307static int handle_encls(struct kvm_vcpu *vcpu)
9308{
9309 /*
9310 * SGX virtualization is not yet supported. There is no software
9311 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9312 * to prevent the guest from executing ENCLS.
9313 */
9314 kvm_queue_exception(vcpu, UD_VECTOR);
9315 return 1;
9316}
9317
Nadav Har'El0140cae2011-05-25 23:06:28 +03009318/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009319 * The exit handlers return 1 if the exit was handled fully and guest execution
9320 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9321 * to be done to userspace and return 0.
9322 */
Mathias Krause772e0312012-08-30 01:30:19 +02009323static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009324 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9325 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009326 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009327 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009328 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009329 [EXIT_REASON_CR_ACCESS] = handle_cr,
9330 [EXIT_REASON_DR_ACCESS] = handle_dr,
9331 [EXIT_REASON_CPUID] = handle_cpuid,
9332 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9333 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9334 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9335 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009336 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009337 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009338 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009339 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009340 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009341 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009342 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009343 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009344 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009345 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009346 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009347 [EXIT_REASON_VMOFF] = handle_vmoff,
9348 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009349 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9350 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009351 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009352 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009353 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009354 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009355 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009356 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009357 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9358 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009359 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9360 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009361 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009362 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009363 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009364 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009365 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009366 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009367 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009368 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009369 [EXIT_REASON_XSAVES] = handle_xsaves,
9370 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009371 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009372 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009373 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009374 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009375 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009376};
9377
9378static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009379 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009380
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009381static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9382 struct vmcs12 *vmcs12)
9383{
9384 unsigned long exit_qualification;
9385 gpa_t bitmap, last_bitmap;
9386 unsigned int port;
9387 int size;
9388 u8 b;
9389
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009390 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009391 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009392
9393 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9394
9395 port = exit_qualification >> 16;
9396 size = (exit_qualification & 7) + 1;
9397
9398 last_bitmap = (gpa_t)-1;
9399 b = -1;
9400
9401 while (size > 0) {
9402 if (port < 0x8000)
9403 bitmap = vmcs12->io_bitmap_a;
9404 else if (port < 0x10000)
9405 bitmap = vmcs12->io_bitmap_b;
9406 else
Joe Perches1d804d02015-03-30 16:46:09 -07009407 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009408 bitmap += (port & 0x7fff) / 8;
9409
9410 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009411 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009412 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009413 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009414 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009415
9416 port++;
9417 size--;
9418 last_bitmap = bitmap;
9419 }
9420
Joe Perches1d804d02015-03-30 16:46:09 -07009421 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009422}
9423
Nadav Har'El644d7112011-05-25 23:12:35 +03009424/*
9425 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9426 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9427 * disinterest in the current event (read or write a specific MSR) by using an
9428 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9429 */
9430static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9431 struct vmcs12 *vmcs12, u32 exit_reason)
9432{
9433 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9434 gpa_t bitmap;
9435
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009436 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009437 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009438
9439 /*
9440 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9441 * for the four combinations of read/write and low/high MSR numbers.
9442 * First we need to figure out which of the four to use:
9443 */
9444 bitmap = vmcs12->msr_bitmap;
9445 if (exit_reason == EXIT_REASON_MSR_WRITE)
9446 bitmap += 2048;
9447 if (msr_index >= 0xc0000000) {
9448 msr_index -= 0xc0000000;
9449 bitmap += 1024;
9450 }
9451
9452 /* Then read the msr_index'th bit from this bitmap: */
9453 if (msr_index < 1024*8) {
9454 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009455 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009456 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009457 return 1 & (b >> (msr_index & 7));
9458 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009459 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009460}
9461
9462/*
9463 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9464 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9465 * intercept (via guest_host_mask etc.) the current event.
9466 */
9467static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9468 struct vmcs12 *vmcs12)
9469{
9470 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9471 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009472 int reg;
9473 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009474
9475 switch ((exit_qualification >> 4) & 3) {
9476 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009477 reg = (exit_qualification >> 8) & 15;
9478 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009479 switch (cr) {
9480 case 0:
9481 if (vmcs12->cr0_guest_host_mask &
9482 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009483 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009484 break;
9485 case 3:
9486 if ((vmcs12->cr3_target_count >= 1 &&
9487 vmcs12->cr3_target_value0 == val) ||
9488 (vmcs12->cr3_target_count >= 2 &&
9489 vmcs12->cr3_target_value1 == val) ||
9490 (vmcs12->cr3_target_count >= 3 &&
9491 vmcs12->cr3_target_value2 == val) ||
9492 (vmcs12->cr3_target_count >= 4 &&
9493 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009494 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009495 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009496 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009497 break;
9498 case 4:
9499 if (vmcs12->cr4_guest_host_mask &
9500 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009501 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009502 break;
9503 case 8:
9504 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009505 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009506 break;
9507 }
9508 break;
9509 case 2: /* clts */
9510 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9511 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009512 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009513 break;
9514 case 1: /* mov from cr */
9515 switch (cr) {
9516 case 3:
9517 if (vmcs12->cpu_based_vm_exec_control &
9518 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009519 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009520 break;
9521 case 8:
9522 if (vmcs12->cpu_based_vm_exec_control &
9523 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009524 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009525 break;
9526 }
9527 break;
9528 case 3: /* lmsw */
9529 /*
9530 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9531 * cr0. Other attempted changes are ignored, with no exit.
9532 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009533 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009534 if (vmcs12->cr0_guest_host_mask & 0xe &
9535 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009536 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009537 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9538 !(vmcs12->cr0_read_shadow & 0x1) &&
9539 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009540 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009541 break;
9542 }
Joe Perches1d804d02015-03-30 16:46:09 -07009543 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009544}
9545
Liran Alona7cde482018-06-23 02:35:10 +03009546static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9547 struct vmcs12 *vmcs12, gpa_t bitmap)
9548{
9549 u32 vmx_instruction_info;
9550 unsigned long field;
9551 u8 b;
9552
9553 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9554 return true;
9555
9556 /* Decode instruction info and find the field to access */
9557 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9558 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9559
9560 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9561 if (field >> 15)
9562 return true;
9563
9564 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9565 return true;
9566
9567 return 1 & (b >> (field & 7));
9568}
9569
Nadav Har'El644d7112011-05-25 23:12:35 +03009570/*
9571 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9572 * should handle it ourselves in L0 (and then continue L2). Only call this
9573 * when in is_guest_mode (L2).
9574 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009575static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009576{
Nadav Har'El644d7112011-05-25 23:12:35 +03009577 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9578 struct vcpu_vmx *vmx = to_vmx(vcpu);
9579 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9580
Jim Mattson4f350c62017-09-14 16:31:44 -07009581 if (vmx->nested.nested_run_pending)
9582 return false;
9583
9584 if (unlikely(vmx->fail)) {
9585 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9586 vmcs_read32(VM_INSTRUCTION_ERROR));
9587 return true;
9588 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009589
David Matlackc9f04402017-08-01 14:00:40 -07009590 /*
9591 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009592 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9593 * Page). The CPU may write to these pages via their host
9594 * physical address while L2 is running, bypassing any
9595 * address-translation-based dirty tracking (e.g. EPT write
9596 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009597 *
9598 * Mark them dirty on every exit from L2 to prevent them from
9599 * getting out of sync with dirty tracking.
9600 */
9601 nested_mark_vmcs12_pages_dirty(vcpu);
9602
Jim Mattson4f350c62017-09-14 16:31:44 -07009603 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9604 vmcs_readl(EXIT_QUALIFICATION),
9605 vmx->idt_vectoring_info,
9606 intr_info,
9607 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9608 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009609
9610 switch (exit_reason) {
9611 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009612 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009613 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009614 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009615 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009616 else if (is_debug(intr_info) &&
9617 vcpu->guest_debug &
9618 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9619 return false;
9620 else if (is_breakpoint(intr_info) &&
9621 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9622 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009623 return vmcs12->exception_bitmap &
9624 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9625 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009626 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009627 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009628 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009629 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009630 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009631 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009632 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009633 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009634 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009635 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009636 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009637 case EXIT_REASON_HLT:
9638 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9639 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009640 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009641 case EXIT_REASON_INVLPG:
9642 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9643 case EXIT_REASON_RDPMC:
9644 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009645 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009646 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009647 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009648 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009649 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009650 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009651 case EXIT_REASON_VMREAD:
9652 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9653 vmcs12->vmread_bitmap);
9654 case EXIT_REASON_VMWRITE:
9655 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9656 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009657 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9658 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009659 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009660 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009661 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009662 /*
9663 * VMX instructions trap unconditionally. This allows L1 to
9664 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9665 */
Joe Perches1d804d02015-03-30 16:46:09 -07009666 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009667 case EXIT_REASON_CR_ACCESS:
9668 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9669 case EXIT_REASON_DR_ACCESS:
9670 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9671 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009672 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009673 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9674 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009675 case EXIT_REASON_MSR_READ:
9676 case EXIT_REASON_MSR_WRITE:
9677 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9678 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009679 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009680 case EXIT_REASON_MWAIT_INSTRUCTION:
9681 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009682 case EXIT_REASON_MONITOR_TRAP_FLAG:
9683 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009684 case EXIT_REASON_MONITOR_INSTRUCTION:
9685 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9686 case EXIT_REASON_PAUSE_INSTRUCTION:
9687 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9688 nested_cpu_has2(vmcs12,
9689 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9690 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009691 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009692 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009693 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009694 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009695 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009696 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009697 /*
9698 * The controls for "virtualize APIC accesses," "APIC-
9699 * register virtualization," and "virtual-interrupt
9700 * delivery" only come from vmcs12.
9701 */
Joe Perches1d804d02015-03-30 16:46:09 -07009702 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009703 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009704 /*
9705 * L0 always deals with the EPT violation. If nested EPT is
9706 * used, and the nested mmu code discovers that the address is
9707 * missing in the guest EPT table (EPT12), the EPT violation
9708 * will be injected with nested_ept_inject_page_fault()
9709 */
Joe Perches1d804d02015-03-30 16:46:09 -07009710 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009711 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009712 /*
9713 * L2 never uses directly L1's EPT, but rather L0's own EPT
9714 * table (shadow on EPT) or a merged EPT table that L0 built
9715 * (EPT on EPT). So any problems with the structure of the
9716 * table is L0's fault.
9717 */
Joe Perches1d804d02015-03-30 16:46:09 -07009718 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009719 case EXIT_REASON_INVPCID:
9720 return
9721 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9722 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009723 case EXIT_REASON_WBINVD:
9724 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9725 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009726 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009727 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9728 /*
9729 * This should never happen, since it is not possible to
9730 * set XSS to a non-zero value---neither in L1 nor in L2.
9731 * If if it were, XSS would have to be checked against
9732 * the XSS exit bitmap in vmcs12.
9733 */
9734 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009735 case EXIT_REASON_PREEMPTION_TIMER:
9736 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009737 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009738 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009739 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009740 case EXIT_REASON_VMFUNC:
9741 /* VM functions are emulated through L2->L0 vmexits. */
9742 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009743 case EXIT_REASON_ENCLS:
9744 /* SGX is never exposed to L1 */
9745 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009746 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009747 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009748 }
9749}
9750
Paolo Bonzini7313c692017-07-27 10:31:25 +02009751static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9752{
9753 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9754
9755 /*
9756 * At this point, the exit interruption info in exit_intr_info
9757 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9758 * we need to query the in-kernel LAPIC.
9759 */
9760 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9761 if ((exit_intr_info &
9762 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9763 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9764 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9765 vmcs12->vm_exit_intr_error_code =
9766 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9767 }
9768
9769 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9770 vmcs_readl(EXIT_QUALIFICATION));
9771 return 1;
9772}
9773
Avi Kivity586f9602010-11-18 13:09:54 +02009774static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9775{
9776 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9777 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9778}
9779
Kai Huanga3eaa862015-11-04 13:46:05 +08009780static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009781{
Kai Huanga3eaa862015-11-04 13:46:05 +08009782 if (vmx->pml_pg) {
9783 __free_page(vmx->pml_pg);
9784 vmx->pml_pg = NULL;
9785 }
Kai Huang843e4332015-01-28 10:54:28 +08009786}
9787
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009788static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009789{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009790 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009791 u64 *pml_buf;
9792 u16 pml_idx;
9793
9794 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9795
9796 /* Do nothing if PML buffer is empty */
9797 if (pml_idx == (PML_ENTITY_NUM - 1))
9798 return;
9799
9800 /* PML index always points to next available PML buffer entity */
9801 if (pml_idx >= PML_ENTITY_NUM)
9802 pml_idx = 0;
9803 else
9804 pml_idx++;
9805
9806 pml_buf = page_address(vmx->pml_pg);
9807 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9808 u64 gpa;
9809
9810 gpa = pml_buf[pml_idx];
9811 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009812 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009813 }
9814
9815 /* reset PML index */
9816 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9817}
9818
9819/*
9820 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9821 * Called before reporting dirty_bitmap to userspace.
9822 */
9823static void kvm_flush_pml_buffers(struct kvm *kvm)
9824{
9825 int i;
9826 struct kvm_vcpu *vcpu;
9827 /*
9828 * We only need to kick vcpu out of guest mode here, as PML buffer
9829 * is flushed at beginning of all VMEXITs, and it's obvious that only
9830 * vcpus running in guest are possible to have unflushed GPAs in PML
9831 * buffer.
9832 */
9833 kvm_for_each_vcpu(i, vcpu, kvm)
9834 kvm_vcpu_kick(vcpu);
9835}
9836
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009837static void vmx_dump_sel(char *name, uint32_t sel)
9838{
9839 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009840 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009841 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9842 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9843 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9844}
9845
9846static void vmx_dump_dtsel(char *name, uint32_t limit)
9847{
9848 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9849 name, vmcs_read32(limit),
9850 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9851}
9852
9853static void dump_vmcs(void)
9854{
9855 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9856 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9857 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9858 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9859 u32 secondary_exec_control = 0;
9860 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009861 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009862 int i, n;
9863
9864 if (cpu_has_secondary_exec_ctrls())
9865 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9866
9867 pr_err("*** Guest State ***\n");
9868 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9869 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9870 vmcs_readl(CR0_GUEST_HOST_MASK));
9871 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9872 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9873 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9874 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9875 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9876 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009877 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9878 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9879 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9880 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009881 }
9882 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9883 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9884 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9885 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9886 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9887 vmcs_readl(GUEST_SYSENTER_ESP),
9888 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9889 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9890 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9891 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9892 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9893 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9894 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9895 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9896 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9897 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9898 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9899 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9900 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009901 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9902 efer, vmcs_read64(GUEST_IA32_PAT));
9903 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9904 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009905 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009906 if (cpu_has_load_perf_global_ctrl &&
9907 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009908 pr_err("PerfGlobCtl = 0x%016llx\n",
9909 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009910 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009911 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009912 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9913 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9914 vmcs_read32(GUEST_ACTIVITY_STATE));
9915 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9916 pr_err("InterruptStatus = %04x\n",
9917 vmcs_read16(GUEST_INTR_STATUS));
9918
9919 pr_err("*** Host State ***\n");
9920 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9921 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9922 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9923 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9924 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9925 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9926 vmcs_read16(HOST_TR_SELECTOR));
9927 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9928 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9929 vmcs_readl(HOST_TR_BASE));
9930 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9931 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9932 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9933 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9934 vmcs_readl(HOST_CR4));
9935 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9936 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9937 vmcs_read32(HOST_IA32_SYSENTER_CS),
9938 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9939 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009940 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9941 vmcs_read64(HOST_IA32_EFER),
9942 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009943 if (cpu_has_load_perf_global_ctrl &&
9944 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009945 pr_err("PerfGlobCtl = 0x%016llx\n",
9946 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009947
9948 pr_err("*** Control State ***\n");
9949 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9950 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9951 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9952 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9953 vmcs_read32(EXCEPTION_BITMAP),
9954 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9955 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9956 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9957 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9958 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9959 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9960 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9961 vmcs_read32(VM_EXIT_INTR_INFO),
9962 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9963 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9964 pr_err(" reason=%08x qualification=%016lx\n",
9965 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9966 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9967 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9968 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009969 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009970 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009971 pr_err("TSC Multiplier = 0x%016llx\n",
9972 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009973 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9974 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9975 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9976 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9977 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009978 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009979 n = vmcs_read32(CR3_TARGET_COUNT);
9980 for (i = 0; i + 1 < n; i += 4)
9981 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9982 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9983 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9984 if (i < n)
9985 pr_err("CR3 target%u=%016lx\n",
9986 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9987 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9988 pr_err("PLE Gap=%08x Window=%08x\n",
9989 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9990 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9991 pr_err("Virtual processor ID = 0x%04x\n",
9992 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9993}
9994
Avi Kivity6aa8b732006-12-10 02:21:36 -08009995/*
9996 * The guest has exited. See if we can fix it or if we need userspace
9997 * assistance.
9998 */
Avi Kivity851ba692009-08-24 11:10:17 +03009999static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010000{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010001 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010002 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010003 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010004
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010005 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10006
Kai Huang843e4332015-01-28 10:54:28 +080010007 /*
10008 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10009 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10010 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10011 * mode as if vcpus is in root mode, the PML buffer must has been
10012 * flushed already.
10013 */
10014 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010015 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010016
Mohammed Gamal80ced182009-09-01 12:48:18 +020010017 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010018 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010019 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010020
Paolo Bonzini7313c692017-07-27 10:31:25 +020010021 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10022 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010023
Mohammed Gamal51207022010-05-31 22:40:54 +030010024 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010025 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010026 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10027 vcpu->run->fail_entry.hardware_entry_failure_reason
10028 = exit_reason;
10029 return 0;
10030 }
10031
Avi Kivity29bd8a72007-09-10 17:27:03 +030010032 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010033 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10034 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010035 = vmcs_read32(VM_INSTRUCTION_ERROR);
10036 return 0;
10037 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010038
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010039 /*
10040 * Note:
10041 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10042 * delivery event since it indicates guest is accessing MMIO.
10043 * The vm-exit can be triggered again after return to guest that
10044 * will cause infinite loop.
10045 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010046 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010047 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010048 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010049 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010050 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10051 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10052 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010053 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010054 vcpu->run->internal.data[0] = vectoring_info;
10055 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010056 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10057 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10058 vcpu->run->internal.ndata++;
10059 vcpu->run->internal.data[3] =
10060 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10061 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010062 return 0;
10063 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010064
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010065 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010066 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10067 if (vmx_interrupt_allowed(vcpu)) {
10068 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10069 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10070 vcpu->arch.nmi_pending) {
10071 /*
10072 * This CPU don't support us in finding the end of an
10073 * NMI-blocked window if the guest runs with IRQs
10074 * disabled. So we pull the trigger after 1 s of
10075 * futile waiting, but inform the user about this.
10076 */
10077 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10078 "state on VCPU %d after 1 s timeout\n",
10079 __func__, vcpu->vcpu_id);
10080 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10081 }
10082 }
10083
Avi Kivity6aa8b732006-12-10 02:21:36 -080010084 if (exit_reason < kvm_vmx_max_exit_handlers
10085 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010086 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010087 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010088 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10089 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010090 kvm_queue_exception(vcpu, UD_VECTOR);
10091 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010092 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010093}
10094
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010095/*
10096 * Software based L1D cache flush which is used when microcode providing
10097 * the cache control MSR is not loaded.
10098 *
10099 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10100 * flush it is required to read in 64 KiB because the replacement algorithm
10101 * is not exactly LRU. This could be sized at runtime via topology
10102 * information but as all relevant affected CPUs have 32KiB L1D cache size
10103 * there is no point in doing so.
10104 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010105static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010106{
10107 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010108
10109 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010110 * This code is only executed when the the flush mode is 'cond' or
10111 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010112 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010113 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010114 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010115
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010116 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010117 * Clear the per-vcpu flush bit, it gets set again
10118 * either from vcpu_run() or from one of the unsafe
10119 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010120 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010121 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010122 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010123
10124 /*
10125 * Clear the per-cpu flush bit, it gets set again from
10126 * the interrupt handlers.
10127 */
10128 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10129 kvm_clear_cpu_l1tf_flush_l1d();
10130
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010131 if (!flush_l1d)
10132 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010133 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010134
10135 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010136
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010137 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10138 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10139 return;
10140 }
10141
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010142 asm volatile(
10143 /* First ensure the pages are in the TLB */
10144 "xorl %%eax, %%eax\n"
10145 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010146 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010147 "addl $4096, %%eax\n\t"
10148 "cmpl %%eax, %[size]\n\t"
10149 "jne .Lpopulate_tlb\n\t"
10150 "xorl %%eax, %%eax\n\t"
10151 "cpuid\n\t"
10152 /* Now fill the cache */
10153 "xorl %%eax, %%eax\n"
10154 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010155 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010156 "addl $64, %%eax\n\t"
10157 "cmpl %%eax, %[size]\n\t"
10158 "jne .Lfill_cache\n\t"
10159 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010160 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010161 [size] "r" (size)
10162 : "eax", "ebx", "ecx", "edx");
10163}
10164
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010165static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010166{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010167 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10168
10169 if (is_guest_mode(vcpu) &&
10170 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10171 return;
10172
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010173 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010174 vmcs_write32(TPR_THRESHOLD, 0);
10175 return;
10176 }
10177
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010178 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010179}
10180
Jim Mattson8d860bb2018-05-09 16:56:05 -040010181static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010182{
10183 u32 sec_exec_control;
10184
Jim Mattson8d860bb2018-05-09 16:56:05 -040010185 if (!lapic_in_kernel(vcpu))
10186 return;
10187
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010188 if (!flexpriority_enabled &&
10189 !cpu_has_vmx_virtualize_x2apic_mode())
10190 return;
10191
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010192 /* Postpone execution until vmcs01 is the current VMCS. */
10193 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010194 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010195 return;
10196 }
10197
Yang Zhang8d146952013-01-25 10:18:50 +080010198 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010199 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10200 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010201
Jim Mattson8d860bb2018-05-09 16:56:05 -040010202 switch (kvm_get_apic_mode(vcpu)) {
10203 case LAPIC_MODE_INVALID:
10204 WARN_ONCE(true, "Invalid local APIC state");
10205 case LAPIC_MODE_DISABLED:
10206 break;
10207 case LAPIC_MODE_XAPIC:
10208 if (flexpriority_enabled) {
10209 sec_exec_control |=
10210 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10211 vmx_flush_tlb(vcpu, true);
10212 }
10213 break;
10214 case LAPIC_MODE_X2APIC:
10215 if (cpu_has_vmx_virtualize_x2apic_mode())
10216 sec_exec_control |=
10217 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10218 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010219 }
10220 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10221
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010222 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010223}
10224
Tang Chen38b99172014-09-24 15:57:54 +080010225static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10226{
Jim Mattsonab5df312018-05-09 17:02:03 -040010227 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010228 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010229 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010230 }
Tang Chen38b99172014-09-24 15:57:54 +080010231}
10232
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010233static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010234{
10235 u16 status;
10236 u8 old;
10237
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010238 if (max_isr == -1)
10239 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010240
10241 status = vmcs_read16(GUEST_INTR_STATUS);
10242 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010243 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010244 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010245 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010246 vmcs_write16(GUEST_INTR_STATUS, status);
10247 }
10248}
10249
10250static void vmx_set_rvi(int vector)
10251{
10252 u16 status;
10253 u8 old;
10254
Wei Wang4114c272014-11-05 10:53:43 +080010255 if (vector == -1)
10256 vector = 0;
10257
Yang Zhangc7c9c562013-01-25 10:18:51 +080010258 status = vmcs_read16(GUEST_INTR_STATUS);
10259 old = (u8)status & 0xff;
10260 if ((u8)vector != old) {
10261 status &= ~0xff;
10262 status |= (u8)vector;
10263 vmcs_write16(GUEST_INTR_STATUS, status);
10264 }
10265}
10266
10267static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10268{
Liran Alon851c1a182017-12-24 18:12:56 +020010269 /*
10270 * When running L2, updating RVI is only relevant when
10271 * vmcs12 virtual-interrupt-delivery enabled.
10272 * However, it can be enabled only when L1 also
10273 * intercepts external-interrupts and in that case
10274 * we should not update vmcs02 RVI but instead intercept
10275 * interrupt. Therefore, do nothing when running L2.
10276 */
10277 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010278 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010279}
10280
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010281static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010282{
10283 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010284 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010285 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010286
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010287 WARN_ON(!vcpu->arch.apicv_active);
10288 if (pi_test_on(&vmx->pi_desc)) {
10289 pi_clear_on(&vmx->pi_desc);
10290 /*
10291 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10292 * But on x86 this is just a compiler barrier anyway.
10293 */
10294 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010295 max_irr_updated =
10296 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10297
10298 /*
10299 * If we are running L2 and L1 has a new pending interrupt
10300 * which can be injected, we should re-evaluate
10301 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010302 * If L1 intercepts external-interrupts, we should
10303 * exit from L2 to L1. Otherwise, interrupt should be
10304 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010305 */
Liran Alon851c1a182017-12-24 18:12:56 +020010306 if (is_guest_mode(vcpu) && max_irr_updated) {
10307 if (nested_exit_on_intr(vcpu))
10308 kvm_vcpu_exiting_guest_mode(vcpu);
10309 else
10310 kvm_make_request(KVM_REQ_EVENT, vcpu);
10311 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010312 } else {
10313 max_irr = kvm_lapic_find_highest_irr(vcpu);
10314 }
10315 vmx_hwapic_irr_update(vcpu, max_irr);
10316 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010317}
10318
Paolo Bonzini7e712682018-10-03 13:44:26 +020010319static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10320{
10321 u8 rvi = vmx_get_rvi();
10322 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10323
10324 return ((rvi & 0xf0) > (vppr & 0xf0));
10325}
10326
Andrey Smetanin63086302015-11-10 15:36:32 +030010327static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010328{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010329 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010330 return;
10331
Yang Zhangc7c9c562013-01-25 10:18:51 +080010332 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10333 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10334 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10335 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10336}
10337
Paolo Bonzini967235d2016-12-19 14:03:45 +010010338static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10339{
10340 struct vcpu_vmx *vmx = to_vmx(vcpu);
10341
10342 pi_clear_on(&vmx->pi_desc);
10343 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10344}
10345
Avi Kivity51aa01d2010-07-20 14:31:20 +030010346static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010347{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010348 u32 exit_intr_info = 0;
10349 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010350
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010351 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10352 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010353 return;
10354
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010355 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10356 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10357 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010358
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010359 /* if exit due to PF check for async PF */
10360 if (is_page_fault(exit_intr_info))
10361 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10362
Andi Kleena0861c02009-06-08 17:37:09 +080010363 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010364 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10365 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010366 kvm_machine_check();
10367
Gleb Natapov20f65982009-05-11 13:35:55 +030010368 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010369 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010370 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010371 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010372 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010373 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010374}
Gleb Natapov20f65982009-05-11 13:35:55 +030010375
Yang Zhanga547c6d2013-04-11 19:25:10 +080010376static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10377{
10378 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10379
Yang Zhanga547c6d2013-04-11 19:25:10 +080010380 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10381 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10382 unsigned int vector;
10383 unsigned long entry;
10384 gate_desc *desc;
10385 struct vcpu_vmx *vmx = to_vmx(vcpu);
10386#ifdef CONFIG_X86_64
10387 unsigned long tmp;
10388#endif
10389
10390 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10391 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010392 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010393 asm volatile(
10394#ifdef CONFIG_X86_64
10395 "mov %%" _ASM_SP ", %[sp]\n\t"
10396 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10397 "push $%c[ss]\n\t"
10398 "push %[sp]\n\t"
10399#endif
10400 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010401 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010402 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010403 :
10404#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010405 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010406#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010407 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010408 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010409 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010410 [ss]"i"(__KERNEL_DS),
10411 [cs]"i"(__KERNEL_CS)
10412 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010413 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010414}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010415STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010416
Tom Lendackybc226f02018-05-10 22:06:39 +020010417static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010418{
Tom Lendackybc226f02018-05-10 22:06:39 +020010419 switch (index) {
10420 case MSR_IA32_SMBASE:
10421 /*
10422 * We cannot do SMM unless we can run the guest in big
10423 * real mode.
10424 */
10425 return enable_unrestricted_guest || emulate_invalid_guest_state;
10426 case MSR_AMD64_VIRT_SPEC_CTRL:
10427 /* This is AMD only. */
10428 return false;
10429 default:
10430 return true;
10431 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010432}
10433
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010434static bool vmx_mpx_supported(void)
10435{
10436 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10437 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10438}
10439
Wanpeng Li55412b22014-12-02 19:21:30 +080010440static bool vmx_xsaves_supported(void)
10441{
10442 return vmcs_config.cpu_based_2nd_exec_ctrl &
10443 SECONDARY_EXEC_XSAVES;
10444}
10445
Avi Kivity51aa01d2010-07-20 14:31:20 +030010446static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10447{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010448 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010449 bool unblock_nmi;
10450 u8 vector;
10451 bool idtv_info_valid;
10452
10453 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010454
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010455 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010456 if (vmx->loaded_vmcs->nmi_known_unmasked)
10457 return;
10458 /*
10459 * Can't use vmx->exit_intr_info since we're not sure what
10460 * the exit reason is.
10461 */
10462 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10463 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10464 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10465 /*
10466 * SDM 3: 27.7.1.2 (September 2008)
10467 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10468 * a guest IRET fault.
10469 * SDM 3: 23.2.2 (September 2008)
10470 * Bit 12 is undefined in any of the following cases:
10471 * If the VM exit sets the valid bit in the IDT-vectoring
10472 * information field.
10473 * If the VM exit is due to a double fault.
10474 */
10475 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10476 vector != DF_VECTOR && !idtv_info_valid)
10477 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10478 GUEST_INTR_STATE_NMI);
10479 else
10480 vmx->loaded_vmcs->nmi_known_unmasked =
10481 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10482 & GUEST_INTR_STATE_NMI);
10483 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10484 vmx->loaded_vmcs->vnmi_blocked_time +=
10485 ktime_to_ns(ktime_sub(ktime_get(),
10486 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010487}
10488
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010489static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010490 u32 idt_vectoring_info,
10491 int instr_len_field,
10492 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010493{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010494 u8 vector;
10495 int type;
10496 bool idtv_info_valid;
10497
10498 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010499
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010500 vcpu->arch.nmi_injected = false;
10501 kvm_clear_exception_queue(vcpu);
10502 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010503
10504 if (!idtv_info_valid)
10505 return;
10506
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010507 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010508
Avi Kivity668f6122008-07-02 09:28:55 +030010509 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10510 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010511
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010512 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010513 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010514 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010515 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010516 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010517 * Clear bit "block by NMI" before VM entry if a NMI
10518 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010519 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010520 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010521 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010522 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010523 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010524 /* fall through */
10525 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010526 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010527 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010528 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010529 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010530 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010531 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010532 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010533 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010534 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010535 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010536 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010537 break;
10538 default:
10539 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010540 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010541}
10542
Avi Kivity83422e12010-07-20 14:43:23 +030010543static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10544{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010545 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010546 VM_EXIT_INSTRUCTION_LEN,
10547 IDT_VECTORING_ERROR_CODE);
10548}
10549
Avi Kivityb463a6f2010-07-20 15:06:17 +030010550static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10551{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010552 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010553 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10554 VM_ENTRY_INSTRUCTION_LEN,
10555 VM_ENTRY_EXCEPTION_ERROR_CODE);
10556
10557 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10558}
10559
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010560static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10561{
10562 int i, nr_msrs;
10563 struct perf_guest_switch_msr *msrs;
10564
10565 msrs = perf_guest_get_msrs(&nr_msrs);
10566
10567 if (!msrs)
10568 return;
10569
10570 for (i = 0; i < nr_msrs; i++)
10571 if (msrs[i].host == msrs[i].guest)
10572 clear_atomic_switch_msr(vmx, msrs[i].msr);
10573 else
10574 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010575 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010576}
10577
Sean Christophersonf459a702018-08-27 15:21:11 -070010578static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10579{
10580 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10581 if (!vmx->loaded_vmcs->hv_timer_armed)
10582 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10583 PIN_BASED_VMX_PREEMPTION_TIMER);
10584 vmx->loaded_vmcs->hv_timer_armed = true;
10585}
10586
10587static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010588{
10589 struct vcpu_vmx *vmx = to_vmx(vcpu);
10590 u64 tscl;
10591 u32 delta_tsc;
10592
Sean Christophersond264ee02018-08-27 15:21:12 -070010593 if (vmx->req_immediate_exit) {
10594 vmx_arm_hv_timer(vmx, 0);
10595 return;
10596 }
10597
Sean Christophersonf459a702018-08-27 15:21:11 -070010598 if (vmx->hv_deadline_tsc != -1) {
10599 tscl = rdtsc();
10600 if (vmx->hv_deadline_tsc > tscl)
10601 /* set_hv_timer ensures the delta fits in 32-bits */
10602 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10603 cpu_preemption_timer_multi);
10604 else
10605 delta_tsc = 0;
10606
10607 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010608 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010609 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010610
Sean Christophersonf459a702018-08-27 15:21:11 -070010611 if (vmx->loaded_vmcs->hv_timer_armed)
10612 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10613 PIN_BASED_VMX_PREEMPTION_TIMER);
10614 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010615}
10616
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010617static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010618{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010619 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010620 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010621
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010622 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010623 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010624 vmx->loaded_vmcs->soft_vnmi_blocked))
10625 vmx->loaded_vmcs->entry_time = ktime_get();
10626
Avi Kivity104f2262010-11-18 13:12:52 +020010627 /* Don't enter VMX if guest state is invalid, let the exit handler
10628 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010629 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010630 return;
10631
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010632 if (vmx->ple_window_dirty) {
10633 vmx->ple_window_dirty = false;
10634 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10635 }
10636
Abel Gordon012f83c2013-04-18 14:39:25 +030010637 if (vmx->nested.sync_shadow_vmcs) {
10638 copy_vmcs12_to_shadow(vmx);
10639 vmx->nested.sync_shadow_vmcs = false;
10640 }
10641
Avi Kivity104f2262010-11-18 13:12:52 +020010642 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10643 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10644 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10645 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10646
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010647 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010648 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010649 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010650 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010651 }
10652
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010653 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010654 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010655 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010656 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010657 }
10658
Avi Kivity104f2262010-11-18 13:12:52 +020010659 /* When single-stepping over STI and MOV SS, we must clear the
10660 * corresponding interruptibility bits in the guest state. Otherwise
10661 * vmentry fails as it then expects bit 14 (BS) in pending debug
10662 * exceptions being set, but that's not correct for the guest debugging
10663 * case. */
10664 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10665 vmx_set_interrupt_shadow(vcpu, 0);
10666
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010667 if (static_cpu_has(X86_FEATURE_PKU) &&
10668 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10669 vcpu->arch.pkru != vmx->host_pkru)
10670 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010671
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010672 atomic_switch_perf_msrs(vmx);
10673
Sean Christophersonf459a702018-08-27 15:21:11 -070010674 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010675
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010676 /*
10677 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10678 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10679 * is no need to worry about the conditional branch over the wrmsr
10680 * being speculatively taken.
10681 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010682 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010683
Nadav Har'Eld462b812011-05-24 15:26:10 +030010684 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010685
10686 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10687 (unsigned long)&current_evmcs->host_rsp : 0;
10688
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010689 if (static_branch_unlikely(&vmx_l1d_should_flush))
10690 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010691
Avi Kivity104f2262010-11-18 13:12:52 +020010692 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010693 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010694 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10695 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10696 "push %%" _ASM_CX " \n\t"
10697 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010698 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010699 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010700 /* Avoid VMWRITE when Enlightened VMCS is in use */
10701 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10702 "jz 2f \n\t"
10703 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10704 "jmp 1f \n\t"
10705 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010706 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010707 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010708 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010709 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10710 "mov %%cr2, %%" _ASM_DX " \n\t"
10711 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010712 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010713 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010714 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010715 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010716 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010717 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010718 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10719 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10720 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10721 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10722 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10723 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010724#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010725 "mov %c[r8](%0), %%r8 \n\t"
10726 "mov %c[r9](%0), %%r9 \n\t"
10727 "mov %c[r10](%0), %%r10 \n\t"
10728 "mov %c[r11](%0), %%r11 \n\t"
10729 "mov %c[r12](%0), %%r12 \n\t"
10730 "mov %c[r13](%0), %%r13 \n\t"
10731 "mov %c[r14](%0), %%r14 \n\t"
10732 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010733#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010734 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010735
Avi Kivity6aa8b732006-12-10 02:21:36 -080010736 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010737 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010738 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010739 "jmp 2f \n\t"
10740 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10741 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010742 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010743 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010744 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010745 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010746 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10747 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10748 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10749 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10750 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10751 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10752 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010753#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010754 "mov %%r8, %c[r8](%0) \n\t"
10755 "mov %%r9, %c[r9](%0) \n\t"
10756 "mov %%r10, %c[r10](%0) \n\t"
10757 "mov %%r11, %c[r11](%0) \n\t"
10758 "mov %%r12, %c[r12](%0) \n\t"
10759 "mov %%r13, %c[r13](%0) \n\t"
10760 "mov %%r14, %c[r14](%0) \n\t"
10761 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010762 "xor %%r8d, %%r8d \n\t"
10763 "xor %%r9d, %%r9d \n\t"
10764 "xor %%r10d, %%r10d \n\t"
10765 "xor %%r11d, %%r11d \n\t"
10766 "xor %%r12d, %%r12d \n\t"
10767 "xor %%r13d, %%r13d \n\t"
10768 "xor %%r14d, %%r14d \n\t"
10769 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010770#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010771 "mov %%cr2, %%" _ASM_AX " \n\t"
10772 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010773
Jim Mattson0cb5b302018-01-03 14:31:38 -080010774 "xor %%eax, %%eax \n\t"
10775 "xor %%ebx, %%ebx \n\t"
10776 "xor %%esi, %%esi \n\t"
10777 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010778 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010779 ".pushsection .rodata \n\t"
10780 ".global vmx_return \n\t"
10781 "vmx_return: " _ASM_PTR " 2b \n\t"
10782 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010783 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010784 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010785 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010786 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010787 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10788 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10789 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10790 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10791 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10792 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10793 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010794#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010795 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10796 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10797 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10798 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10799 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10800 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10801 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10802 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010803#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010804 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10805 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010806 : "cc", "memory"
10807#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010808 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010809 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010810#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010811 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010812#endif
10813 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010814
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010815 /*
10816 * We do not use IBRS in the kernel. If this vCPU has used the
10817 * SPEC_CTRL MSR it may have left it on; save the value and
10818 * turn it off. This is much more efficient than blindly adding
10819 * it to the atomic save/restore list. Especially as the former
10820 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10821 *
10822 * For non-nested case:
10823 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10824 * save it.
10825 *
10826 * For nested case:
10827 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10828 * save it.
10829 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010830 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010831 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010832
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010833 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010834
David Woodhouse117cc7a2018-01-12 11:11:27 +000010835 /* Eliminate branch target predictions from guest mode */
10836 vmexit_fill_RSB();
10837
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010838 /* All fields are clean at this point */
10839 if (static_branch_unlikely(&enable_evmcs))
10840 current_evmcs->hv_clean_fields |=
10841 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10842
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010843 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010844 if (vmx->host_debugctlmsr)
10845 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010846
Avi Kivityaa67f602012-08-01 16:48:03 +030010847#ifndef CONFIG_X86_64
10848 /*
10849 * The sysexit path does not restore ds/es, so we must set them to
10850 * a reasonable value ourselves.
10851 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010852 * We can't defer this to vmx_prepare_switch_to_host() since that
10853 * function may be executed in interrupt context, which saves and
10854 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010855 */
10856 loadsegment(ds, __USER_DS);
10857 loadsegment(es, __USER_DS);
10858#endif
10859
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010860 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010861 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010862 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010863 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010864 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010865 vcpu->arch.regs_dirty = 0;
10866
Gleb Natapove0b890d2013-09-25 12:51:33 +030010867 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010868 * eager fpu is enabled if PKEY is supported and CR4 is switched
10869 * back on host, so it is safe to read guest PKRU from current
10870 * XSAVE.
10871 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010872 if (static_cpu_has(X86_FEATURE_PKU) &&
10873 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10874 vcpu->arch.pkru = __read_pkru();
10875 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010876 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010877 }
10878
Gleb Natapove0b890d2013-09-25 12:51:33 +030010879 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010880 vmx->idt_vectoring_info = 0;
10881
10882 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10883 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10884 return;
10885
10886 vmx->loaded_vmcs->launched = 1;
10887 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010888
Avi Kivity51aa01d2010-07-20 14:31:20 +030010889 vmx_complete_atomic_exit(vmx);
10890 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010891 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010892}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010893STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010894
Sean Christopherson434a1e92018-03-20 12:17:18 -070010895static struct kvm *vmx_vm_alloc(void)
10896{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010897 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010898 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010899}
10900
10901static void vmx_vm_free(struct kvm *kvm)
10902{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010903 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010904}
10905
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010906static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010907{
10908 struct vcpu_vmx *vmx = to_vmx(vcpu);
10909 int cpu;
10910
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010911 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010912 return;
10913
10914 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010915 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010916 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010917 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010918 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070010919
10920 vm_entry_controls_reset_shadow(vmx);
10921 vm_exit_controls_reset_shadow(vmx);
10922 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010923}
10924
Jim Mattson2f1fe812016-07-08 15:36:06 -070010925/*
10926 * Ensure that the current vmcs of the logical processor is the
10927 * vmcs01 of the vcpu before calling free_nested().
10928 */
10929static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10930{
10931 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010932
Christoffer Dallec7660c2017-12-04 21:35:23 +010010933 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010934 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010935 free_nested(vmx);
10936 vcpu_put(vcpu);
10937}
10938
Avi Kivity6aa8b732006-12-10 02:21:36 -080010939static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10940{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010941 struct vcpu_vmx *vmx = to_vmx(vcpu);
10942
Kai Huang843e4332015-01-28 10:54:28 +080010943 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010944 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010945 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010946 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010947 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010948 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010949 kfree(vmx->guest_msrs);
10950 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010951 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010952}
10953
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010954static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010955{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010956 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010957 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010958 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010959 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010960
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010961 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010962 return ERR_PTR(-ENOMEM);
10963
Wanpeng Li991e7a02015-09-16 17:30:05 +080010964 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010965
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010966 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10967 if (err)
10968 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010969
Peter Feiner4e595162016-07-07 14:49:58 -070010970 err = -ENOMEM;
10971
10972 /*
10973 * If PML is turned on, failure on enabling PML just results in failure
10974 * of creating the vcpu, therefore we can simplify PML logic (by
10975 * avoiding dealing with cases, such as enabling PML partially on vcpus
10976 * for the guest, etc.
10977 */
10978 if (enable_pml) {
10979 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10980 if (!vmx->pml_pg)
10981 goto uninit_vcpu;
10982 }
10983
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010984 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010985 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10986 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010987
Peter Feiner4e595162016-07-07 14:49:58 -070010988 if (!vmx->guest_msrs)
10989 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010990
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010991 err = alloc_loaded_vmcs(&vmx->vmcs01);
10992 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010993 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010994
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010995 msr_bitmap = vmx->vmcs01.msr_bitmap;
10996 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10997 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10998 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10999 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11000 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11001 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11002 vmx->msr_bitmap_mode = 0;
11003
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011004 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011005 cpu = get_cpu();
11006 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011007 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011008 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011009 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011010 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011011 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011012 err = alloc_apic_access_page(kvm);
11013 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011014 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011015 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011016
Sean Christophersone90008d2018-03-05 12:04:37 -080011017 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011018 err = init_rmode_identity_map(kvm);
11019 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011020 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011021 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011022
Roman Kagan63aff652018-07-19 21:59:07 +030011023 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011024 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11025 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011026
Wincy Van705699a2015-02-03 23:58:17 +080011027 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011028 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011029
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011030 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11031
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011032 /*
11033 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11034 * or POSTED_INTR_WAKEUP_VECTOR.
11035 */
11036 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11037 vmx->pi_desc.sn = 1;
11038
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011039 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011040
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011041free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011042 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011043free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011044 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011045free_pml:
11046 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011047uninit_vcpu:
11048 kvm_vcpu_uninit(&vmx->vcpu);
11049free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011050 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011051 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011052 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011053}
11054
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011055#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"
11056#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 -040011057
Wanpeng Lib31c1142018-03-12 04:53:04 -070011058static int vmx_vm_init(struct kvm *kvm)
11059{
Tianyu Lan877ad952018-07-19 08:40:23 +000011060 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11061
Wanpeng Lib31c1142018-03-12 04:53:04 -070011062 if (!ple_gap)
11063 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011064
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011065 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11066 switch (l1tf_mitigation) {
11067 case L1TF_MITIGATION_OFF:
11068 case L1TF_MITIGATION_FLUSH_NOWARN:
11069 /* 'I explicitly don't care' is set */
11070 break;
11071 case L1TF_MITIGATION_FLUSH:
11072 case L1TF_MITIGATION_FLUSH_NOSMT:
11073 case L1TF_MITIGATION_FULL:
11074 /*
11075 * Warn upon starting the first VM in a potentially
11076 * insecure environment.
11077 */
11078 if (cpu_smt_control == CPU_SMT_ENABLED)
11079 pr_warn_once(L1TF_MSG_SMT);
11080 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11081 pr_warn_once(L1TF_MSG_L1D);
11082 break;
11083 case L1TF_MITIGATION_FULL_FORCE:
11084 /* Flush is enforced */
11085 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011086 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011087 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011088 return 0;
11089}
11090
Yang, Sheng002c7f72007-07-31 14:23:01 +030011091static void __init vmx_check_processor_compat(void *rtn)
11092{
11093 struct vmcs_config vmcs_conf;
11094
11095 *(int *)rtn = 0;
11096 if (setup_vmcs_config(&vmcs_conf) < 0)
11097 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011098 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011099 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11100 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11101 smp_processor_id());
11102 *(int *)rtn = -EIO;
11103 }
11104}
11105
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011106static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011107{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011108 u8 cache;
11109 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011110
Sheng Yang522c68c2009-04-27 20:35:43 +080011111 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011112 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011113 * 2. EPT with VT-d:
11114 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011115 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011116 * b. VT-d with snooping control feature: snooping control feature of
11117 * VT-d engine can guarantee the cache correctness. Just set it
11118 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011119 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011120 * consistent with host MTRR
11121 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011122 if (is_mmio) {
11123 cache = MTRR_TYPE_UNCACHABLE;
11124 goto exit;
11125 }
11126
11127 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011128 ipat = VMX_EPT_IPAT_BIT;
11129 cache = MTRR_TYPE_WRBACK;
11130 goto exit;
11131 }
11132
11133 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11134 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011135 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011136 cache = MTRR_TYPE_WRBACK;
11137 else
11138 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011139 goto exit;
11140 }
11141
Xiao Guangrongff536042015-06-15 16:55:22 +080011142 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011143
11144exit:
11145 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011146}
11147
Sheng Yang17cc3932010-01-05 19:02:27 +080011148static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011149{
Sheng Yang878403b2010-01-05 19:02:29 +080011150 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11151 return PT_DIRECTORY_LEVEL;
11152 else
11153 /* For shadow and EPT supported 1GB page */
11154 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011155}
11156
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011157static void vmcs_set_secondary_exec_control(u32 new_ctl)
11158{
11159 /*
11160 * These bits in the secondary execution controls field
11161 * are dynamic, the others are mostly based on the hypervisor
11162 * architecture and the guest's CPUID. Do not touch the
11163 * dynamic bits.
11164 */
11165 u32 mask =
11166 SECONDARY_EXEC_SHADOW_VMCS |
11167 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011168 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11169 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011170
11171 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11172
11173 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11174 (new_ctl & ~mask) | (cur_ctl & mask));
11175}
11176
David Matlack8322ebb2016-11-29 18:14:09 -080011177/*
11178 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11179 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11180 */
11181static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11182{
11183 struct vcpu_vmx *vmx = to_vmx(vcpu);
11184 struct kvm_cpuid_entry2 *entry;
11185
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011186 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11187 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011188
11189#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11190 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011191 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011192} while (0)
11193
11194 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11195 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11196 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11197 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11198 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11199 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11200 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11201 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11202 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11203 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11204 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11205 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11206 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11207 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11208 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11209
11210 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11211 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11212 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11213 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11214 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011215 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011216
11217#undef cr4_fixed1_update
11218}
11219
Liran Alon5f76f6f2018-09-14 03:25:52 +030011220static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11221{
11222 struct vcpu_vmx *vmx = to_vmx(vcpu);
11223
11224 if (kvm_mpx_supported()) {
11225 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11226
11227 if (mpx_enabled) {
11228 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11229 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11230 } else {
11231 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11232 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11233 }
11234 }
11235}
11236
Sheng Yang0e851882009-12-18 16:48:46 +080011237static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11238{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011239 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011240
Paolo Bonzini80154d72017-08-24 13:55:35 +020011241 if (cpu_has_secondary_exec_ctrls()) {
11242 vmx_compute_secondary_exec_control(vmx);
11243 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011244 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011245
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011246 if (nested_vmx_allowed(vcpu))
11247 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11248 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11249 else
11250 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11251 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011252
Liran Alon5f76f6f2018-09-14 03:25:52 +030011253 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011254 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011255 nested_vmx_entry_exit_ctls_update(vcpu);
11256 }
Sheng Yang0e851882009-12-18 16:48:46 +080011257}
11258
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011259static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11260{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011261 if (func == 1 && nested)
11262 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011263}
11264
Yang Zhang25d92082013-08-06 12:00:32 +030011265static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11266 struct x86_exception *fault)
11267{
Jan Kiszka533558b2014-01-04 18:47:20 +010011268 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011269 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011270 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011271 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011272
Bandan Dasc5f983f2017-05-05 15:25:14 -040011273 if (vmx->nested.pml_full) {
11274 exit_reason = EXIT_REASON_PML_FULL;
11275 vmx->nested.pml_full = false;
11276 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11277 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011278 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011279 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011280 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011281
11282 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011283 vmcs12->guest_physical_address = fault->address;
11284}
11285
Peter Feiner995f00a2017-06-30 17:26:32 -070011286static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11287{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011288 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011289}
11290
Nadav Har'El155a97a2013-08-05 11:07:16 +030011291/* Callbacks for nested_ept_init_mmu_context: */
11292
11293static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11294{
11295 /* return the page table to be shadowed - in our case, EPT12 */
11296 return get_vmcs12(vcpu)->ept_pointer;
11297}
11298
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011299static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011300{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011301 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011302
Paolo Bonziniad896af2013-10-02 16:56:14 +020011303 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011304 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011305 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011306 nested_ept_ad_enabled(vcpu),
11307 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011308 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
11309 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
11310 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +020011311 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011312
11313 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011314}
11315
11316static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11317{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011318 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011319}
11320
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011321static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11322 u16 error_code)
11323{
11324 bool inequality, bit;
11325
11326 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11327 inequality =
11328 (error_code & vmcs12->page_fault_error_code_mask) !=
11329 vmcs12->page_fault_error_code_match;
11330 return inequality ^ bit;
11331}
11332
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011333static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11334 struct x86_exception *fault)
11335{
11336 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11337
11338 WARN_ON(!is_guest_mode(vcpu));
11339
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011340 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11341 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011342 vmcs12->vm_exit_intr_error_code = fault->error_code;
11343 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11344 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11345 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11346 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011347 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011348 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011349 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011350}
11351
Paolo Bonzinic9923842017-12-13 14:16:30 +010011352static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11353 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011354
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011355static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011356{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011357 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011358 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011359 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011360 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011361
11362 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011363 /*
11364 * Translate L1 physical address to host physical
11365 * address for vmcs02. Keep the page pinned, so this
11366 * physical address remains valid. We keep a reference
11367 * to it so we can release it later.
11368 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011369 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011370 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011371 vmx->nested.apic_access_page = NULL;
11372 }
11373 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011374 /*
11375 * If translation failed, no matter: This feature asks
11376 * to exit when accessing the given address, and if it
11377 * can never be accessed, this feature won't do
11378 * anything anyway.
11379 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011380 if (!is_error_page(page)) {
11381 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011382 hpa = page_to_phys(vmx->nested.apic_access_page);
11383 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11384 } else {
11385 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11386 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11387 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011388 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011389
11390 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011391 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011392 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011393 vmx->nested.virtual_apic_page = NULL;
11394 }
11395 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011396
11397 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011398 * If translation failed, VM entry will fail because
11399 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11400 * Failing the vm entry is _not_ what the processor
11401 * does but it's basically the only possibility we
11402 * have. We could still enter the guest if CR8 load
11403 * exits are enabled, CR8 store exits are enabled, and
11404 * virtualize APIC access is disabled; in this case
11405 * the processor would never use the TPR shadow and we
11406 * could simply clear the bit from the execution
11407 * control. But such a configuration is useless, so
11408 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011409 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011410 if (!is_error_page(page)) {
11411 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011412 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11413 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11414 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011415 }
11416
Wincy Van705699a2015-02-03 23:58:17 +080011417 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011418 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11419 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011420 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011421 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011422 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011423 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11424 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011425 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011426 vmx->nested.pi_desc_page = page;
11427 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011428 vmx->nested.pi_desc =
11429 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11430 (unsigned long)(vmcs12->posted_intr_desc_addr &
11431 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011432 vmcs_write64(POSTED_INTR_DESC_ADDR,
11433 page_to_phys(vmx->nested.pi_desc_page) +
11434 (unsigned long)(vmcs12->posted_intr_desc_addr &
11435 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011436 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011437 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011438 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11439 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011440 else
11441 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11442 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011443}
11444
Jan Kiszkaf4124502014-03-07 20:03:13 +010011445static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11446{
11447 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11448 struct vcpu_vmx *vmx = to_vmx(vcpu);
11449
Sean Christopherson4c008122018-08-27 15:21:10 -070011450 /*
11451 * A timer value of zero is architecturally guaranteed to cause
11452 * a VMExit prior to executing any instructions in the guest.
11453 */
11454 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011455 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11456 return;
11457 }
11458
Sean Christopherson4c008122018-08-27 15:21:10 -070011459 if (vcpu->arch.virtual_tsc_khz == 0)
11460 return;
11461
Jan Kiszkaf4124502014-03-07 20:03:13 +010011462 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11463 preemption_timeout *= 1000000;
11464 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11465 hrtimer_start(&vmx->nested.preemption_timer,
11466 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11467}
11468
Jim Mattson56a20512017-07-06 16:33:06 -070011469static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11470 struct vmcs12 *vmcs12)
11471{
11472 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11473 return 0;
11474
11475 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11476 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11477 return -EINVAL;
11478
11479 return 0;
11480}
11481
Wincy Van3af18d92015-02-03 23:49:31 +080011482static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11483 struct vmcs12 *vmcs12)
11484{
Wincy Van3af18d92015-02-03 23:49:31 +080011485 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11486 return 0;
11487
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011488 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011489 return -EINVAL;
11490
11491 return 0;
11492}
11493
Jim Mattson712b12d2017-08-24 13:24:47 -070011494static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11495 struct vmcs12 *vmcs12)
11496{
11497 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11498 return 0;
11499
11500 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11501 return -EINVAL;
11502
11503 return 0;
11504}
11505
Wincy Van3af18d92015-02-03 23:49:31 +080011506/*
11507 * Merge L0's and L1's MSR bitmap, return false to indicate that
11508 * we do not use the hardware.
11509 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011510static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11511 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011512{
Wincy Van82f0dd42015-02-03 23:57:18 +080011513 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011514 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011515 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011516 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011517 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011518 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011519 *
11520 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11521 * ensures that we do not accidentally generate an L02 MSR bitmap
11522 * from the L12 MSR bitmap that is too permissive.
11523 * 2. That L1 or L2s have actually used the MSR. This avoids
11524 * unnecessarily merging of the bitmap if the MSR is unused. This
11525 * works properly because we only update the L01 MSR bitmap lazily.
11526 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11527 * updated to reflect this when L1 (or its L2s) actually write to
11528 * the MSR.
11529 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011530 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11531 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011532
Paolo Bonzinic9923842017-12-13 14:16:30 +010011533 /* Nothing to do if the MSR bitmap is not in use. */
11534 if (!cpu_has_vmx_msr_bitmap() ||
11535 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11536 return false;
11537
Ashok Raj15d45072018-02-01 22:59:43 +010011538 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011539 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011540 return false;
11541
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011542 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11543 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011544 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011545
Radim Krčmářd048c092016-08-08 20:16:22 +020011546 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011547 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11548 /*
11549 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11550 * just lets the processor take the value from the virtual-APIC page;
11551 * take those 256 bits directly from the L1 bitmap.
11552 */
11553 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11554 unsigned word = msr / BITS_PER_LONG;
11555 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11556 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011557 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011558 } else {
11559 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11560 unsigned word = msr / BITS_PER_LONG;
11561 msr_bitmap_l0[word] = ~0;
11562 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11563 }
11564 }
11565
11566 nested_vmx_disable_intercept_for_msr(
11567 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011568 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011569 MSR_TYPE_W);
11570
11571 if (nested_cpu_has_vid(vmcs12)) {
11572 nested_vmx_disable_intercept_for_msr(
11573 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011574 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011575 MSR_TYPE_W);
11576 nested_vmx_disable_intercept_for_msr(
11577 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011578 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011579 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011580 }
Ashok Raj15d45072018-02-01 22:59:43 +010011581
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011582 if (spec_ctrl)
11583 nested_vmx_disable_intercept_for_msr(
11584 msr_bitmap_l1, msr_bitmap_l0,
11585 MSR_IA32_SPEC_CTRL,
11586 MSR_TYPE_R | MSR_TYPE_W);
11587
Ashok Raj15d45072018-02-01 22:59:43 +010011588 if (pred_cmd)
11589 nested_vmx_disable_intercept_for_msr(
11590 msr_bitmap_l1, msr_bitmap_l0,
11591 MSR_IA32_PRED_CMD,
11592 MSR_TYPE_W);
11593
Wincy Vanf2b93282015-02-03 23:56:03 +080011594 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011595 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011596
11597 return true;
11598}
11599
Liran Alon61ada742018-06-23 02:35:08 +030011600static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11601 struct vmcs12 *vmcs12)
11602{
11603 struct vmcs12 *shadow;
11604 struct page *page;
11605
11606 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11607 vmcs12->vmcs_link_pointer == -1ull)
11608 return;
11609
11610 shadow = get_shadow_vmcs12(vcpu);
11611 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11612
11613 memcpy(shadow, kmap(page), VMCS12_SIZE);
11614
11615 kunmap(page);
11616 kvm_release_page_clean(page);
11617}
11618
11619static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11620 struct vmcs12 *vmcs12)
11621{
11622 struct vcpu_vmx *vmx = to_vmx(vcpu);
11623
11624 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11625 vmcs12->vmcs_link_pointer == -1ull)
11626 return;
11627
11628 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11629 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11630}
11631
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011632static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11633 struct vmcs12 *vmcs12)
11634{
11635 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11636 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11637 return -EINVAL;
11638 else
11639 return 0;
11640}
11641
Wincy Vanf2b93282015-02-03 23:56:03 +080011642static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11643 struct vmcs12 *vmcs12)
11644{
Wincy Van82f0dd42015-02-03 23:57:18 +080011645 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011646 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011647 !nested_cpu_has_vid(vmcs12) &&
11648 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011649 return 0;
11650
11651 /*
11652 * If virtualize x2apic mode is enabled,
11653 * virtualize apic access must be disabled.
11654 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011655 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11656 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011657 return -EINVAL;
11658
Wincy Van608406e2015-02-03 23:57:51 +080011659 /*
11660 * If virtual interrupt delivery is enabled,
11661 * we must exit on external interrupts.
11662 */
11663 if (nested_cpu_has_vid(vmcs12) &&
11664 !nested_exit_on_intr(vcpu))
11665 return -EINVAL;
11666
Wincy Van705699a2015-02-03 23:58:17 +080011667 /*
11668 * bits 15:8 should be zero in posted_intr_nv,
11669 * the descriptor address has been already checked
11670 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011671 *
11672 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011673 */
11674 if (nested_cpu_has_posted_intr(vmcs12) &&
11675 (!nested_cpu_has_vid(vmcs12) ||
11676 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011677 (vmcs12->posted_intr_nv & 0xff00) ||
11678 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11679 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011680 return -EINVAL;
11681
Wincy Vanf2b93282015-02-03 23:56:03 +080011682 /* tpr shadow is needed by all apicv features. */
11683 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11684 return -EINVAL;
11685
11686 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011687}
11688
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011689static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11690 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011691 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011692{
Liran Alone2536742018-06-23 02:35:02 +030011693 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011694 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011695 u64 count, addr;
11696
Liran Alone2536742018-06-23 02:35:02 +030011697 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11698 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011699 WARN_ON(1);
11700 return -EINVAL;
11701 }
11702 if (count == 0)
11703 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011704 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011705 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11706 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011707 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011708 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11709 addr_field, maxphyaddr, count, addr);
11710 return -EINVAL;
11711 }
11712 return 0;
11713}
11714
11715static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11716 struct vmcs12 *vmcs12)
11717{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011718 if (vmcs12->vm_exit_msr_load_count == 0 &&
11719 vmcs12->vm_exit_msr_store_count == 0 &&
11720 vmcs12->vm_entry_msr_load_count == 0)
11721 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011722 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011723 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011724 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011725 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011726 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011727 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011728 return -EINVAL;
11729 return 0;
11730}
11731
Bandan Dasc5f983f2017-05-05 15:25:14 -040011732static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11733 struct vmcs12 *vmcs12)
11734{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011735 if (!nested_cpu_has_pml(vmcs12))
11736 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011737
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011738 if (!nested_cpu_has_ept(vmcs12) ||
11739 !page_address_valid(vcpu, vmcs12->pml_address))
11740 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011741
11742 return 0;
11743}
11744
Liran Alona8a7c022018-06-23 02:35:06 +030011745static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11746 struct vmcs12 *vmcs12)
11747{
11748 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11749 return 0;
11750
11751 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11752 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11753 return -EINVAL;
11754
11755 return 0;
11756}
11757
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011758static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11759 struct vmx_msr_entry *e)
11760{
11761 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011762 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011763 return -EINVAL;
11764 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11765 e->index == MSR_IA32_UCODE_REV)
11766 return -EINVAL;
11767 if (e->reserved != 0)
11768 return -EINVAL;
11769 return 0;
11770}
11771
11772static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11773 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011774{
11775 if (e->index == MSR_FS_BASE ||
11776 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011777 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11778 nested_vmx_msr_check_common(vcpu, e))
11779 return -EINVAL;
11780 return 0;
11781}
11782
11783static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11784 struct vmx_msr_entry *e)
11785{
11786 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11787 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011788 return -EINVAL;
11789 return 0;
11790}
11791
11792/*
11793 * Load guest's/host's msr at nested entry/exit.
11794 * return 0 for success, entry index for failure.
11795 */
11796static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11797{
11798 u32 i;
11799 struct vmx_msr_entry e;
11800 struct msr_data msr;
11801
11802 msr.host_initiated = false;
11803 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011804 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11805 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011806 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011807 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11808 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011809 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011810 }
11811 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011812 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011813 "%s check failed (%u, 0x%x, 0x%x)\n",
11814 __func__, i, e.index, e.reserved);
11815 goto fail;
11816 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011817 msr.index = e.index;
11818 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011819 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011820 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011821 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11822 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011823 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011824 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011825 }
11826 return 0;
11827fail:
11828 return i + 1;
11829}
11830
11831static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11832{
11833 u32 i;
11834 struct vmx_msr_entry e;
11835
11836 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011837 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011838 if (kvm_vcpu_read_guest(vcpu,
11839 gpa + i * sizeof(e),
11840 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011841 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011842 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11843 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011844 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011845 }
11846 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011847 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011848 "%s check failed (%u, 0x%x, 0x%x)\n",
11849 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011850 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011851 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011852 msr_info.host_initiated = false;
11853 msr_info.index = e.index;
11854 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011855 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011856 "%s cannot read MSR (%u, 0x%x)\n",
11857 __func__, i, e.index);
11858 return -EINVAL;
11859 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011860 if (kvm_vcpu_write_guest(vcpu,
11861 gpa + i * sizeof(e) +
11862 offsetof(struct vmx_msr_entry, value),
11863 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011864 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011865 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011866 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011867 return -EINVAL;
11868 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011869 }
11870 return 0;
11871}
11872
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011873static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11874{
11875 unsigned long invalid_mask;
11876
11877 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11878 return (val & invalid_mask) == 0;
11879}
11880
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011881/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011882 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11883 * emulating VM entry into a guest with EPT enabled.
11884 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11885 * is assigned to entry_failure_code on failure.
11886 */
11887static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011888 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011889{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011890 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011891 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011892 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11893 return 1;
11894 }
11895
11896 /*
11897 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11898 * must not be dereferenced.
11899 */
11900 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11901 !nested_ept) {
11902 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11903 *entry_failure_code = ENTRY_FAIL_PDPTE;
11904 return 1;
11905 }
11906 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011907 }
11908
Junaid Shahid50c28f22018-06-27 14:59:11 -070011909 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011910 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011911
11912 vcpu->arch.cr3 = cr3;
11913 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11914
11915 kvm_init_mmu(vcpu, false);
11916
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011917 return 0;
11918}
11919
Liran Alonefebf0a2018-10-08 23:42:20 +030011920/*
11921 * Returns if KVM is able to config CPU to tag TLB entries
11922 * populated by L2 differently than TLB entries populated
11923 * by L1.
11924 *
11925 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11926 *
11927 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11928 * with different VPID (L1 entries are tagged with vmx->vpid
11929 * while L2 entries are tagged with vmx->nested.vpid02).
11930 */
11931static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11932{
11933 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11934
11935 return nested_cpu_has_ept(vmcs12) ||
11936 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11937}
11938
Sean Christopherson3df5c372018-09-26 09:23:44 -070011939static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
11940{
11941 if (vmx->nested.nested_run_pending &&
11942 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11943 return vmcs12->guest_ia32_efer;
11944 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11945 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
11946 else
11947 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
11948}
11949
Sean Christopherson09abe322018-09-26 09:23:50 -070011950static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011951{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011952 /*
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011953 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070011954 * according to L0's settings (vmcs12 is irrelevant here). Host
11955 * fields that come from L0 and are not constant, e.g. HOST_CR3,
11956 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011957 */
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011958 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070011959 return;
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011960 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011961
Sean Christopherson52017602018-09-26 09:23:57 -070011962 /*
11963 * We don't care what the EPTP value is we just need to guarantee
11964 * it's valid so we don't get a false positive when doing early
11965 * consistency checks.
11966 */
11967 if (enable_ept && nested_early_check)
11968 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
11969
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011970 /* All VMFUNCs are currently emulated through L0 vmexits. */
11971 if (cpu_has_vmx_vmfunc())
11972 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11973
Sean Christopherson09abe322018-09-26 09:23:50 -070011974 if (cpu_has_vmx_posted_intr())
11975 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11976
11977 if (cpu_has_vmx_msr_bitmap())
11978 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
11979
11980 if (enable_pml)
11981 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011982
11983 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070011984 * Set the MSR load/store lists to match L0's settings. Only the
11985 * addresses are constant (for vmcs02), the counts can change based
11986 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011987 */
11988 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011989 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011990 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011991
Sean Christopherson09abe322018-09-26 09:23:50 -070011992 vmx_set_constant_host_state(vmx);
11993}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011994
Sean Christopherson09abe322018-09-26 09:23:50 -070011995static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
11996 struct vmcs12 *vmcs12)
11997{
11998 prepare_vmcs02_constant_state(vmx);
11999
12000 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012001
12002 if (enable_vpid) {
12003 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12004 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12005 else
12006 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12007 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012008}
12009
Sean Christopherson09abe322018-09-26 09:23:50 -070012010static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012011{
Bandan Das03efce62017-05-05 15:25:15 -040012012 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012013 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012014
Sean Christopherson09abe322018-09-26 09:23:50 -070012015 if (vmx->nested.dirty_vmcs12)
12016 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012017
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012018 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012019 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12020 * entry, but only if the current (host) sp changed from the value
12021 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12022 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070012023 * here we just force the write to happen on entry. host_rsp will
12024 * also be written unconditionally by nested_vmx_check_vmentry_hw()
12025 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012026 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012027 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012028
Sean Christopherson09abe322018-09-26 09:23:50 -070012029 /*
12030 * PIN CONTROLS
12031 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012032 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012033
Sean Christophersonf459a702018-08-27 15:21:11 -070012034 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012035 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012036 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12037 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012038
12039 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012040 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012041 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12042 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012043 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012044 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012045 }
Jan Kiszkaf4124502014-03-07 20:03:13 +010012046 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012047
Sean Christopherson09abe322018-09-26 09:23:50 -070012048 /*
12049 * EXEC CONTROLS
12050 */
12051 exec_control = vmx_exec_control(vmx); /* L0's desires */
12052 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12053 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12054 exec_control &= ~CPU_BASED_TPR_SHADOW;
12055 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012056
Sean Christopherson09abe322018-09-26 09:23:50 -070012057 /*
12058 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12059 * nested_get_vmcs12_pages can't fix it up, the illegal value
12060 * will result in a VM entry failure.
12061 */
12062 if (exec_control & CPU_BASED_TPR_SHADOW) {
12063 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12064 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12065 } else {
12066#ifdef CONFIG_X86_64
12067 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12068 CPU_BASED_CR8_STORE_EXITING;
12069#endif
12070 }
12071
12072 /*
12073 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12074 * for I/O port accesses.
12075 */
12076 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12077 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12078 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12079
12080 /*
12081 * SECONDARY EXEC CONTROLS
12082 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012083 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012084 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012085
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012086 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012087 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012088 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012089 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012090 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012091 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012092 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12093 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012094 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012095 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12096 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12097 ~SECONDARY_EXEC_ENABLE_PML;
12098 exec_control |= vmcs12_exec_ctrl;
12099 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012100
Liran Alon32c7acf2018-06-23 02:35:11 +030012101 /* VMCS shadowing for L2 is emulated for now */
12102 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12103
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012104 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012105 vmcs_write16(GUEST_INTR_STATUS,
12106 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012107
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012108 /*
12109 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12110 * nested_get_vmcs12_pages will either fix it up or
12111 * remove the VM execution control.
12112 */
12113 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12114 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12115
Sean Christopherson0b665d32018-08-14 09:33:34 -070012116 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12117 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12118
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012119 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12120 }
12121
Jim Mattson83bafef2016-10-04 10:48:38 -070012122 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012123 * ENTRY CONTROLS
12124 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012125 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012126 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12127 * on the related bits (if supported by the CPU) in the hope that
12128 * we can avoid VMWrites during vmx_set_efer().
Sean Christopherson3df5c372018-09-26 09:23:44 -070012129 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012130 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12131 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12132 if (cpu_has_load_ia32_efer) {
12133 if (guest_efer & EFER_LMA)
12134 exec_control |= VM_ENTRY_IA32E_MODE;
12135 if (guest_efer != host_efer)
12136 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12137 }
12138 vm_entry_controls_init(vmx, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012139
Sean Christopherson09abe322018-09-26 09:23:50 -070012140 /*
12141 * EXIT CONTROLS
12142 *
12143 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12144 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12145 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
12146 */
12147 exec_control = vmcs_config.vmexit_ctrl;
12148 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12149 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12150 vm_exit_controls_init(vmx, exec_control);
12151
12152 /*
12153 * Conceptually we want to copy the PML address and index from
12154 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12155 * since we always flush the log on each vmexit and never change
12156 * the PML address (once set), this happens to be equivalent to
12157 * simply resetting the index in vmcs02.
12158 */
12159 if (enable_pml)
12160 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12161
12162 /*
12163 * Interrupt/Exception Fields
12164 */
12165 if (vmx->nested.nested_run_pending) {
12166 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12167 vmcs12->vm_entry_intr_info_field);
12168 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12169 vmcs12->vm_entry_exception_error_code);
12170 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12171 vmcs12->vm_entry_instruction_len);
12172 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12173 vmcs12->guest_interruptibility_info);
12174 vmx->loaded_vmcs->nmi_known_unmasked =
12175 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12176 } else {
12177 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12178 }
12179}
12180
12181static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12182{
12183 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12184 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12185 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12186 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12187 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12188 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12189 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12190 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12191 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12192 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12193 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12194 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12195 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12196 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12197 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12198 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12199 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12200 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12201 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12202 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12203 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12204 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12205 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12206 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12207 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12208 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12209 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12210 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12211 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12212 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12213 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12214
12215 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12216 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12217 vmcs12->guest_pending_dbg_exceptions);
12218 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12219 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12220
12221 if (nested_cpu_has_xsaves(vmcs12))
12222 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12223
12224 /*
12225 * Whether page-faults are trapped is determined by a combination of
12226 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12227 * If enable_ept, L0 doesn't care about page faults and we should
12228 * set all of these to L1's desires. However, if !enable_ept, L0 does
12229 * care about (at least some) page faults, and because it is not easy
12230 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12231 * to exit on each and every L2 page fault. This is done by setting
12232 * MASK=MATCH=0 and (see below) EB.PF=1.
12233 * Note that below we don't need special code to set EB.PF beyond the
12234 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12235 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12236 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12237 */
12238 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12239 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12240 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12241 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12242
12243 if (cpu_has_vmx_apicv()) {
12244 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12245 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12246 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12247 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12248 }
12249
12250 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12251 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12252
12253 set_cr4_guest_host_mask(vmx);
12254
12255 if (kvm_mpx_supported()) {
12256 if (vmx->nested.nested_run_pending &&
12257 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12258 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12259 else
12260 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12261 }
12262
12263 /*
12264 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12265 */
12266 if (enable_ept) {
12267 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12268 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12269 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12270 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12271 }
12272}
12273
12274/*
12275 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12276 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12277 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12278 * guest in a way that will both be appropriate to L1's requests, and our
12279 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12280 * function also has additional necessary side-effects, like setting various
12281 * vcpu->arch fields.
12282 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12283 * is assigned to entry_failure_code on failure.
12284 */
12285static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12286 u32 *entry_failure_code)
12287{
12288 struct vcpu_vmx *vmx = to_vmx(vcpu);
12289
12290 if (vmx->nested.dirty_vmcs12) {
12291 prepare_vmcs02_full(vmx, vmcs12);
12292 vmx->nested.dirty_vmcs12 = false;
12293 }
12294
12295 /*
12296 * First, the fields that are shadowed. This must be kept in sync
12297 * with vmx_shadow_fields.h.
12298 */
12299
12300 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12301 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12302 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12303 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12304 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12305
12306 if (vmx->nested.nested_run_pending &&
12307 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12308 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12309 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12310 } else {
12311 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12312 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12313 }
12314 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12315
12316 vmx->nested.preemption_timer_expired = false;
12317 if (nested_cpu_has_preemption_timer(vmcs12))
12318 vmx_start_preemption_timer(vcpu);
12319
12320 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12321 * bitwise-or of what L1 wants to trap for L2, and what we want to
12322 * trap. Note that CR0.TS also needs updating - we do this later.
12323 */
12324 update_exception_bitmap(vcpu);
12325 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12326 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12327
Jim Mattson6514dc32018-04-26 16:09:12 -070012328 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012329 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012330 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012331 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012332 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012333 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012334 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012335
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012336 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12337
Peter Feinerc95ba922016-08-17 09:36:47 -070012338 if (kvm_has_tsc_control)
12339 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012340
12341 if (enable_vpid) {
12342 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012343 * There is no direct mapping between vpid02 and vpid12, the
12344 * vpid02 is per-vCPU for L0 and reused while the value of
12345 * vpid12 is changed w/ one invvpid during nested vmentry.
12346 * The vpid12 is allocated by L1 for L2, so it will not
12347 * influence global bitmap(for vpid01 and vpid02 allocation)
12348 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012349 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012350 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012351 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12352 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012353 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012354 }
12355 } else {
Liran Alon14389212018-10-08 23:42:17 +030012356 /*
12357 * If L1 use EPT, then L0 needs to execute INVEPT on
12358 * EPTP02 instead of EPTP01. Therefore, delay TLB
12359 * flush until vmcs02->eptp is fully updated by
12360 * KVM_REQ_LOAD_CR3. Note that this assumes
12361 * KVM_REQ_TLB_FLUSH is evaluated after
12362 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12363 */
12364 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012365 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012366 }
12367
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012368 if (nested_cpu_has_ept(vmcs12))
12369 nested_ept_init_mmu_context(vcpu);
12370 else if (nested_cpu_has2(vmcs12,
12371 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012372 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012373
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012374 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012375 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12376 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012377 * The CR0_READ_SHADOW is what L2 should have expected to read given
12378 * the specifications by L1; It's not enough to take
12379 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12380 * have more bits than L1 expected.
12381 */
12382 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12383 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12384
12385 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12386 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12387
Sean Christopherson09abe322018-09-26 09:23:50 -070012388 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012389 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012390 vmx_set_efer(vcpu, vcpu->arch.efer);
12391
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012392 /*
12393 * Guest state is invalid and unrestricted guest is disabled,
12394 * which means L1 attempted VMEntry to L2 with invalid state.
12395 * Fail the VMEntry.
12396 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012397 if (vmx->emulation_required) {
12398 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012399 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012400 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012401
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012402 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012403 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012404 entry_failure_code))
12405 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012406
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012407 if (!enable_ept)
12408 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12409
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012410 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12411 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012412 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012413}
12414
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012415static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12416{
12417 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12418 nested_cpu_has_virtual_nmis(vmcs12))
12419 return -EINVAL;
12420
12421 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12422 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12423 return -EINVAL;
12424
12425 return 0;
12426}
12427
Jim Mattsonca0bde22016-11-30 12:03:46 -080012428static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12429{
12430 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012431 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012432
12433 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12434 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12435 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12436
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012437 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12438 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12439
Jim Mattson56a20512017-07-06 16:33:06 -070012440 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12441 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12442
Jim Mattsonca0bde22016-11-30 12:03:46 -080012443 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12444 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12445
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012446 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12447 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12448
Jim Mattson712b12d2017-08-24 13:24:47 -070012449 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12450 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12451
Jim Mattsonca0bde22016-11-30 12:03:46 -080012452 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12453 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12454
12455 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12456 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12457
Bandan Dasc5f983f2017-05-05 15:25:14 -040012458 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12459 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12460
Liran Alona8a7c022018-06-23 02:35:06 +030012461 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12462 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12463
Jim Mattsonca0bde22016-11-30 12:03:46 -080012464 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012465 vmx->nested.msrs.procbased_ctls_low,
12466 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012467 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12468 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012469 vmx->nested.msrs.secondary_ctls_low,
12470 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012471 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012472 vmx->nested.msrs.pinbased_ctls_low,
12473 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012474 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012475 vmx->nested.msrs.exit_ctls_low,
12476 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012477 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012478 vmx->nested.msrs.entry_ctls_low,
12479 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012480 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12481
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012482 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012483 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12484
Bandan Das41ab9372017-08-03 15:54:43 -040012485 if (nested_cpu_has_vmfunc(vmcs12)) {
12486 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012487 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012488 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12489
12490 if (nested_cpu_has_eptp_switching(vmcs12)) {
12491 if (!nested_cpu_has_ept(vmcs12) ||
12492 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12493 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12494 }
12495 }
Bandan Das27c42a12017-08-03 15:54:42 -040012496
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012497 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12498 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12499
Jim Mattsonca0bde22016-11-30 12:03:46 -080012500 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12501 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12502 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12503 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12504
Marc Orr04473782018-06-20 17:21:29 -070012505 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012506 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12507 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12508 * the values of the LMA and LME bits in the field must each be that of
12509 * the host address-space size VM-exit control.
12510 */
12511 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12512 ia32e = (vmcs12->vm_exit_controls &
12513 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12514 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12515 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12516 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12517 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12518 }
12519
12520 /*
Marc Orr04473782018-06-20 17:21:29 -070012521 * From the Intel SDM, volume 3:
12522 * Fields relevant to VM-entry event injection must be set properly.
12523 * These fields are the VM-entry interruption-information field, the
12524 * VM-entry exception error code, and the VM-entry instruction length.
12525 */
12526 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12527 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12528 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12529 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12530 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12531 bool should_have_error_code;
12532 bool urg = nested_cpu_has2(vmcs12,
12533 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12534 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12535
12536 /* VM-entry interruption-info field: interruption type */
12537 if (intr_type == INTR_TYPE_RESERVED ||
12538 (intr_type == INTR_TYPE_OTHER_EVENT &&
12539 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12540 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12541
12542 /* VM-entry interruption-info field: vector */
12543 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12544 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12545 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12546 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12547
12548 /* VM-entry interruption-info field: deliver error code */
12549 should_have_error_code =
12550 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12551 x86_exception_has_error_code(vector);
12552 if (has_error_code != should_have_error_code)
12553 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12554
12555 /* VM-entry exception error code */
12556 if (has_error_code &&
12557 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12558 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12559
12560 /* VM-entry interruption-info field: reserved bits */
12561 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12562 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12563
12564 /* VM-entry instruction length */
12565 switch (intr_type) {
12566 case INTR_TYPE_SOFT_EXCEPTION:
12567 case INTR_TYPE_SOFT_INTR:
12568 case INTR_TYPE_PRIV_SW_EXCEPTION:
12569 if ((vmcs12->vm_entry_instruction_len > 15) ||
12570 (vmcs12->vm_entry_instruction_len == 0 &&
12571 !nested_cpu_has_zero_length_injection(vcpu)))
12572 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12573 }
12574 }
12575
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012576 if (nested_cpu_has_ept(vmcs12) &&
12577 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12578 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12579
Jim Mattsonca0bde22016-11-30 12:03:46 -080012580 return 0;
12581}
12582
Liran Alonf145d902018-06-23 02:35:07 +030012583static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12584 struct vmcs12 *vmcs12)
12585{
12586 int r;
12587 struct page *page;
12588 struct vmcs12 *shadow;
12589
12590 if (vmcs12->vmcs_link_pointer == -1ull)
12591 return 0;
12592
12593 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12594 return -EINVAL;
12595
12596 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12597 if (is_error_page(page))
12598 return -EINVAL;
12599
12600 r = 0;
12601 shadow = kmap(page);
12602 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12603 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12604 r = -EINVAL;
12605 kunmap(page);
12606 kvm_release_page_clean(page);
12607 return r;
12608}
12609
Jim Mattsonca0bde22016-11-30 12:03:46 -080012610static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12611 u32 *exit_qual)
12612{
12613 bool ia32e;
12614
12615 *exit_qual = ENTRY_FAIL_DEFAULT;
12616
12617 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12618 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12619 return 1;
12620
Liran Alonf145d902018-06-23 02:35:07 +030012621 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012622 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12623 return 1;
12624 }
12625
12626 /*
12627 * If the load IA32_EFER VM-entry control is 1, the following checks
12628 * are performed on the field for the IA32_EFER MSR:
12629 * - Bits reserved in the IA32_EFER MSR must be 0.
12630 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12631 * the IA-32e mode guest VM-exit control. It must also be identical
12632 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12633 * CR0.PG) is 1.
12634 */
12635 if (to_vmx(vcpu)->nested.nested_run_pending &&
12636 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12637 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12638 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12639 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12640 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12641 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12642 return 1;
12643 }
12644
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012645 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12646 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12647 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12648 return 1;
12649
Jim Mattsonca0bde22016-11-30 12:03:46 -080012650 return 0;
12651}
12652
Sean Christopherson52017602018-09-26 09:23:57 -070012653static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
12654{
12655 struct vcpu_vmx *vmx = to_vmx(vcpu);
12656 unsigned long cr3, cr4;
12657
12658 if (!nested_early_check)
12659 return 0;
12660
12661 if (vmx->msr_autoload.host.nr)
12662 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
12663 if (vmx->msr_autoload.guest.nr)
12664 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
12665
12666 preempt_disable();
12667
12668 vmx_prepare_switch_to_guest(vcpu);
12669
12670 /*
12671 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
12672 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
12673 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
12674 * there is no need to preserve other bits or save/restore the field.
12675 */
12676 vmcs_writel(GUEST_RFLAGS, 0);
12677
12678 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
12679
12680 cr3 = __get_current_cr3_fast();
12681 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
12682 vmcs_writel(HOST_CR3, cr3);
12683 vmx->loaded_vmcs->host_state.cr3 = cr3;
12684 }
12685
12686 cr4 = cr4_read_shadow();
12687 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
12688 vmcs_writel(HOST_CR4, cr4);
12689 vmx->loaded_vmcs->host_state.cr4 = cr4;
12690 }
12691
12692 vmx->__launched = vmx->loaded_vmcs->launched;
12693
12694 asm(
12695 /* Set HOST_RSP */
12696 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
12697 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
12698
12699 /* Check if vmlaunch of vmresume is needed */
12700 "cmpl $0, %c[launched](%0)\n\t"
12701 "je 1f\n\t"
12702 __ex(ASM_VMX_VMRESUME) "\n\t"
12703 "jmp 2f\n\t"
12704 "1: " __ex(ASM_VMX_VMLAUNCH) "\n\t"
12705 "jmp 2f\n\t"
12706 "2: "
12707
12708 /* Set vmx->fail accordingly */
12709 "setbe %c[fail](%0)\n\t"
12710
12711 ".pushsection .rodata\n\t"
12712 ".global vmx_early_consistency_check_return\n\t"
12713 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
12714 ".popsection"
12715 :
12716 : "c"(vmx), "d"((unsigned long)HOST_RSP),
12717 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
12718 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
12719 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
12720 : "rax", "cc", "memory"
12721 );
12722
12723 vmcs_writel(HOST_RIP, vmx_return);
12724
12725 preempt_enable();
12726
12727 if (vmx->msr_autoload.host.nr)
12728 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12729 if (vmx->msr_autoload.guest.nr)
12730 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12731
12732 if (vmx->fail) {
12733 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
12734 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12735 vmx->fail = 0;
12736 return 1;
12737 }
12738
12739 /*
12740 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
12741 */
12742 local_irq_enable();
12743 if (hw_breakpoint_active())
12744 set_debugreg(__this_cpu_read(cpu_dr7), 7);
12745
12746 /*
12747 * A non-failing VMEntry means we somehow entered guest mode with
12748 * an illegal RIP, and that's just the tip of the iceberg. There
12749 * is no telling what memory has been modified or what state has
12750 * been exposed to unknown code. Hitting this all but guarantees
12751 * a (very critical) hardware issue.
12752 */
12753 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
12754 VMX_EXIT_REASONS_FAILED_VMENTRY));
12755
12756 return 0;
12757}
12758STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
12759
Sean Christophersona633e412018-09-26 09:23:47 -070012760static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12761 struct vmcs12 *vmcs12);
12762
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012763/*
Sean Christophersona633e412018-09-26 09:23:47 -070012764 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012765 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070012766+ *
12767+ * Returns:
12768+ * 0 - success, i.e. proceed with actual VMEnter
12769+ * 1 - consistency check VMExit
12770+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012771 */
Sean Christophersona633e412018-09-26 09:23:47 -070012772static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
12773 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080012774{
12775 struct vcpu_vmx *vmx = to_vmx(vcpu);
12776 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020012777 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070012778 u32 exit_reason = EXIT_REASON_INVALID_STATE;
12779 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080012780
Paolo Bonzini7e712682018-10-03 13:44:26 +020012781 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12782 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12783 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12784 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012785
Jim Mattson858e25c2016-11-30 12:03:47 -080012786 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12787 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012788 if (kvm_mpx_supported() &&
12789 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12790 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012791
Jim Mattsonde3a0022017-11-27 17:22:25 -060012792 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012793
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012794 prepare_vmcs02_early(vmx, vmcs12);
12795
12796 if (from_vmentry) {
12797 nested_get_vmcs12_pages(vcpu);
12798
Sean Christopherson52017602018-09-26 09:23:57 -070012799 if (nested_vmx_check_vmentry_hw(vcpu)) {
12800 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12801 return -1;
12802 }
12803
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012804 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
12805 goto vmentry_fail_vmexit;
12806 }
12807
12808 enter_guest_mode(vcpu);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012809 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12810 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12811
Sean Christophersona633e412018-09-26 09:23:47 -070012812 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070012813 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080012814
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012815 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070012816 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
12817 exit_qual = nested_vmx_load_msr(vcpu,
12818 vmcs12->vm_entry_msr_load_addr,
12819 vmcs12->vm_entry_msr_load_count);
12820 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070012821 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012822 } else {
12823 /*
12824 * The MMU is not initialized to point at the right entities yet and
12825 * "get pages" would need to read data from the guest (i.e. we will
12826 * need to perform gpa to hpa translation). Request a call
12827 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12828 * have already been set at vmentry time and should not be reset.
12829 */
12830 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12831 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012832
Jim Mattson858e25c2016-11-30 12:03:47 -080012833 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012834 * If L1 had a pending IRQ/NMI until it executed
12835 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12836 * disallowed (e.g. interrupts disabled), L0 needs to
12837 * evaluate if this pending event should cause an exit from L2
12838 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12839 * intercept EXTERNAL_INTERRUPT).
12840 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012841 * Usually this would be handled by the processor noticing an
12842 * IRQ/NMI window request, or checking RVI during evaluation of
12843 * pending virtual interrupts. However, this setting was done
12844 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12845 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012846 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012847 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012848 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012849
12850 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012851 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12852 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12853 * returned as far as L1 is concerned. It will only return (and set
12854 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12855 */
12856 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012857
Sean Christophersona633e412018-09-26 09:23:47 -070012858 /*
12859 * A failed consistency check that leads to a VMExit during L1's
12860 * VMEnter to L2 is a variation of a normal VMexit, as explained in
12861 * 26.7 "VM-entry failures during or after loading guest state".
12862 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070012863vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012864 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12865 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12866 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012867
12868vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012869 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070012870
12871 if (!from_vmentry)
12872 return 1;
12873
Sean Christophersona633e412018-09-26 09:23:47 -070012874 load_vmcs12_host_state(vcpu, vmcs12);
12875 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12876 vmcs12->exit_qualification = exit_qual;
Sean Christophersona633e412018-09-26 09:23:47 -070012877 if (enable_shadow_vmcs)
12878 vmx->nested.sync_shadow_vmcs = true;
12879 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012880}
12881
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012882/*
12883 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12884 * for running an L2 nested guest.
12885 */
12886static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12887{
12888 struct vmcs12 *vmcs12;
12889 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012890 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012891 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012892
Kyle Hueyeb277562016-11-29 12:40:39 -080012893 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012894 return 1;
12895
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012896 if (vmx->nested.current_vmptr == -1ull)
12897 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080012898
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012899 vmcs12 = get_vmcs12(vcpu);
12900
Liran Alona6192d42018-06-23 02:35:04 +030012901 /*
12902 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12903 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12904 * rather than RFLAGS.ZF, and no error number is stored to the
12905 * VM-instruction error field.
12906 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012907 if (vmcs12->hdr.shadow_vmcs)
12908 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030012909
Abel Gordon012f83c2013-04-18 14:39:25 +030012910 if (enable_shadow_vmcs)
12911 copy_shadow_to_vmcs12(vmx);
12912
Nadav Har'El7c177932011-05-25 23:12:04 +030012913 /*
12914 * The nested entry process starts with enforcing various prerequisites
12915 * on vmcs12 as required by the Intel SDM, and act appropriately when
12916 * they fail: As the SDM explains, some conditions should cause the
12917 * instruction to fail, while others will cause the instruction to seem
12918 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12919 * To speed up the normal (success) code path, we should avoid checking
12920 * for misconfigurations which will anyway be caught by the processor
12921 * when using the merged vmcs02.
12922 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012923 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
12924 return nested_vmx_failValid(vcpu,
12925 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012926
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012927 if (vmcs12->launch_state == launch)
12928 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030012929 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12930 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030012931
Jim Mattsonca0bde22016-11-30 12:03:46 -080012932 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012933 if (ret)
12934 return nested_vmx_failValid(vcpu, ret);
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012935
Nadav Har'El7c177932011-05-25 23:12:04 +030012936 /*
12937 * We're finally done with prerequisite checking, and can start with
12938 * the nested entry.
12939 */
Jim Mattson6514dc32018-04-26 16:09:12 -070012940 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070012941 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070012942 vmx->nested.nested_run_pending = !ret;
12943 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012944 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070012945 else if (ret)
12946 return nested_vmx_failValid(vcpu,
12947 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030012948
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012949 /* Hide L1D cache contents from the nested guest. */
12950 vmx->vcpu.arch.l1tf_flush_l1d = true;
12951
Chao Gao135a06c2018-02-11 10:06:30 +080012952 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070012953 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030012954 * also be used as part of restoring nVMX state for
12955 * snapshot restore (migration).
12956 *
12957 * In this flow, it is assumed that vmcs12 cache was
12958 * trasferred as part of captured nVMX state and should
12959 * therefore not be read from guest memory (which may not
12960 * exist on destination host yet).
12961 */
12962 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12963
12964 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012965 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12966 * by event injection, halt vcpu.
12967 */
12968 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012969 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12970 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012971 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012972 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012973 return 1;
12974}
12975
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012976/*
12977 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12978 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12979 * This function returns the new value we should put in vmcs12.guest_cr0.
12980 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12981 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12982 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12983 * didn't trap the bit, because if L1 did, so would L0).
12984 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12985 * been modified by L2, and L1 knows it. So just leave the old value of
12986 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12987 * isn't relevant, because if L0 traps this bit it can set it to anything.
12988 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12989 * changed these bits, and therefore they need to be updated, but L0
12990 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12991 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12992 */
12993static inline unsigned long
12994vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12995{
12996 return
12997 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12998 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12999 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
13000 vcpu->arch.cr0_guest_owned_bits));
13001}
13002
13003static inline unsigned long
13004vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13005{
13006 return
13007 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
13008 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
13009 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13010 vcpu->arch.cr4_guest_owned_bits));
13011}
13012
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013013static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13014 struct vmcs12 *vmcs12)
13015{
13016 u32 idt_vectoring;
13017 unsigned int nr;
13018
Wanpeng Li664f8e22017-08-24 03:35:09 -070013019 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013020 nr = vcpu->arch.exception.nr;
13021 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13022
13023 if (kvm_exception_is_soft(nr)) {
13024 vmcs12->vm_exit_instruction_len =
13025 vcpu->arch.event_exit_inst_len;
13026 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13027 } else
13028 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13029
13030 if (vcpu->arch.exception.has_error_code) {
13031 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13032 vmcs12->idt_vectoring_error_code =
13033 vcpu->arch.exception.error_code;
13034 }
13035
13036 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010013037 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013038 vmcs12->idt_vectoring_info_field =
13039 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030013040 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013041 nr = vcpu->arch.interrupt.nr;
13042 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13043
13044 if (vcpu->arch.interrupt.soft) {
13045 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13046 vmcs12->vm_entry_instruction_len =
13047 vcpu->arch.event_exit_inst_len;
13048 } else
13049 idt_vectoring |= INTR_TYPE_EXT_INTR;
13050
13051 vmcs12->idt_vectoring_info_field = idt_vectoring;
13052 }
13053}
13054
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013055static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
13056{
13057 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013058 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020013059 bool block_nested_events =
13060 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080013061
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013062 if (vcpu->arch.exception.pending &&
13063 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020013064 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013065 return -EBUSY;
13066 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013067 return 0;
13068 }
13069
Jan Kiszkaf4124502014-03-07 20:03:13 +010013070 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13071 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013072 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010013073 return -EBUSY;
13074 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13075 return 0;
13076 }
13077
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013078 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013079 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013080 return -EBUSY;
13081 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13082 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13083 INTR_INFO_VALID_MASK, 0);
13084 /*
13085 * The NMI-triggered VM exit counts as injection:
13086 * clear this one and block further NMIs.
13087 */
13088 vcpu->arch.nmi_pending = 0;
13089 vmx_set_nmi_mask(vcpu, true);
13090 return 0;
13091 }
13092
13093 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13094 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013095 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013096 return -EBUSY;
13097 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013098 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013099 }
13100
David Hildenbrand6342c502017-01-25 11:58:58 +010013101 vmx_complete_nested_posted_interrupt(vcpu);
13102 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013103}
13104
Sean Christophersond264ee02018-08-27 15:21:12 -070013105static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13106{
13107 to_vmx(vcpu)->req_immediate_exit = true;
13108}
13109
Jan Kiszkaf4124502014-03-07 20:03:13 +010013110static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13111{
13112 ktime_t remaining =
13113 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13114 u64 value;
13115
13116 if (ktime_to_ns(remaining) <= 0)
13117 return 0;
13118
13119 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13120 do_div(value, 1000000);
13121 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13122}
13123
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013124/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013125 * Update the guest state fields of vmcs12 to reflect changes that
13126 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13127 * VM-entry controls is also updated, since this is really a guest
13128 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013129 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013130static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013131{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013132 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13133 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13134
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013135 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13136 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13137 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13138
13139 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13140 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13141 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13142 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13143 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13144 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13145 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13146 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13147 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13148 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13149 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13150 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13151 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13152 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13153 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13154 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13155 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13156 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13157 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13158 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13159 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13160 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13161 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13162 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13163 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13164 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13165 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13166 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13167 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13168 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13169 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13170 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13171 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13172 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13173 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13174 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13175
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013176 vmcs12->guest_interruptibility_info =
13177 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13178 vmcs12->guest_pending_dbg_exceptions =
13179 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013180 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13181 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13182 else
13183 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013184
Jan Kiszkaf4124502014-03-07 20:03:13 +010013185 if (nested_cpu_has_preemption_timer(vmcs12)) {
13186 if (vmcs12->vm_exit_controls &
13187 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13188 vmcs12->vmx_preemption_timer_value =
13189 vmx_get_preemption_timer_value(vcpu);
13190 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13191 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013192
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013193 /*
13194 * In some cases (usually, nested EPT), L2 is allowed to change its
13195 * own CR3 without exiting. If it has changed it, we must keep it.
13196 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13197 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13198 *
13199 * Additionally, restore L2's PDPTR to vmcs12.
13200 */
13201 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013202 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013203 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13204 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13205 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13206 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13207 }
13208
Jim Mattsond281e132017-06-01 12:44:46 -070013209 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013210
Wincy Van608406e2015-02-03 23:57:51 +080013211 if (nested_cpu_has_vid(vmcs12))
13212 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13213
Jan Kiszkac18911a2013-03-13 16:06:41 +010013214 vmcs12->vm_entry_controls =
13215 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013216 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013217
Jan Kiszka2996fca2014-06-16 13:59:43 +020013218 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13219 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13220 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13221 }
13222
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013223 /* TODO: These cannot have changed unless we have MSR bitmaps and
13224 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013225 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013226 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013227 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13228 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013229 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13230 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13231 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013232 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013233 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013234}
13235
13236/*
13237 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13238 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13239 * and this function updates it to reflect the changes to the guest state while
13240 * L2 was running (and perhaps made some exits which were handled directly by L0
13241 * without going back to L1), and to reflect the exit reason.
13242 * Note that we do not have to copy here all VMCS fields, just those that
13243 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13244 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13245 * which already writes to vmcs12 directly.
13246 */
13247static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13248 u32 exit_reason, u32 exit_intr_info,
13249 unsigned long exit_qualification)
13250{
13251 /* update guest state fields: */
13252 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013253
13254 /* update exit information fields: */
13255
Jan Kiszka533558b2014-01-04 18:47:20 +010013256 vmcs12->vm_exit_reason = exit_reason;
13257 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013258 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013259
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013260 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013261 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13262 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13263
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013264 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013265 vmcs12->launch_state = 1;
13266
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013267 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13268 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013269 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013270
13271 /*
13272 * Transfer the event that L0 or L1 may wanted to inject into
13273 * L2 to IDT_VECTORING_INFO_FIELD.
13274 */
13275 vmcs12_save_pending_event(vcpu, vmcs12);
13276 }
13277
13278 /*
13279 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13280 * preserved above and would only end up incorrectly in L1.
13281 */
13282 vcpu->arch.nmi_injected = false;
13283 kvm_clear_exception_queue(vcpu);
13284 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013285}
13286
13287/*
13288 * A part of what we need to when the nested L2 guest exits and we want to
13289 * run its L1 parent, is to reset L1's guest state to the host state specified
13290 * in vmcs12.
13291 * This function is to be called not only on normal nested exit, but also on
13292 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13293 * Failures During or After Loading Guest State").
13294 * This function should be called when the active VMCS is L1's (vmcs01).
13295 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013296static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13297 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013298{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013299 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013300 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013301
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013302 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13303 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013304 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013305 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13306 else
13307 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13308 vmx_set_efer(vcpu, vcpu->arch.efer);
13309
13310 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13311 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013312 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013313 vmx_set_interrupt_shadow(vcpu, 0);
13314
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013315 /*
13316 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013317 * actually changed, because vmx_set_cr0 refers to efer set above.
13318 *
13319 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13320 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013321 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013322 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013323 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013324
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013325 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013326 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013327 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013328
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013329 nested_ept_uninit_mmu_context(vcpu);
13330
13331 /*
13332 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13333 * couldn't have changed.
13334 */
13335 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13336 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13337
13338 if (!enable_ept)
13339 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013340
Liran Alon6f1e03b2018-05-22 17:16:14 +030013341 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013342 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013343 * VMEntry/VMExit. Thus, no need to flush TLB.
13344 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013345 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13346 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013347 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013348 * Otherwise, we can preserve TLB entries as long as we are
13349 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013350 *
13351 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13352 * and therefore we request the TLB flush to happen only after VMCS EPTP
13353 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013354 */
13355 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013356 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013357 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013358 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013359
13360 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13361 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13362 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13363 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13364 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013365 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13366 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013367
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013368 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13369 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13370 vmcs_write64(GUEST_BNDCFGS, 0);
13371
Jan Kiszka44811c02013-08-04 17:17:27 +020013372 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013373 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013374 vcpu->arch.pat = vmcs12->host_ia32_pat;
13375 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013376 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13377 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13378 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013379
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013380 /* Set L1 segment info according to Intel SDM
13381 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13382 seg = (struct kvm_segment) {
13383 .base = 0,
13384 .limit = 0xFFFFFFFF,
13385 .selector = vmcs12->host_cs_selector,
13386 .type = 11,
13387 .present = 1,
13388 .s = 1,
13389 .g = 1
13390 };
13391 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13392 seg.l = 1;
13393 else
13394 seg.db = 1;
13395 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13396 seg = (struct kvm_segment) {
13397 .base = 0,
13398 .limit = 0xFFFFFFFF,
13399 .type = 3,
13400 .present = 1,
13401 .s = 1,
13402 .db = 1,
13403 .g = 1
13404 };
13405 seg.selector = vmcs12->host_ds_selector;
13406 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13407 seg.selector = vmcs12->host_es_selector;
13408 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13409 seg.selector = vmcs12->host_ss_selector;
13410 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13411 seg.selector = vmcs12->host_fs_selector;
13412 seg.base = vmcs12->host_fs_base;
13413 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13414 seg.selector = vmcs12->host_gs_selector;
13415 seg.base = vmcs12->host_gs_base;
13416 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13417 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013418 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013419 .limit = 0x67,
13420 .selector = vmcs12->host_tr_selector,
13421 .type = 11,
13422 .present = 1
13423 };
13424 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13425
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013426 kvm_set_dr(vcpu, 7, 0x400);
13427 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013428
Wincy Van3af18d92015-02-03 23:49:31 +080013429 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013430 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013431
Wincy Vanff651cb2014-12-11 08:52:58 +030013432 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13433 vmcs12->vm_exit_msr_load_count))
13434 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013435}
13436
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013437static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13438{
13439 struct shared_msr_entry *efer_msr;
13440 unsigned int i;
13441
13442 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13443 return vmcs_read64(GUEST_IA32_EFER);
13444
13445 if (cpu_has_load_ia32_efer)
13446 return host_efer;
13447
13448 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13449 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13450 return vmx->msr_autoload.guest.val[i].value;
13451 }
13452
13453 efer_msr = find_msr_entry(vmx, MSR_EFER);
13454 if (efer_msr)
13455 return efer_msr->data;
13456
13457 return host_efer;
13458}
13459
13460static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13461{
13462 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13463 struct vcpu_vmx *vmx = to_vmx(vcpu);
13464 struct vmx_msr_entry g, h;
13465 struct msr_data msr;
13466 gpa_t gpa;
13467 u32 i, j;
13468
13469 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13470
13471 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13472 /*
13473 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13474 * as vmcs01.GUEST_DR7 contains a userspace defined value
13475 * and vcpu->arch.dr7 is not squirreled away before the
13476 * nested VMENTER (not worth adding a variable in nested_vmx).
13477 */
13478 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13479 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13480 else
13481 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13482 }
13483
13484 /*
13485 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13486 * handle a variety of side effects to KVM's software model.
13487 */
13488 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13489
13490 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13491 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13492
13493 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13494 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13495
13496 nested_ept_uninit_mmu_context(vcpu);
13497 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13498 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13499
13500 /*
13501 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13502 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13503 * VMFail, like everything else we just need to ensure our
13504 * software model is up-to-date.
13505 */
13506 ept_save_pdptrs(vcpu);
13507
13508 kvm_mmu_reset_context(vcpu);
13509
13510 if (cpu_has_vmx_msr_bitmap())
13511 vmx_update_msr_bitmap(vcpu);
13512
13513 /*
13514 * This nasty bit of open coding is a compromise between blindly
13515 * loading L1's MSRs using the exit load lists (incorrect emulation
13516 * of VMFail), leaving the nested VM's MSRs in the software model
13517 * (incorrect behavior) and snapshotting the modified MSRs (too
13518 * expensive since the lists are unbound by hardware). For each
13519 * MSR that was (prematurely) loaded from the nested VMEntry load
13520 * list, reload it from the exit load list if it exists and differs
13521 * from the guest value. The intent is to stuff host state as
13522 * silently as possible, not to fully process the exit load list.
13523 */
13524 msr.host_initiated = false;
13525 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13526 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13527 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13528 pr_debug_ratelimited(
13529 "%s read MSR index failed (%u, 0x%08llx)\n",
13530 __func__, i, gpa);
13531 goto vmabort;
13532 }
13533
13534 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13535 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13536 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13537 pr_debug_ratelimited(
13538 "%s read MSR failed (%u, 0x%08llx)\n",
13539 __func__, j, gpa);
13540 goto vmabort;
13541 }
13542 if (h.index != g.index)
13543 continue;
13544 if (h.value == g.value)
13545 break;
13546
13547 if (nested_vmx_load_msr_check(vcpu, &h)) {
13548 pr_debug_ratelimited(
13549 "%s check failed (%u, 0x%x, 0x%x)\n",
13550 __func__, j, h.index, h.reserved);
13551 goto vmabort;
13552 }
13553
13554 msr.index = h.index;
13555 msr.data = h.value;
13556 if (kvm_set_msr(vcpu, &msr)) {
13557 pr_debug_ratelimited(
13558 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13559 __func__, j, h.index, h.value);
13560 goto vmabort;
13561 }
13562 }
13563 }
13564
13565 return;
13566
13567vmabort:
13568 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13569}
13570
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013571/*
13572 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13573 * and modify vmcs12 to make it see what it would expect to see there if
13574 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13575 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013576static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13577 u32 exit_intr_info,
13578 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013579{
13580 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013581 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13582
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013583 /* trying to cancel vmlaunch/vmresume is a bug */
13584 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13585
Jim Mattson4f350c62017-09-14 16:31:44 -070013586 leave_guest_mode(vcpu);
13587
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013588 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13589 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13590
Jim Mattson4f350c62017-09-14 16:31:44 -070013591 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013592 if (exit_reason == -1)
13593 sync_vmcs12(vcpu, vmcs12);
13594 else
13595 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13596 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013597
Liran Alon61ada742018-06-23 02:35:08 +030013598 /*
13599 * Must happen outside of sync_vmcs12() as it will
13600 * also be used to capture vmcs12 cache as part of
13601 * capturing nVMX state for snapshot (migration).
13602 *
13603 * Otherwise, this flush will dirty guest memory at a
13604 * point it is already assumed by user-space to be
13605 * immutable.
13606 */
13607 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13608
Jim Mattson4f350c62017-09-14 16:31:44 -070013609 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13610 vmcs12->vm_exit_msr_store_count))
13611 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070013612 } else {
13613 /*
13614 * The only expected VM-instruction error is "VM entry with
13615 * invalid control field(s)." Anything else indicates a
13616 * problem with L0. And we should never get here with a
13617 * VMFail of any type if early consistency checks are enabled.
13618 */
13619 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
13620 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13621 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013622 }
13623
Jim Mattson4f350c62017-09-14 16:31:44 -070013624 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013625
Paolo Bonzini93140062016-07-06 13:23:51 +020013626 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013627 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13628 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013629 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013630
Peter Feinerc95ba922016-08-17 09:36:47 -070013631 if (kvm_has_tsc_control)
13632 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013633
Jim Mattson8d860bb2018-05-09 16:56:05 -040013634 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13635 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13636 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013637 } else if (!nested_cpu_has_ept(vmcs12) &&
13638 nested_cpu_has2(vmcs12,
13639 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013640 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013641 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013642
13643 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13644 vmx->host_rsp = 0;
13645
13646 /* Unpin physical memory we referred to in vmcs02 */
13647 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013648 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013649 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013650 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013651 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013652 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013653 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013654 }
Wincy Van705699a2015-02-03 23:58:17 +080013655 if (vmx->nested.pi_desc_page) {
13656 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013657 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013658 vmx->nested.pi_desc_page = NULL;
13659 vmx->nested.pi_desc = NULL;
13660 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013661
13662 /*
Tang Chen38b99172014-09-24 15:57:54 +080013663 * We are now running in L2, mmu_notifier will force to reload the
13664 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13665 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013666 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013667
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013668 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013669 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013670
13671 /* in case we halted in L2 */
13672 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013673
13674 if (likely(!vmx->fail)) {
13675 /*
13676 * TODO: SDM says that with acknowledge interrupt on
13677 * exit, bit 31 of the VM-exit interrupt information
13678 * (valid interrupt) is always set to 1 on
13679 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13680 * need kvm_cpu_has_interrupt(). See the commit
13681 * message for details.
13682 */
13683 if (nested_exit_intr_ack_set(vcpu) &&
13684 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13685 kvm_cpu_has_interrupt(vcpu)) {
13686 int irq = kvm_cpu_get_interrupt(vcpu);
13687 WARN_ON(irq < 0);
13688 vmcs12->vm_exit_intr_info = irq |
13689 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13690 }
13691
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013692 if (exit_reason != -1)
13693 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13694 vmcs12->exit_qualification,
13695 vmcs12->idt_vectoring_info_field,
13696 vmcs12->vm_exit_intr_info,
13697 vmcs12->vm_exit_intr_error_code,
13698 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013699
13700 load_vmcs12_host_state(vcpu, vmcs12);
13701
13702 return;
13703 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013704
Jim Mattson4f350c62017-09-14 16:31:44 -070013705 /*
13706 * After an early L2 VM-entry failure, we're now back
13707 * in L1 which thinks it just finished a VMLAUNCH or
13708 * VMRESUME instruction, so we need to set the failure
13709 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070013710 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070013711 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013712 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013713
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013714 /*
13715 * Restore L1's host state to KVM's software model. We're here
13716 * because a consistency check was caught by hardware, which
13717 * means some amount of guest state has been propagated to KVM's
13718 * model and needs to be unwound to the host's state.
13719 */
13720 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013721
Jim Mattson4f350c62017-09-14 16:31:44 -070013722 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013723}
13724
Nadav Har'El7c177932011-05-25 23:12:04 +030013725/*
Jan Kiszka42124922014-01-04 18:47:19 +010013726 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13727 */
13728static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13729{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013730 if (is_guest_mode(vcpu)) {
13731 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013732 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013733 }
Jan Kiszka42124922014-01-04 18:47:19 +010013734 free_nested(to_vmx(vcpu));
13735}
13736
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013737static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13738 struct x86_instruction_info *info,
13739 enum x86_intercept_stage stage)
13740{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013741 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13742 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13743
13744 /*
13745 * RDPID causes #UD if disabled through secondary execution controls.
13746 * Because it is marked as EmulateOnUD, we need to intercept it here.
13747 */
13748 if (info->intercept == x86_intercept_rdtscp &&
13749 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13750 ctxt->exception.vector = UD_VECTOR;
13751 ctxt->exception.error_code_valid = false;
13752 return X86EMUL_PROPAGATE_FAULT;
13753 }
13754
13755 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013756 return X86EMUL_CONTINUE;
13757}
13758
Yunhong Jiang64672c92016-06-13 14:19:59 -070013759#ifdef CONFIG_X86_64
13760/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13761static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13762 u64 divisor, u64 *result)
13763{
13764 u64 low = a << shift, high = a >> (64 - shift);
13765
13766 /* To avoid the overflow on divq */
13767 if (high >= divisor)
13768 return 1;
13769
13770 /* Low hold the result, high hold rem which is discarded */
13771 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13772 "rm" (divisor), "0" (low), "1" (high));
13773 *result = low;
13774
13775 return 0;
13776}
13777
13778static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13779{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013780 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013781 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013782
13783 if (kvm_mwait_in_guest(vcpu->kvm))
13784 return -EOPNOTSUPP;
13785
13786 vmx = to_vmx(vcpu);
13787 tscl = rdtsc();
13788 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13789 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013790 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13791
13792 if (delta_tsc > lapic_timer_advance_cycles)
13793 delta_tsc -= lapic_timer_advance_cycles;
13794 else
13795 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013796
13797 /* Convert to host delta tsc if tsc scaling is enabled */
13798 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13799 u64_shl_div_u64(delta_tsc,
13800 kvm_tsc_scaling_ratio_frac_bits,
13801 vcpu->arch.tsc_scaling_ratio,
13802 &delta_tsc))
13803 return -ERANGE;
13804
13805 /*
13806 * If the delta tsc can't fit in the 32 bit after the multi shift,
13807 * we can't use the preemption timer.
13808 * It's possible that it fits on later vmentries, but checking
13809 * on every vmentry is costly so we just use an hrtimer.
13810 */
13811 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13812 return -ERANGE;
13813
13814 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013815 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013816}
13817
13818static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13819{
Sean Christophersonf459a702018-08-27 15:21:11 -070013820 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013821}
13822#endif
13823
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013824static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013825{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013826 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013827 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013828}
13829
Kai Huang843e4332015-01-28 10:54:28 +080013830static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13831 struct kvm_memory_slot *slot)
13832{
13833 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13834 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13835}
13836
13837static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13838 struct kvm_memory_slot *slot)
13839{
13840 kvm_mmu_slot_set_dirty(kvm, slot);
13841}
13842
13843static void vmx_flush_log_dirty(struct kvm *kvm)
13844{
13845 kvm_flush_pml_buffers(kvm);
13846}
13847
Bandan Dasc5f983f2017-05-05 15:25:14 -040013848static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13849{
13850 struct vmcs12 *vmcs12;
13851 struct vcpu_vmx *vmx = to_vmx(vcpu);
13852 gpa_t gpa;
13853 struct page *page = NULL;
13854 u64 *pml_address;
13855
13856 if (is_guest_mode(vcpu)) {
13857 WARN_ON_ONCE(vmx->nested.pml_full);
13858
13859 /*
13860 * Check if PML is enabled for the nested guest.
13861 * Whether eptp bit 6 is set is already checked
13862 * as part of A/D emulation.
13863 */
13864 vmcs12 = get_vmcs12(vcpu);
13865 if (!nested_cpu_has_pml(vmcs12))
13866 return 0;
13867
Dan Carpenter47698862017-05-10 22:43:17 +030013868 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013869 vmx->nested.pml_full = true;
13870 return 1;
13871 }
13872
13873 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13874
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013875 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13876 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013877 return 0;
13878
13879 pml_address = kmap(page);
13880 pml_address[vmcs12->guest_pml_index--] = gpa;
13881 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013882 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013883 }
13884
13885 return 0;
13886}
13887
Kai Huang843e4332015-01-28 10:54:28 +080013888static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13889 struct kvm_memory_slot *memslot,
13890 gfn_t offset, unsigned long mask)
13891{
13892 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13893}
13894
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013895static void __pi_post_block(struct kvm_vcpu *vcpu)
13896{
13897 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13898 struct pi_desc old, new;
13899 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013900
13901 do {
13902 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013903 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13904 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013905
13906 dest = cpu_physical_id(vcpu->cpu);
13907
13908 if (x2apic_enabled())
13909 new.ndst = dest;
13910 else
13911 new.ndst = (dest << 8) & 0xFF00;
13912
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013913 /* set 'NV' to 'notification vector' */
13914 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013915 } while (cmpxchg64(&pi_desc->control, old.control,
13916 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013917
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013918 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13919 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013920 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013921 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013922 vcpu->pre_pcpu = -1;
13923 }
13924}
13925
Feng Wuefc64402015-09-18 22:29:51 +080013926/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013927 * This routine does the following things for vCPU which is going
13928 * to be blocked if VT-d PI is enabled.
13929 * - Store the vCPU to the wakeup list, so when interrupts happen
13930 * we can find the right vCPU to wake up.
13931 * - Change the Posted-interrupt descriptor as below:
13932 * 'NDST' <-- vcpu->pre_pcpu
13933 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13934 * - If 'ON' is set during this process, which means at least one
13935 * interrupt is posted for this vCPU, we cannot block it, in
13936 * this case, return 1, otherwise, return 0.
13937 *
13938 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013939static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013940{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013941 unsigned int dest;
13942 struct pi_desc old, new;
13943 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13944
13945 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013946 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13947 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013948 return 0;
13949
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013950 WARN_ON(irqs_disabled());
13951 local_irq_disable();
13952 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13953 vcpu->pre_pcpu = vcpu->cpu;
13954 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13955 list_add_tail(&vcpu->blocked_vcpu_list,
13956 &per_cpu(blocked_vcpu_on_cpu,
13957 vcpu->pre_pcpu));
13958 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13959 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013960
13961 do {
13962 old.control = new.control = pi_desc->control;
13963
Feng Wubf9f6ac2015-09-18 22:29:55 +080013964 WARN((pi_desc->sn == 1),
13965 "Warning: SN field of posted-interrupts "
13966 "is set before blocking\n");
13967
13968 /*
13969 * Since vCPU can be preempted during this process,
13970 * vcpu->cpu could be different with pre_pcpu, we
13971 * need to set pre_pcpu as the destination of wakeup
13972 * notification event, then we can find the right vCPU
13973 * to wakeup in wakeup handler if interrupts happen
13974 * when the vCPU is in blocked state.
13975 */
13976 dest = cpu_physical_id(vcpu->pre_pcpu);
13977
13978 if (x2apic_enabled())
13979 new.ndst = dest;
13980 else
13981 new.ndst = (dest << 8) & 0xFF00;
13982
13983 /* set 'NV' to 'wakeup vector' */
13984 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013985 } while (cmpxchg64(&pi_desc->control, old.control,
13986 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013987
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013988 /* We should not block the vCPU if an interrupt is posted for it. */
13989 if (pi_test_on(pi_desc) == 1)
13990 __pi_post_block(vcpu);
13991
13992 local_irq_enable();
13993 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013994}
13995
Yunhong Jiangbc225122016-06-13 14:19:58 -070013996static int vmx_pre_block(struct kvm_vcpu *vcpu)
13997{
13998 if (pi_pre_block(vcpu))
13999 return 1;
14000
Yunhong Jiang64672c92016-06-13 14:19:59 -070014001 if (kvm_lapic_hv_timer_in_use(vcpu))
14002 kvm_lapic_switch_to_sw_timer(vcpu);
14003
Yunhong Jiangbc225122016-06-13 14:19:58 -070014004 return 0;
14005}
14006
14007static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014008{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014009 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014010 return;
14011
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014012 WARN_ON(irqs_disabled());
14013 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014014 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014015 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080014016}
14017
Yunhong Jiangbc225122016-06-13 14:19:58 -070014018static void vmx_post_block(struct kvm_vcpu *vcpu)
14019{
Yunhong Jiang64672c92016-06-13 14:19:59 -070014020 if (kvm_x86_ops->set_hv_timer)
14021 kvm_lapic_switch_to_hv_timer(vcpu);
14022
Yunhong Jiangbc225122016-06-13 14:19:58 -070014023 pi_post_block(vcpu);
14024}
14025
Feng Wubf9f6ac2015-09-18 22:29:55 +080014026/*
Feng Wuefc64402015-09-18 22:29:51 +080014027 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14028 *
14029 * @kvm: kvm
14030 * @host_irq: host irq of the interrupt
14031 * @guest_irq: gsi of the interrupt
14032 * @set: set or unset PI
14033 * returns 0 on success, < 0 on failure
14034 */
14035static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14036 uint32_t guest_irq, bool set)
14037{
14038 struct kvm_kernel_irq_routing_entry *e;
14039 struct kvm_irq_routing_table *irq_rt;
14040 struct kvm_lapic_irq irq;
14041 struct kvm_vcpu *vcpu;
14042 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014043 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080014044
14045 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014046 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14047 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080014048 return 0;
14049
14050 idx = srcu_read_lock(&kvm->irq_srcu);
14051 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014052 if (guest_irq >= irq_rt->nr_rt_entries ||
14053 hlist_empty(&irq_rt->map[guest_irq])) {
14054 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14055 guest_irq, irq_rt->nr_rt_entries);
14056 goto out;
14057 }
Feng Wuefc64402015-09-18 22:29:51 +080014058
14059 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14060 if (e->type != KVM_IRQ_ROUTING_MSI)
14061 continue;
14062 /*
14063 * VT-d PI cannot support posting multicast/broadcast
14064 * interrupts to a vCPU, we still use interrupt remapping
14065 * for these kind of interrupts.
14066 *
14067 * For lowest-priority interrupts, we only support
14068 * those with single CPU as the destination, e.g. user
14069 * configures the interrupts via /proc/irq or uses
14070 * irqbalance to make the interrupts single-CPU.
14071 *
14072 * We will support full lowest-priority interrupt later.
14073 */
14074
Radim Krčmář371313132016-07-12 22:09:27 +020014075 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014076 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14077 /*
14078 * Make sure the IRTE is in remapped mode if
14079 * we don't handle it in posted mode.
14080 */
14081 ret = irq_set_vcpu_affinity(host_irq, NULL);
14082 if (ret < 0) {
14083 printk(KERN_INFO
14084 "failed to back to remapped mode, irq: %u\n",
14085 host_irq);
14086 goto out;
14087 }
14088
Feng Wuefc64402015-09-18 22:29:51 +080014089 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014090 }
Feng Wuefc64402015-09-18 22:29:51 +080014091
14092 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14093 vcpu_info.vector = irq.vector;
14094
hu huajun2698d822018-04-11 15:16:40 +080014095 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014096 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14097
14098 if (set)
14099 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014100 else
Feng Wuefc64402015-09-18 22:29:51 +080014101 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014102
14103 if (ret < 0) {
14104 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14105 __func__);
14106 goto out;
14107 }
14108 }
14109
14110 ret = 0;
14111out:
14112 srcu_read_unlock(&kvm->irq_srcu, idx);
14113 return ret;
14114}
14115
Ashok Rajc45dcc72016-06-22 14:59:56 +080014116static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14117{
14118 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14119 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14120 FEATURE_CONTROL_LMCE;
14121 else
14122 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14123 ~FEATURE_CONTROL_LMCE;
14124}
14125
Ladi Prosek72d7b372017-10-11 16:54:41 +020014126static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14127{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014128 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14129 if (to_vmx(vcpu)->nested.nested_run_pending)
14130 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014131 return 1;
14132}
14133
Ladi Prosek0234bf82017-10-11 16:54:40 +020014134static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14135{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014136 struct vcpu_vmx *vmx = to_vmx(vcpu);
14137
14138 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14139 if (vmx->nested.smm.guest_mode)
14140 nested_vmx_vmexit(vcpu, -1, 0, 0);
14141
14142 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14143 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014144 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014145 return 0;
14146}
14147
14148static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14149{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014150 struct vcpu_vmx *vmx = to_vmx(vcpu);
14151 int ret;
14152
14153 if (vmx->nested.smm.vmxon) {
14154 vmx->nested.vmxon = true;
14155 vmx->nested.smm.vmxon = false;
14156 }
14157
14158 if (vmx->nested.smm.guest_mode) {
14159 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014160 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014161 vcpu->arch.hflags |= HF_SMM_MASK;
14162 if (ret)
14163 return ret;
14164
14165 vmx->nested.smm.guest_mode = false;
14166 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014167 return 0;
14168}
14169
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014170static int enable_smi_window(struct kvm_vcpu *vcpu)
14171{
14172 return 0;
14173}
14174
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014175static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14176 struct kvm_nested_state __user *user_kvm_nested_state,
14177 u32 user_data_size)
14178{
14179 struct vcpu_vmx *vmx;
14180 struct vmcs12 *vmcs12;
14181 struct kvm_nested_state kvm_state = {
14182 .flags = 0,
14183 .format = 0,
14184 .size = sizeof(kvm_state),
14185 .vmx.vmxon_pa = -1ull,
14186 .vmx.vmcs_pa = -1ull,
14187 };
14188
14189 if (!vcpu)
14190 return kvm_state.size + 2 * VMCS12_SIZE;
14191
14192 vmx = to_vmx(vcpu);
14193 vmcs12 = get_vmcs12(vcpu);
14194 if (nested_vmx_allowed(vcpu) &&
14195 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14196 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14197 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14198
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014199 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014200 kvm_state.size += VMCS12_SIZE;
14201
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014202 if (is_guest_mode(vcpu) &&
14203 nested_cpu_has_shadow_vmcs(vmcs12) &&
14204 vmcs12->vmcs_link_pointer != -1ull)
14205 kvm_state.size += VMCS12_SIZE;
14206 }
14207
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014208 if (vmx->nested.smm.vmxon)
14209 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14210
14211 if (vmx->nested.smm.guest_mode)
14212 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14213
14214 if (is_guest_mode(vcpu)) {
14215 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14216
14217 if (vmx->nested.nested_run_pending)
14218 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14219 }
14220 }
14221
14222 if (user_data_size < kvm_state.size)
14223 goto out;
14224
14225 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14226 return -EFAULT;
14227
14228 if (vmx->nested.current_vmptr == -1ull)
14229 goto out;
14230
14231 /*
14232 * When running L2, the authoritative vmcs12 state is in the
14233 * vmcs02. When running L1, the authoritative vmcs12 state is
14234 * in the shadow vmcs linked to vmcs01, unless
14235 * sync_shadow_vmcs is set, in which case, the authoritative
14236 * vmcs12 state is in the vmcs12 already.
14237 */
14238 if (is_guest_mode(vcpu))
14239 sync_vmcs12(vcpu, vmcs12);
14240 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14241 copy_shadow_to_vmcs12(vmx);
14242
14243 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14244 return -EFAULT;
14245
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014246 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14247 vmcs12->vmcs_link_pointer != -1ull) {
14248 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14249 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14250 return -EFAULT;
14251 }
14252
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014253out:
14254 return kvm_state.size;
14255}
14256
14257static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14258 struct kvm_nested_state __user *user_kvm_nested_state,
14259 struct kvm_nested_state *kvm_state)
14260{
14261 struct vcpu_vmx *vmx = to_vmx(vcpu);
14262 struct vmcs12 *vmcs12;
14263 u32 exit_qual;
14264 int ret;
14265
14266 if (kvm_state->format != 0)
14267 return -EINVAL;
14268
14269 if (!nested_vmx_allowed(vcpu))
14270 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14271
14272 if (kvm_state->vmx.vmxon_pa == -1ull) {
14273 if (kvm_state->vmx.smm.flags)
14274 return -EINVAL;
14275
14276 if (kvm_state->vmx.vmcs_pa != -1ull)
14277 return -EINVAL;
14278
14279 vmx_leave_nested(vcpu);
14280 return 0;
14281 }
14282
14283 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14284 return -EINVAL;
14285
14286 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14287 return -EINVAL;
14288
14289 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14290 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14291 return -EINVAL;
14292
14293 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14294 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14295 return -EINVAL;
14296
14297 if (kvm_state->vmx.smm.flags &
14298 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14299 return -EINVAL;
14300
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014301 /*
14302 * SMM temporarily disables VMX, so we cannot be in guest mode,
14303 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14304 * must be zero.
14305 */
14306 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14307 return -EINVAL;
14308
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014309 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14310 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14311 return -EINVAL;
14312
14313 vmx_leave_nested(vcpu);
14314 if (kvm_state->vmx.vmxon_pa == -1ull)
14315 return 0;
14316
14317 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14318 ret = enter_vmx_operation(vcpu);
14319 if (ret)
14320 return ret;
14321
14322 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14323
14324 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14325 vmx->nested.smm.vmxon = true;
14326 vmx->nested.vmxon = false;
14327
14328 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14329 vmx->nested.smm.guest_mode = true;
14330 }
14331
14332 vmcs12 = get_vmcs12(vcpu);
14333 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14334 return -EFAULT;
14335
Liran Alon392b2f22018-06-23 02:35:01 +030014336 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014337 return -EINVAL;
14338
14339 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14340 return 0;
14341
14342 vmx->nested.nested_run_pending =
14343 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14344
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014345 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14346 vmcs12->vmcs_link_pointer != -1ull) {
14347 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14348 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14349 return -EINVAL;
14350
14351 if (copy_from_user(shadow_vmcs12,
14352 user_kvm_nested_state->data + VMCS12_SIZE,
14353 sizeof(*vmcs12)))
14354 return -EFAULT;
14355
14356 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14357 !shadow_vmcs12->hdr.shadow_vmcs)
14358 return -EINVAL;
14359 }
14360
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014361 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14362 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14363 return -EINVAL;
14364
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014365 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014366 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014367 if (ret)
14368 return -EINVAL;
14369
14370 return 0;
14371}
14372
Kees Cook404f6aa2016-08-08 16:29:06 -070014373static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014374 .cpu_has_kvm_support = cpu_has_kvm_support,
14375 .disabled_by_bios = vmx_disabled_by_bios,
14376 .hardware_setup = hardware_setup,
14377 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014378 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014379 .hardware_enable = hardware_enable,
14380 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014381 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014382 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014383
Wanpeng Lib31c1142018-03-12 04:53:04 -070014384 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014385 .vm_alloc = vmx_vm_alloc,
14386 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014387
Avi Kivity6aa8b732006-12-10 02:21:36 -080014388 .vcpu_create = vmx_create_vcpu,
14389 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014390 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014391
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014392 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014393 .vcpu_load = vmx_vcpu_load,
14394 .vcpu_put = vmx_vcpu_put,
14395
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014396 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014397 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014398 .get_msr = vmx_get_msr,
14399 .set_msr = vmx_set_msr,
14400 .get_segment_base = vmx_get_segment_base,
14401 .get_segment = vmx_get_segment,
14402 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014403 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014404 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014405 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014406 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014407 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014408 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014409 .set_cr3 = vmx_set_cr3,
14410 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014411 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014412 .get_idt = vmx_get_idt,
14413 .set_idt = vmx_set_idt,
14414 .get_gdt = vmx_get_gdt,
14415 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014416 .get_dr6 = vmx_get_dr6,
14417 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014418 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014419 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014420 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014421 .get_rflags = vmx_get_rflags,
14422 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014423
Avi Kivity6aa8b732006-12-10 02:21:36 -080014424 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014425 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014426
Avi Kivity6aa8b732006-12-10 02:21:36 -080014427 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014428 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014429 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014430 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14431 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014432 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014433 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014434 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014435 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014436 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014437 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014438 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014439 .get_nmi_mask = vmx_get_nmi_mask,
14440 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014441 .enable_nmi_window = enable_nmi_window,
14442 .enable_irq_window = enable_irq_window,
14443 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014444 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014445 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014446 .get_enable_apicv = vmx_get_enable_apicv,
14447 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014448 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014449 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014450 .hwapic_irr_update = vmx_hwapic_irr_update,
14451 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014452 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014453 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14454 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014455
Izik Eiduscbc94022007-10-25 00:29:55 +020014456 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014457 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014458 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014459 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014460
Avi Kivity586f9602010-11-18 13:09:54 +020014461 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014462
Sheng Yang17cc3932010-01-05 19:02:27 +080014463 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014464
14465 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014466
14467 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014468 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014469
14470 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014471
14472 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014473
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014474 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014475 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014476
14477 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014478
14479 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014480 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014481 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014482 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014483 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014484
14485 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014486 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014487
14488 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014489
14490 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14491 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14492 .flush_log_dirty = vmx_flush_log_dirty,
14493 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014494 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014495
Feng Wubf9f6ac2015-09-18 22:29:55 +080014496 .pre_block = vmx_pre_block,
14497 .post_block = vmx_post_block,
14498
Wei Huang25462f72015-06-19 15:45:05 +020014499 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014500
14501 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014502
14503#ifdef CONFIG_X86_64
14504 .set_hv_timer = vmx_set_hv_timer,
14505 .cancel_hv_timer = vmx_cancel_hv_timer,
14506#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014507
14508 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014509
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014510 .get_nested_state = vmx_get_nested_state,
14511 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014512 .get_vmcs12_pages = nested_get_vmcs12_pages,
14513
Ladi Prosek72d7b372017-10-11 16:54:41 +020014514 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014515 .pre_enter_smm = vmx_pre_enter_smm,
14516 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014517 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014518};
14519
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014520static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014521{
14522 if (vmx_l1d_flush_pages) {
14523 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14524 vmx_l1d_flush_pages = NULL;
14525 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014526 /* Restore state so sysfs ignores VMX */
14527 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014528}
14529
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014530static void vmx_exit(void)
14531{
14532#ifdef CONFIG_KEXEC_CORE
14533 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14534 synchronize_rcu();
14535#endif
14536
14537 kvm_exit();
14538
14539#if IS_ENABLED(CONFIG_HYPERV)
14540 if (static_branch_unlikely(&enable_evmcs)) {
14541 int cpu;
14542 struct hv_vp_assist_page *vp_ap;
14543 /*
14544 * Reset everything to support using non-enlightened VMCS
14545 * access later (e.g. when we reload the module with
14546 * enlightened_vmcs=0)
14547 */
14548 for_each_online_cpu(cpu) {
14549 vp_ap = hv_get_vp_assist_page(cpu);
14550
14551 if (!vp_ap)
14552 continue;
14553
14554 vp_ap->current_nested_vmcs = 0;
14555 vp_ap->enlighten_vmentry = 0;
14556 }
14557
14558 static_branch_disable(&enable_evmcs);
14559 }
14560#endif
14561 vmx_cleanup_l1d_flush();
14562}
14563module_exit(vmx_exit);
14564
Avi Kivity6aa8b732006-12-10 02:21:36 -080014565static int __init vmx_init(void)
14566{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014567 int r;
14568
14569#if IS_ENABLED(CONFIG_HYPERV)
14570 /*
14571 * Enlightened VMCS usage should be recommended and the host needs
14572 * to support eVMCS v1 or above. We can also disable eVMCS support
14573 * with module parameter.
14574 */
14575 if (enlightened_vmcs &&
14576 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14577 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14578 KVM_EVMCS_VERSION) {
14579 int cpu;
14580
14581 /* Check that we have assist pages on all online CPUs */
14582 for_each_online_cpu(cpu) {
14583 if (!hv_get_vp_assist_page(cpu)) {
14584 enlightened_vmcs = false;
14585 break;
14586 }
14587 }
14588
14589 if (enlightened_vmcs) {
14590 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14591 static_branch_enable(&enable_evmcs);
14592 }
14593 } else {
14594 enlightened_vmcs = false;
14595 }
14596#endif
14597
14598 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014599 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014600 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014601 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014602
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014603 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014604 * Must be called after kvm_init() so enable_ept is properly set
14605 * up. Hand the parameter mitigation value in which was stored in
14606 * the pre module init parser. If no parameter was given, it will
14607 * contain 'auto' which will be turned into the default 'cond'
14608 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014609 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014610 if (boot_cpu_has(X86_BUG_L1TF)) {
14611 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14612 if (r) {
14613 vmx_exit();
14614 return r;
14615 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014616 }
14617
Dave Young2965faa2015-09-09 15:38:55 -070014618#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014619 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14620 crash_vmclear_local_loaded_vmcss);
14621#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014622 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014623
He, Qingfdef3ad2007-04-30 09:45:24 +030014624 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014625}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014626module_init(vmx_init);