blob: a9ed7a723a0c4d215e919db3c33596c6b37053cb [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) \
Uros Bizjak44c2d662018-10-11 19:40:45 +020064 ____kvm_handle_fault_on_reboot(x, "xorl " 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{
Lan Tianyua5c214d2018-10-13 22:54:05 +08001574 struct kvm_vcpu *vcpu;
1575 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +00001576
1577 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1578
1579 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1580 check_ept_pointer_match(kvm);
1581
1582 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +08001583 kvm_for_each_vcpu(i, vcpu, kvm)
1584 ret |= hyperv_flush_guest_mapping(
1585 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer);
1586 } else {
1587 ret = hyperv_flush_guest_mapping(
1588 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
Tianyu Lan877ad952018-07-19 08:40:23 +00001589 }
1590
Tianyu Lan877ad952018-07-19 08:40:23 +00001591 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1592 return ret;
1593}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001594#else /* !IS_ENABLED(CONFIG_HYPERV) */
1595static inline void evmcs_write64(unsigned long field, u64 value) {}
1596static inline void evmcs_write32(unsigned long field, u32 value) {}
1597static inline void evmcs_write16(unsigned long field, u16 value) {}
1598static inline u64 evmcs_read64(unsigned long field) { return 0; }
1599static inline u32 evmcs_read32(unsigned long field) { return 0; }
1600static inline u16 evmcs_read16(unsigned long field) { return 0; }
1601static inline void evmcs_load(u64 phys_addr) {}
1602static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001603static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001604#endif /* IS_ENABLED(CONFIG_HYPERV) */
1605
Jan Kiszka5bb16012016-02-09 20:14:21 +01001606static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607{
1608 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1609 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001610 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1611}
1612
Jan Kiszka6f054852016-02-09 20:15:18 +01001613static inline bool is_debug(u32 intr_info)
1614{
1615 return is_exception_n(intr_info, DB_VECTOR);
1616}
1617
1618static inline bool is_breakpoint(u32 intr_info)
1619{
1620 return is_exception_n(intr_info, BP_VECTOR);
1621}
1622
Jan Kiszka5bb16012016-02-09 20:14:21 +01001623static inline bool is_page_fault(u32 intr_info)
1624{
1625 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626}
1627
Gui Jianfeng31299942010-03-15 17:29:09 +08001628static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001629{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001630 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001631}
1632
Liran Alon9e869482018-03-12 13:12:51 +02001633static inline bool is_gp_fault(u32 intr_info)
1634{
1635 return is_exception_n(intr_info, GP_VECTOR);
1636}
1637
Gui Jianfeng31299942010-03-15 17:29:09 +08001638static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001639{
1640 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1641 INTR_INFO_VALID_MASK)) ==
1642 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1643}
1644
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001645/* Undocumented: icebp/int1 */
1646static inline bool is_icebp(u32 intr_info)
1647{
1648 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1649 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1650}
1651
Gui Jianfeng31299942010-03-15 17:29:09 +08001652static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001653{
Sheng Yang04547152009-04-01 15:52:31 +08001654 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001655}
1656
Gui Jianfeng31299942010-03-15 17:29:09 +08001657static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001658{
Sheng Yang04547152009-04-01 15:52:31 +08001659 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001660}
1661
Paolo Bonzini35754c92015-07-29 12:05:37 +02001662static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001663{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001664 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001665}
1666
Gui Jianfeng31299942010-03-15 17:29:09 +08001667static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001668{
Sheng Yang04547152009-04-01 15:52:31 +08001669 return vmcs_config.cpu_based_exec_ctrl &
1670 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001671}
1672
Avi Kivity774ead32007-12-26 13:57:04 +02001673static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001674{
Sheng Yang04547152009-04-01 15:52:31 +08001675 return vmcs_config.cpu_based_2nd_exec_ctrl &
1676 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1677}
1678
Yang Zhang8d146952013-01-25 10:18:50 +08001679static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1680{
1681 return vmcs_config.cpu_based_2nd_exec_ctrl &
1682 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1683}
1684
Yang Zhang83d4c282013-01-25 10:18:49 +08001685static inline bool cpu_has_vmx_apic_register_virt(void)
1686{
1687 return vmcs_config.cpu_based_2nd_exec_ctrl &
1688 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1689}
1690
Yang Zhangc7c9c562013-01-25 10:18:51 +08001691static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1692{
1693 return vmcs_config.cpu_based_2nd_exec_ctrl &
1694 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1695}
1696
Sean Christopherson0b665d32018-08-14 09:33:34 -07001697static inline bool cpu_has_vmx_encls_vmexit(void)
1698{
1699 return vmcs_config.cpu_based_2nd_exec_ctrl &
1700 SECONDARY_EXEC_ENCLS_EXITING;
1701}
1702
Yunhong Jiang64672c92016-06-13 14:19:59 -07001703/*
1704 * Comment's format: document - errata name - stepping - processor name.
1705 * Refer from
1706 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1707 */
1708static u32 vmx_preemption_cpu_tfms[] = {
1709/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17100x000206E6,
1711/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1712/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1713/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17140x00020652,
1715/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17160x00020655,
1717/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1718/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1719/*
1720 * 320767.pdf - AAP86 - B1 -
1721 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1722 */
17230x000106E5,
1724/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17250x000106A0,
1726/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17270x000106A1,
1728/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17290x000106A4,
1730 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1731 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1732 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17330x000106A5,
1734};
1735
1736static inline bool cpu_has_broken_vmx_preemption_timer(void)
1737{
1738 u32 eax = cpuid_eax(0x00000001), i;
1739
1740 /* Clear the reserved bits */
1741 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001742 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001743 if (eax == vmx_preemption_cpu_tfms[i])
1744 return true;
1745
1746 return false;
1747}
1748
1749static inline bool cpu_has_vmx_preemption_timer(void)
1750{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001751 return vmcs_config.pin_based_exec_ctrl &
1752 PIN_BASED_VMX_PREEMPTION_TIMER;
1753}
1754
Yang Zhang01e439b2013-04-11 19:25:12 +08001755static inline bool cpu_has_vmx_posted_intr(void)
1756{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001757 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1758 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001759}
1760
1761static inline bool cpu_has_vmx_apicv(void)
1762{
1763 return cpu_has_vmx_apic_register_virt() &&
1764 cpu_has_vmx_virtual_intr_delivery() &&
1765 cpu_has_vmx_posted_intr();
1766}
1767
Sheng Yang04547152009-04-01 15:52:31 +08001768static inline bool cpu_has_vmx_flexpriority(void)
1769{
1770 return cpu_has_vmx_tpr_shadow() &&
1771 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001772}
1773
Marcelo Tosattie7997942009-06-11 12:07:40 -03001774static inline bool cpu_has_vmx_ept_execute_only(void)
1775{
Gui Jianfeng31299942010-03-15 17:29:09 +08001776 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001777}
1778
Marcelo Tosattie7997942009-06-11 12:07:40 -03001779static inline bool cpu_has_vmx_ept_2m_page(void)
1780{
Gui Jianfeng31299942010-03-15 17:29:09 +08001781 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001782}
1783
Sheng Yang878403b2010-01-05 19:02:29 +08001784static inline bool cpu_has_vmx_ept_1g_page(void)
1785{
Gui Jianfeng31299942010-03-15 17:29:09 +08001786 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001787}
1788
Sheng Yang4bc9b982010-06-02 14:05:24 +08001789static inline bool cpu_has_vmx_ept_4levels(void)
1790{
1791 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1792}
1793
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001794static inline bool cpu_has_vmx_ept_mt_wb(void)
1795{
1796 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1797}
1798
Yu Zhang855feb62017-08-24 20:27:55 +08001799static inline bool cpu_has_vmx_ept_5levels(void)
1800{
1801 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1802}
1803
Xudong Hao83c3a332012-05-28 19:33:35 +08001804static inline bool cpu_has_vmx_ept_ad_bits(void)
1805{
1806 return vmx_capability.ept & VMX_EPT_AD_BIT;
1807}
1808
Gui Jianfeng31299942010-03-15 17:29:09 +08001809static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001810{
Gui Jianfeng31299942010-03-15 17:29:09 +08001811 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001812}
1813
Gui Jianfeng31299942010-03-15 17:29:09 +08001814static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001815{
Gui Jianfeng31299942010-03-15 17:29:09 +08001816 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001817}
1818
Liran Aloncd9a4912018-05-22 17:16:15 +03001819static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1820{
1821 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1822}
1823
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001824static inline bool cpu_has_vmx_invvpid_single(void)
1825{
1826 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1827}
1828
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001829static inline bool cpu_has_vmx_invvpid_global(void)
1830{
1831 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1832}
1833
Wanpeng Li08d839c2017-03-23 05:30:08 -07001834static inline bool cpu_has_vmx_invvpid(void)
1835{
1836 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1837}
1838
Gui Jianfeng31299942010-03-15 17:29:09 +08001839static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001840{
Sheng Yang04547152009-04-01 15:52:31 +08001841 return vmcs_config.cpu_based_2nd_exec_ctrl &
1842 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001843}
1844
Gui Jianfeng31299942010-03-15 17:29:09 +08001845static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001846{
1847 return vmcs_config.cpu_based_2nd_exec_ctrl &
1848 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1849}
1850
Gui Jianfeng31299942010-03-15 17:29:09 +08001851static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001852{
1853 return vmcs_config.cpu_based_2nd_exec_ctrl &
1854 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1855}
1856
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001857static inline bool cpu_has_vmx_basic_inout(void)
1858{
1859 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1860}
1861
Paolo Bonzini35754c92015-07-29 12:05:37 +02001862static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001863{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001864 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001865}
1866
Gui Jianfeng31299942010-03-15 17:29:09 +08001867static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001868{
Sheng Yang04547152009-04-01 15:52:31 +08001869 return vmcs_config.cpu_based_2nd_exec_ctrl &
1870 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001871}
1872
Gui Jianfeng31299942010-03-15 17:29:09 +08001873static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001874{
1875 return vmcs_config.cpu_based_2nd_exec_ctrl &
1876 SECONDARY_EXEC_RDTSCP;
1877}
1878
Mao, Junjiead756a12012-07-02 01:18:48 +00001879static inline bool cpu_has_vmx_invpcid(void)
1880{
1881 return vmcs_config.cpu_based_2nd_exec_ctrl &
1882 SECONDARY_EXEC_ENABLE_INVPCID;
1883}
1884
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001885static inline bool cpu_has_virtual_nmis(void)
1886{
1887 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1888}
1889
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001890static inline bool cpu_has_vmx_wbinvd_exit(void)
1891{
1892 return vmcs_config.cpu_based_2nd_exec_ctrl &
1893 SECONDARY_EXEC_WBINVD_EXITING;
1894}
1895
Abel Gordonabc4fc52013-04-18 14:35:25 +03001896static inline bool cpu_has_vmx_shadow_vmcs(void)
1897{
1898 u64 vmx_msr;
1899 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1900 /* check if the cpu supports writing r/o exit information fields */
1901 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1902 return false;
1903
1904 return vmcs_config.cpu_based_2nd_exec_ctrl &
1905 SECONDARY_EXEC_SHADOW_VMCS;
1906}
1907
Kai Huang843e4332015-01-28 10:54:28 +08001908static inline bool cpu_has_vmx_pml(void)
1909{
1910 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1911}
1912
Haozhong Zhang64903d62015-10-20 15:39:09 +08001913static inline bool cpu_has_vmx_tsc_scaling(void)
1914{
1915 return vmcs_config.cpu_based_2nd_exec_ctrl &
1916 SECONDARY_EXEC_TSC_SCALING;
1917}
1918
Bandan Das2a499e42017-08-03 15:54:41 -04001919static inline bool cpu_has_vmx_vmfunc(void)
1920{
1921 return vmcs_config.cpu_based_2nd_exec_ctrl &
1922 SECONDARY_EXEC_ENABLE_VMFUNC;
1923}
1924
Sean Christopherson64f7a112018-04-30 10:01:06 -07001925static bool vmx_umip_emulated(void)
1926{
1927 return vmcs_config.cpu_based_2nd_exec_ctrl &
1928 SECONDARY_EXEC_DESC;
1929}
1930
Sheng Yang04547152009-04-01 15:52:31 +08001931static inline bool report_flexpriority(void)
1932{
1933 return flexpriority_enabled;
1934}
1935
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001936static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1937{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001938 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001939}
1940
Jim Mattsonf4160e42018-05-29 09:11:33 -07001941/*
1942 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1943 * to modify any valid field of the VMCS, or are the VM-exit
1944 * information fields read-only?
1945 */
1946static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1947{
1948 return to_vmx(vcpu)->nested.msrs.misc_low &
1949 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1950}
1951
Marc Orr04473782018-06-20 17:21:29 -07001952static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1953{
1954 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1955}
1956
1957static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1958{
1959 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1960 CPU_BASED_MONITOR_TRAP_FLAG;
1961}
1962
Liran Alonfa97d7d2018-07-18 14:07:59 +02001963static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1964{
1965 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1966 SECONDARY_EXEC_SHADOW_VMCS;
1967}
1968
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001969static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1970{
1971 return vmcs12->cpu_based_vm_exec_control & bit;
1972}
1973
1974static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1975{
1976 return (vmcs12->cpu_based_vm_exec_control &
1977 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1978 (vmcs12->secondary_vm_exec_control & bit);
1979}
1980
Jan Kiszkaf4124502014-03-07 20:03:13 +01001981static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1982{
1983 return vmcs12->pin_based_vm_exec_control &
1984 PIN_BASED_VMX_PREEMPTION_TIMER;
1985}
1986
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001987static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1988{
1989 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1990}
1991
1992static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1993{
1994 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1995}
1996
Nadav Har'El155a97a2013-08-05 11:07:16 +03001997static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1998{
1999 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
2000}
2001
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002002static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2003{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002004 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002005}
2006
Bandan Dasc5f983f2017-05-05 15:25:14 -04002007static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2008{
2009 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2010}
2011
Wincy Vanf2b93282015-02-03 23:56:03 +08002012static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2013{
2014 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2015}
2016
Wanpeng Li5c614b32015-10-13 09:18:36 -07002017static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2018{
2019 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2020}
2021
Wincy Van82f0dd42015-02-03 23:57:18 +08002022static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2023{
2024 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2025}
2026
Wincy Van608406e2015-02-03 23:57:51 +08002027static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2028{
2029 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2030}
2031
Wincy Van705699a2015-02-03 23:58:17 +08002032static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2033{
2034 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2035}
2036
Bandan Das27c42a12017-08-03 15:54:42 -04002037static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2038{
2039 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2040}
2041
Bandan Das41ab9372017-08-03 15:54:43 -04002042static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2043{
2044 return nested_cpu_has_vmfunc(vmcs12) &&
2045 (vmcs12->vm_function_control &
2046 VMX_VMFUNC_EPTP_SWITCHING);
2047}
2048
Liran Alonf792d272018-06-23 02:35:05 +03002049static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2050{
2051 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2052}
2053
Jim Mattsonef85b672016-12-12 11:01:37 -08002054static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002055{
2056 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002057 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002058}
2059
Jan Kiszka533558b2014-01-04 18:47:20 +01002060static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2061 u32 exit_intr_info,
2062 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002063
Rusty Russell8b9cf982007-07-30 16:31:43 +10002064static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002065{
2066 int i;
2067
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002068 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002069 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002070 return i;
2071 return -1;
2072}
2073
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002074static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002075{
2076 struct {
2077 u64 vpid : 16;
2078 u64 rsvd : 48;
2079 u64 gva;
2080 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002081 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002082
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002083 asm volatile (__ex("invvpid %2, %1") CC_SET(na)
2084 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002085 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002086}
2087
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002088static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08002089{
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 Bizjak4b1e5472018-10-11 19:40:44 +02002095 asm volatile (__ex("invept %2, %1") CC_SET(na)
2096 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002097 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002098}
2099
Avi Kivity26bb0982009-09-07 11:14:12 +03002100static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002101{
2102 int i;
2103
Rusty Russell8b9cf982007-07-30 16:31:43 +10002104 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002105 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002106 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002107 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002108}
2109
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110static void vmcs_clear(struct vmcs *vmcs)
2111{
2112 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002113 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002115 asm volatile (__ex("vmclear %1") CC_SET(na)
2116 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002117 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2119 vmcs, phys_addr);
2120}
2121
Nadav Har'Eld462b812011-05-24 15:26:10 +03002122static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2123{
2124 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002125 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2126 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002127 loaded_vmcs->cpu = -1;
2128 loaded_vmcs->launched = 0;
2129}
2130
Dongxiao Xu7725b892010-05-11 18:29:38 +08002131static void vmcs_load(struct vmcs *vmcs)
2132{
2133 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002134 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002135
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002136 if (static_branch_unlikely(&enable_evmcs))
2137 return evmcs_load(phys_addr);
2138
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002139 asm volatile (__ex("vmptrld %1") CC_SET(na)
2140 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002141 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002142 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002143 vmcs, phys_addr);
2144}
2145
Dave Young2965faa2015-09-09 15:38:55 -07002146#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002147/*
2148 * This bitmap is used to indicate whether the vmclear
2149 * operation is enabled on all cpus. All disabled by
2150 * default.
2151 */
2152static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2153
2154static inline void crash_enable_local_vmclear(int cpu)
2155{
2156 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2157}
2158
2159static inline void crash_disable_local_vmclear(int cpu)
2160{
2161 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2162}
2163
2164static inline int crash_local_vmclear_enabled(int cpu)
2165{
2166 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2167}
2168
2169static void crash_vmclear_local_loaded_vmcss(void)
2170{
2171 int cpu = raw_smp_processor_id();
2172 struct loaded_vmcs *v;
2173
2174 if (!crash_local_vmclear_enabled(cpu))
2175 return;
2176
2177 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2178 loaded_vmcss_on_cpu_link)
2179 vmcs_clear(v->vmcs);
2180}
2181#else
2182static inline void crash_enable_local_vmclear(int cpu) { }
2183static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002184#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002185
Nadav Har'Eld462b812011-05-24 15:26:10 +03002186static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002188 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002189 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190
Nadav Har'Eld462b812011-05-24 15:26:10 +03002191 if (loaded_vmcs->cpu != cpu)
2192 return; /* vcpu migration can race with cpu offline */
2193 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002195 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002196 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002197
2198 /*
2199 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2200 * is before setting loaded_vmcs->vcpu to -1 which is done in
2201 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2202 * then adds the vmcs into percpu list before it is deleted.
2203 */
2204 smp_wmb();
2205
Nadav Har'Eld462b812011-05-24 15:26:10 +03002206 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002207 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208}
2209
Nadav Har'Eld462b812011-05-24 15:26:10 +03002210static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002211{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002212 int cpu = loaded_vmcs->cpu;
2213
2214 if (cpu != -1)
2215 smp_call_function_single(cpu,
2216 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002217}
2218
Junaid Shahidfaff8752018-06-29 13:10:05 -07002219static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2220{
2221 if (vpid == 0)
2222 return true;
2223
2224 if (cpu_has_vmx_invvpid_individual_addr()) {
2225 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2226 return true;
2227 }
2228
2229 return false;
2230}
2231
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002232static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002233{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002234 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002235 return;
2236
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002237 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002238 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002239}
2240
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002241static inline void vpid_sync_vcpu_global(void)
2242{
2243 if (cpu_has_vmx_invvpid_global())
2244 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2245}
2246
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002247static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002248{
2249 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002250 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002251 else
2252 vpid_sync_vcpu_global();
2253}
2254
Sheng Yang14394422008-04-28 12:24:45 +08002255static inline void ept_sync_global(void)
2256{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002257 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002258}
2259
2260static inline void ept_sync_context(u64 eptp)
2261{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002262 if (cpu_has_vmx_invept_context())
2263 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2264 else
2265 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002266}
2267
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002268static __always_inline void vmcs_check16(unsigned long field)
2269{
2270 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2271 "16-bit accessor invalid for 64-bit field");
2272 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2273 "16-bit accessor invalid for 64-bit high field");
2274 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2275 "16-bit accessor invalid for 32-bit high field");
2276 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2277 "16-bit accessor invalid for natural width field");
2278}
2279
2280static __always_inline void vmcs_check32(unsigned long field)
2281{
2282 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2283 "32-bit accessor invalid for 16-bit field");
2284 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2285 "32-bit accessor invalid for natural width field");
2286}
2287
2288static __always_inline void vmcs_check64(unsigned long field)
2289{
2290 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2291 "64-bit accessor invalid for 16-bit field");
2292 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2293 "64-bit accessor invalid for 64-bit high field");
2294 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2295 "64-bit accessor invalid for 32-bit field");
2296 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2297 "64-bit accessor invalid for natural width field");
2298}
2299
2300static __always_inline void vmcs_checkl(unsigned long field)
2301{
2302 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2303 "Natural width accessor invalid for 16-bit field");
2304 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2305 "Natural width accessor invalid for 64-bit field");
2306 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2307 "Natural width accessor invalid for 64-bit high field");
2308 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2309 "Natural width accessor invalid for 32-bit field");
2310}
2311
2312static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313{
Avi Kivity5e520e62011-05-15 10:13:12 -04002314 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
Uros Bizjak44c2d662018-10-11 19:40:45 +02002316 asm volatile (__ex_clear("vmread %1, %0", "%k0")
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002317 : "=r"(value) : "r"(field));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318 return value;
2319}
2320
Avi Kivity96304212011-05-15 10:13:13 -04002321static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002323 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002324 if (static_branch_unlikely(&enable_evmcs))
2325 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002326 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327}
2328
Avi Kivity96304212011-05-15 10:13:13 -04002329static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002331 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002332 if (static_branch_unlikely(&enable_evmcs))
2333 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002334 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335}
2336
Avi Kivity96304212011-05-15 10:13:13 -04002337static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002339 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002340 if (static_branch_unlikely(&enable_evmcs))
2341 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002342#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002343 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002345 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346#endif
2347}
2348
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002349static __always_inline unsigned long vmcs_readl(unsigned long field)
2350{
2351 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002352 if (static_branch_unlikely(&enable_evmcs))
2353 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002354 return __vmcs_readl(field);
2355}
2356
Avi Kivitye52de1b2007-01-05 16:36:56 -08002357static noinline void vmwrite_error(unsigned long field, unsigned long value)
2358{
2359 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2360 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2361 dump_stack();
2362}
2363
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002364static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002366 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002368 asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
2369 : CC_OUT(na) (error) : "r"(field), "rm"(value));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002370 if (unlikely(error))
2371 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372}
2373
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002374static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002376 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002377 if (static_branch_unlikely(&enable_evmcs))
2378 return evmcs_write16(field, value);
2379
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381}
2382
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002383static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002385 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002386 if (static_branch_unlikely(&enable_evmcs))
2387 return evmcs_write32(field, value);
2388
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002389 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390}
2391
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002392static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002394 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002395 if (static_branch_unlikely(&enable_evmcs))
2396 return evmcs_write64(field, value);
2397
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002398 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002399#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002401 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402#endif
2403}
2404
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002405static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002406{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002407 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002408 if (static_branch_unlikely(&enable_evmcs))
2409 return evmcs_write64(field, value);
2410
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002411 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002412}
2413
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002414static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002415{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002416 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2417 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002418 if (static_branch_unlikely(&enable_evmcs))
2419 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2420
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002421 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2422}
2423
2424static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2425{
2426 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2427 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002428 if (static_branch_unlikely(&enable_evmcs))
2429 return evmcs_write32(field, evmcs_read32(field) | mask);
2430
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002431 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002432}
2433
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002434static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2435{
2436 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2437}
2438
Gleb Natapov2961e8762013-11-25 15:37:13 +02002439static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2440{
2441 vmcs_write32(VM_ENTRY_CONTROLS, val);
2442 vmx->vm_entry_controls_shadow = val;
2443}
2444
2445static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2446{
2447 if (vmx->vm_entry_controls_shadow != val)
2448 vm_entry_controls_init(vmx, val);
2449}
2450
2451static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2452{
2453 return vmx->vm_entry_controls_shadow;
2454}
2455
2456
2457static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2458{
2459 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2460}
2461
2462static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2463{
2464 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2465}
2466
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002467static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2468{
2469 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2470}
2471
Gleb Natapov2961e8762013-11-25 15:37:13 +02002472static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2473{
2474 vmcs_write32(VM_EXIT_CONTROLS, val);
2475 vmx->vm_exit_controls_shadow = val;
2476}
2477
2478static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2479{
2480 if (vmx->vm_exit_controls_shadow != val)
2481 vm_exit_controls_init(vmx, val);
2482}
2483
2484static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2485{
2486 return vmx->vm_exit_controls_shadow;
2487}
2488
2489
2490static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2491{
2492 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2493}
2494
2495static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2496{
2497 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2498}
2499
Avi Kivity2fb92db2011-04-27 19:42:18 +03002500static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2501{
2502 vmx->segment_cache.bitmask = 0;
2503}
2504
2505static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2506 unsigned field)
2507{
2508 bool ret;
2509 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2510
2511 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2512 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2513 vmx->segment_cache.bitmask = 0;
2514 }
2515 ret = vmx->segment_cache.bitmask & mask;
2516 vmx->segment_cache.bitmask |= mask;
2517 return ret;
2518}
2519
2520static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2521{
2522 u16 *p = &vmx->segment_cache.seg[seg].selector;
2523
2524 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2525 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2526 return *p;
2527}
2528
2529static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2530{
2531 ulong *p = &vmx->segment_cache.seg[seg].base;
2532
2533 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2534 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2535 return *p;
2536}
2537
2538static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2539{
2540 u32 *p = &vmx->segment_cache.seg[seg].limit;
2541
2542 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2543 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2544 return *p;
2545}
2546
2547static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2548{
2549 u32 *p = &vmx->segment_cache.seg[seg].ar;
2550
2551 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2552 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2553 return *p;
2554}
2555
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002556static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2557{
2558 u32 eb;
2559
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002560 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002561 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002562 /*
2563 * Guest access to VMware backdoor ports could legitimately
2564 * trigger #GP because of TSS I/O permission bitmap.
2565 * We intercept those #GP and allow access to them anyway
2566 * as VMware does.
2567 */
2568 if (enable_vmware_backdoor)
2569 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002570 if ((vcpu->guest_debug &
2571 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2572 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2573 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002574 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002575 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002576 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002577 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002578
2579 /* When we are running a nested L2 guest and L1 specified for it a
2580 * certain exception bitmap, we must trap the same exceptions and pass
2581 * them to L1. When running L2, we will only handle the exceptions
2582 * specified above if L1 did not want them.
2583 */
2584 if (is_guest_mode(vcpu))
2585 eb |= get_vmcs12(vcpu)->exception_bitmap;
2586
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002587 vmcs_write32(EXCEPTION_BITMAP, eb);
2588}
2589
Ashok Raj15d45072018-02-01 22:59:43 +01002590/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002591 * Check if MSR is intercepted for currently loaded MSR bitmap.
2592 */
2593static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2594{
2595 unsigned long *msr_bitmap;
2596 int f = sizeof(unsigned long);
2597
2598 if (!cpu_has_vmx_msr_bitmap())
2599 return true;
2600
2601 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2602
2603 if (msr <= 0x1fff) {
2604 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2605 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2606 msr &= 0x1fff;
2607 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2608 }
2609
2610 return true;
2611}
2612
2613/*
Ashok Raj15d45072018-02-01 22:59:43 +01002614 * Check if MSR is intercepted for L01 MSR bitmap.
2615 */
2616static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2617{
2618 unsigned long *msr_bitmap;
2619 int f = sizeof(unsigned long);
2620
2621 if (!cpu_has_vmx_msr_bitmap())
2622 return true;
2623
2624 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2625
2626 if (msr <= 0x1fff) {
2627 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2628 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2629 msr &= 0x1fff;
2630 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2631 }
2632
2633 return true;
2634}
2635
Gleb Natapov2961e8762013-11-25 15:37:13 +02002636static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2637 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002638{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002639 vm_entry_controls_clearbit(vmx, entry);
2640 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002641}
2642
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002643static int find_msr(struct vmx_msrs *m, unsigned int msr)
2644{
2645 unsigned int i;
2646
2647 for (i = 0; i < m->nr; ++i) {
2648 if (m->val[i].index == msr)
2649 return i;
2650 }
2651 return -ENOENT;
2652}
2653
Avi Kivity61d2ef22010-04-28 16:40:38 +03002654static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2655{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002656 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002657 struct msr_autoload *m = &vmx->msr_autoload;
2658
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002659 switch (msr) {
2660 case MSR_EFER:
2661 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002662 clear_atomic_switch_msr_special(vmx,
2663 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002664 VM_EXIT_LOAD_IA32_EFER);
2665 return;
2666 }
2667 break;
2668 case MSR_CORE_PERF_GLOBAL_CTRL:
2669 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002670 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002671 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2672 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2673 return;
2674 }
2675 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002676 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002677 i = find_msr(&m->guest, msr);
2678 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002679 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002680 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002681 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002682 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002683
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002684skip_guest:
2685 i = find_msr(&m->host, msr);
2686 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002687 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002688
2689 --m->host.nr;
2690 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002691 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002692}
2693
Gleb Natapov2961e8762013-11-25 15:37:13 +02002694static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2695 unsigned long entry, unsigned long exit,
2696 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2697 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002698{
2699 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07002700 if (host_val_vmcs != HOST_IA32_EFER)
2701 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002702 vm_entry_controls_setbit(vmx, entry);
2703 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002704}
2705
Avi Kivity61d2ef22010-04-28 16:40:38 +03002706static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002707 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002708{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002709 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002710 struct msr_autoload *m = &vmx->msr_autoload;
2711
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002712 switch (msr) {
2713 case MSR_EFER:
2714 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002715 add_atomic_switch_msr_special(vmx,
2716 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002717 VM_EXIT_LOAD_IA32_EFER,
2718 GUEST_IA32_EFER,
2719 HOST_IA32_EFER,
2720 guest_val, host_val);
2721 return;
2722 }
2723 break;
2724 case MSR_CORE_PERF_GLOBAL_CTRL:
2725 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002726 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002727 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2728 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2729 GUEST_IA32_PERF_GLOBAL_CTRL,
2730 HOST_IA32_PERF_GLOBAL_CTRL,
2731 guest_val, host_val);
2732 return;
2733 }
2734 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002735 case MSR_IA32_PEBS_ENABLE:
2736 /* PEBS needs a quiescent period after being disabled (to write
2737 * a record). Disabling PEBS through VMX MSR swapping doesn't
2738 * provide that period, so a CPU could write host's record into
2739 * guest's memory.
2740 */
2741 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002742 }
2743
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002744 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002745 if (!entry_only)
2746 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002747
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002748 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002749 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002750 "Can't add msr %x\n", msr);
2751 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002752 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002753 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002754 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002755 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002756 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002757 m->guest.val[i].index = msr;
2758 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002759
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002760 if (entry_only)
2761 return;
2762
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002763 if (j < 0) {
2764 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002765 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002766 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002767 m->host.val[j].index = msr;
2768 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002769}
2770
Avi Kivity92c0d902009-10-29 11:00:16 +02002771static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002772{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002773 u64 guest_efer = vmx->vcpu.arch.efer;
2774 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002775
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002776 if (!enable_ept) {
2777 /*
2778 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2779 * host CPUID is more efficient than testing guest CPUID
2780 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2781 */
2782 if (boot_cpu_has(X86_FEATURE_SMEP))
2783 guest_efer |= EFER_NX;
2784 else if (!(guest_efer & EFER_NX))
2785 ignore_bits |= EFER_NX;
2786 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002787
Avi Kivity51c6cf62007-08-29 03:48:05 +03002788 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002789 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002790 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002791 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002792#ifdef CONFIG_X86_64
2793 ignore_bits |= EFER_LMA | EFER_LME;
2794 /* SCE is meaningful only in long mode on Intel */
2795 if (guest_efer & EFER_LMA)
2796 ignore_bits &= ~(u64)EFER_SCE;
2797#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002798
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002799 /*
2800 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2801 * On CPUs that support "load IA32_EFER", always switch EFER
2802 * atomically, since it's faster than switching it manually.
2803 */
2804 if (cpu_has_load_ia32_efer ||
2805 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002806 if (!(guest_efer & EFER_LMA))
2807 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002808 if (guest_efer != host_efer)
2809 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002810 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002811 else
2812 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002813 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002814 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002815 clear_atomic_switch_msr(vmx, MSR_EFER);
2816
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002817 guest_efer &= ~ignore_bits;
2818 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002819
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002820 vmx->guest_msrs[efer_offset].data = guest_efer;
2821 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2822
2823 return true;
2824 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002825}
2826
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002827#ifdef CONFIG_X86_32
2828/*
2829 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2830 * VMCS rather than the segment table. KVM uses this helper to figure
2831 * out the current bases to poke them into the VMCS before entry.
2832 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002833static unsigned long segment_base(u16 selector)
2834{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002835 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836 unsigned long v;
2837
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002838 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002839 return 0;
2840
Thomas Garnier45fc8752017-03-14 10:05:08 -07002841 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002843 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002844 u16 ldt_selector = kvm_read_ldt();
2845
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002846 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002847 return 0;
2848
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002849 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002850 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002851 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002852 return v;
2853}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002854#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002855
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002856static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002857{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002858 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002859 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002860#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002861 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002862#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002863 unsigned long fs_base, gs_base;
2864 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002865 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002866
Sean Christophersond264ee02018-08-27 15:21:12 -07002867 vmx->req_immediate_exit = false;
2868
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002869 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002870 return;
2871
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002872 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002873 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002874
Avi Kivity33ed6322007-05-02 16:54:03 +03002875 /*
2876 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2877 * allow segment selectors with cpl > 0 or ti == 1.
2878 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002879 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002880
2881#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002882 savesegment(ds, host_state->ds_sel);
2883 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002884
2885 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002886 if (likely(is_64bit_mm(current->mm))) {
2887 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002888 fs_sel = current->thread.fsindex;
2889 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002890 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002891 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002892 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002893 savesegment(fs, fs_sel);
2894 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002895 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002896 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002897 }
2898
Paolo Bonzini4679b612018-09-24 17:23:01 +02002899 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002900#else
Sean Christophersone368b872018-07-23 12:32:41 -07002901 savesegment(fs, fs_sel);
2902 savesegment(gs, gs_sel);
2903 fs_base = segment_base(fs_sel);
2904 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002905#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002906
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002907 if (unlikely(fs_sel != host_state->fs_sel)) {
2908 if (!(fs_sel & 7))
2909 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2910 else
2911 vmcs_write16(HOST_FS_SELECTOR, 0);
2912 host_state->fs_sel = fs_sel;
2913 }
2914 if (unlikely(gs_sel != host_state->gs_sel)) {
2915 if (!(gs_sel & 7))
2916 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2917 else
2918 vmcs_write16(HOST_GS_SELECTOR, 0);
2919 host_state->gs_sel = gs_sel;
2920 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002921 if (unlikely(fs_base != host_state->fs_base)) {
2922 vmcs_writel(HOST_FS_BASE, fs_base);
2923 host_state->fs_base = fs_base;
2924 }
2925 if (unlikely(gs_base != host_state->gs_base)) {
2926 vmcs_writel(HOST_GS_BASE, gs_base);
2927 host_state->gs_base = gs_base;
2928 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002929
Avi Kivity26bb0982009-09-07 11:14:12 +03002930 for (i = 0; i < vmx->save_nmsrs; ++i)
2931 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002932 vmx->guest_msrs[i].data,
2933 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002934}
2935
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002936static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002937{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002938 struct vmcs_host_state *host_state;
2939
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002940 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002941 return;
2942
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002943 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002944 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002945
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002946 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002947 vmx->loaded_cpu_state = NULL;
2948
Avi Kivityc8770e72010-11-11 12:37:26 +02002949#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002950 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002951#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002952 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2953 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002954#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002955 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002956#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002957 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002958#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002959 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002960 if (host_state->fs_sel & 7)
2961 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002962#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002963 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2964 loadsegment(ds, host_state->ds_sel);
2965 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002966 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002967#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002968 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002969#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002970 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002971#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002972 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002973}
2974
Sean Christopherson678e3152018-07-23 12:32:43 -07002975#ifdef CONFIG_X86_64
2976static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002977{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002978 preempt_disable();
2979 if (vmx->loaded_cpu_state)
2980 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2981 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002982 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002983}
2984
Sean Christopherson678e3152018-07-23 12:32:43 -07002985static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2986{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002987 preempt_disable();
2988 if (vmx->loaded_cpu_state)
2989 wrmsrl(MSR_KERNEL_GS_BASE, data);
2990 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002991 vmx->msr_guest_kernel_gs_base = data;
2992}
2993#endif
2994
Feng Wu28b835d2015-09-18 22:29:54 +08002995static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2996{
2997 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2998 struct pi_desc old, new;
2999 unsigned int dest;
3000
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003001 /*
3002 * In case of hot-plug or hot-unplug, we may have to undo
3003 * vmx_vcpu_pi_put even if there is no assigned device. And we
3004 * always keep PI.NDST up to date for simplicity: it makes the
3005 * code easier, and CPU migration is not a fast path.
3006 */
3007 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003008 return;
3009
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003010 /*
3011 * First handle the simple case where no cmpxchg is necessary; just
3012 * allow posting non-urgent interrupts.
3013 *
3014 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3015 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3016 * expects the VCPU to be on the blocked_vcpu_list that matches
3017 * PI.NDST.
3018 */
3019 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3020 vcpu->cpu == cpu) {
3021 pi_clear_sn(pi_desc);
3022 return;
3023 }
3024
3025 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003026 do {
3027 old.control = new.control = pi_desc->control;
3028
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003029 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003030
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003031 if (x2apic_enabled())
3032 new.ndst = dest;
3033 else
3034 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003035
Feng Wu28b835d2015-09-18 22:29:54 +08003036 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003037 } while (cmpxchg64(&pi_desc->control, old.control,
3038 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003039}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003040
Peter Feinerc95ba922016-08-17 09:36:47 -07003041static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3042{
3043 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3044 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3045}
3046
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047/*
3048 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3049 * vcpu mutex is already taken.
3050 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003051static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003053 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003054 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003056 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003057 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003058 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003059 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003060
3061 /*
3062 * Read loaded_vmcs->cpu should be before fetching
3063 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3064 * See the comments in __loaded_vmcs_clear().
3065 */
3066 smp_rmb();
3067
Nadav Har'Eld462b812011-05-24 15:26:10 +03003068 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3069 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003070 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003071 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003072 }
3073
3074 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3075 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3076 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003077 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003078 }
3079
3080 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003081 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003082 unsigned long sysenter_esp;
3083
3084 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003085
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 /*
3087 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003088 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003090 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003091 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003092 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003094 /*
3095 * VM exits change the host TR limit to 0x67 after a VM
3096 * exit. This is okay, since 0x67 covers everything except
3097 * the IO bitmap and have have code to handle the IO bitmap
3098 * being lost after a VM exit.
3099 */
3100 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3101
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3103 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003104
Nadav Har'Eld462b812011-05-24 15:26:10 +03003105 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 }
Feng Wu28b835d2015-09-18 22:29:54 +08003107
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003108 /* Setup TSC multiplier */
3109 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003110 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3111 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003112
Feng Wu28b835d2015-09-18 22:29:54 +08003113 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003114 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003115 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003116}
3117
3118static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3119{
3120 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3121
3122 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003123 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3124 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003125 return;
3126
3127 /* Set SN when the vCPU is preempted */
3128 if (vcpu->preempted)
3129 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130}
3131
3132static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3133{
Feng Wu28b835d2015-09-18 22:29:54 +08003134 vmx_vcpu_pi_put(vcpu);
3135
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003136 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137}
3138
Wanpeng Lif244dee2017-07-20 01:11:54 -07003139static bool emulation_required(struct kvm_vcpu *vcpu)
3140{
3141 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3142}
3143
Avi Kivityedcafe32009-12-30 18:07:40 +02003144static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3145
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003146/*
3147 * Return the cr0 value that a nested guest would read. This is a combination
3148 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3149 * its hypervisor (cr0_read_shadow).
3150 */
3151static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3152{
3153 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3154 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3155}
3156static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3157{
3158 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3159 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3160}
3161
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3163{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003164 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003165
Avi Kivity6de12732011-03-07 12:51:22 +02003166 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3167 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3168 rflags = vmcs_readl(GUEST_RFLAGS);
3169 if (to_vmx(vcpu)->rmode.vm86_active) {
3170 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3171 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3172 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3173 }
3174 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003175 }
Avi Kivity6de12732011-03-07 12:51:22 +02003176 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177}
3178
3179static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3180{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003181 unsigned long old_rflags = vmx_get_rflags(vcpu);
3182
Avi Kivity6de12732011-03-07 12:51:22 +02003183 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3184 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003185 if (to_vmx(vcpu)->rmode.vm86_active) {
3186 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003187 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003188 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003190
3191 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3192 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193}
3194
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003195static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003196{
3197 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3198 int ret = 0;
3199
3200 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003201 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003202 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003203 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003204
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003205 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003206}
3207
3208static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3209{
3210 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3211 u32 interruptibility = interruptibility_old;
3212
3213 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3214
Jan Kiszka48005f62010-02-19 19:38:07 +01003215 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003216 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003217 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003218 interruptibility |= GUEST_INTR_STATE_STI;
3219
3220 if ((interruptibility != interruptibility_old))
3221 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3222}
3223
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3225{
3226 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003228 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003230 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231
Glauber Costa2809f5d2009-05-12 16:21:05 -04003232 /* skipping an emulated instruction also counts */
3233 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234}
3235
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003236static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3237 unsigned long exit_qual)
3238{
3239 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3240 unsigned int nr = vcpu->arch.exception.nr;
3241 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3242
3243 if (vcpu->arch.exception.has_error_code) {
3244 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3245 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3246 }
3247
3248 if (kvm_exception_is_soft(nr))
3249 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3250 else
3251 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3252
3253 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3254 vmx_get_nmi_mask(vcpu))
3255 intr_info |= INTR_INFO_UNBLOCK_NMI;
3256
3257 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3258}
3259
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003260/*
3261 * KVM wants to inject page-faults which it got to the guest. This function
3262 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003263 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003264static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003265{
3266 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003267 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003268
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003269 if (nr == PF_VECTOR) {
3270 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003271 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003272 return 1;
3273 }
3274 /*
3275 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3276 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3277 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3278 * can be written only when inject_pending_event runs. This should be
3279 * conditional on a new capability---if the capability is disabled,
3280 * kvm_multiple_exception would write the ancillary information to
3281 * CR2 or DR6, for backwards ABI-compatibility.
3282 */
3283 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3284 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003285 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003286 return 1;
3287 }
3288 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003289 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003290 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003291 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003292 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3293 *exit_qual ^= DR6_RTM;
3294 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003295 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003296 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003297 return 1;
3298 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003299 }
3300
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003301 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003302}
3303
Wanpeng Licaa057a2018-03-12 04:53:03 -07003304static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3305{
3306 /*
3307 * Ensure that we clear the HLT state in the VMCS. We don't need to
3308 * explicitly skip the instruction because if the HLT state is set,
3309 * then the instruction is already executing and RIP has already been
3310 * advanced.
3311 */
3312 if (kvm_hlt_in_guest(vcpu->kvm) &&
3313 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3314 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3315}
3316
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003317static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003318{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003319 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003320 unsigned nr = vcpu->arch.exception.nr;
3321 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003322 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003323 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003324
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003325 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003326 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003327 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3328 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003329
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003330 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003331 int inc_eip = 0;
3332 if (kvm_exception_is_soft(nr))
3333 inc_eip = vcpu->arch.event_exit_inst_len;
3334 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003335 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003336 return;
3337 }
3338
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003339 WARN_ON_ONCE(vmx->emulation_required);
3340
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003341 if (kvm_exception_is_soft(nr)) {
3342 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3343 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003344 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3345 } else
3346 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3347
3348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003349
3350 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003351}
3352
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003353static bool vmx_rdtscp_supported(void)
3354{
3355 return cpu_has_vmx_rdtscp();
3356}
3357
Mao, Junjiead756a12012-07-02 01:18:48 +00003358static bool vmx_invpcid_supported(void)
3359{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003360 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003361}
3362
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363/*
Eddie Donga75beee2007-05-17 18:55:15 +03003364 * Swap MSR entry in host/guest MSR entry array.
3365 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003366static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003367{
Avi Kivity26bb0982009-09-07 11:14:12 +03003368 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003369
3370 tmp = vmx->guest_msrs[to];
3371 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3372 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003373}
3374
3375/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003376 * Set up the vmcs to automatically save and restore system
3377 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3378 * mode, as fiddling with msrs is very expensive.
3379 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003380static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003381{
Avi Kivity26bb0982009-09-07 11:14:12 +03003382 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003383
Eddie Donga75beee2007-05-17 18:55:15 +03003384 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003385#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003386 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003387 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003388 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003389 move_msr_up(vmx, index, save_nmsrs++);
3390 index = __find_msr_index(vmx, MSR_LSTAR);
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_CSTAR);
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++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003396 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003397 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003398 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003399 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003400 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003401 * if efer.sce is enabled.
3402 */
Brian Gerst8c065852010-07-17 09:03:26 -04003403 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003404 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003405 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003406 }
Eddie Donga75beee2007-05-17 18:55:15 +03003407#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003408 index = __find_msr_index(vmx, MSR_EFER);
3409 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003410 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003411
Avi Kivity26bb0982009-09-07 11:14:12 +03003412 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003413
Yang Zhang8d146952013-01-25 10:18:50 +08003414 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003415 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003416}
3417
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003418static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003420 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003422 if (is_guest_mode(vcpu) &&
3423 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3424 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3425
3426 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427}
3428
3429/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003430 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003432static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003434 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003435 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003436 * We're here if L1 chose not to trap WRMSR to TSC. According
3437 * to the spec, this should set L1's TSC; The offset that L1
3438 * set for L2 remains unchanged, and still needs to be added
3439 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003440 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003441 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003442 /* recalculate vmcs02.TSC_OFFSET: */
3443 vmcs12 = get_vmcs12(vcpu);
3444 vmcs_write64(TSC_OFFSET, offset +
3445 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3446 vmcs12->tsc_offset : 0));
3447 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003448 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3449 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003450 vmcs_write64(TSC_OFFSET, offset);
3451 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452}
3453
Nadav Har'El801d3422011-05-25 23:02:23 +03003454/*
3455 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3456 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3457 * all guests if the "nested" module option is off, and can also be disabled
3458 * for a single guest by disabling its VMX cpuid bit.
3459 */
3460static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3461{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003462 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003463}
3464
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3467 * returned for the various VMX controls MSRs when nested VMX is enabled.
3468 * The same values should also be used to verify that vmcs12 control fields are
3469 * valid during nested entry from L1 to L2.
3470 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3471 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3472 * bit in the high half is on if the corresponding bit in the control field
3473 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003475static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003476{
Paolo Bonzini13893092018-02-26 13:40:09 +01003477 if (!nested) {
3478 memset(msrs, 0, sizeof(*msrs));
3479 return;
3480 }
3481
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003482 /*
3483 * Note that as a general rule, the high half of the MSRs (bits in
3484 * the control fields which may be 1) should be initialized by the
3485 * intersection of the underlying hardware's MSR (i.e., features which
3486 * can be supported) and the list of features we want to expose -
3487 * because they are known to be properly supported in our code.
3488 * Also, usually, the low half of the MSRs (bits which must be 1) can
3489 * be set to 0, meaning that L1 may turn off any of these bits. The
3490 * reason is that if one of these bits is necessary, it will appear
3491 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3492 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003493 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003494 * These rules have exceptions below.
3495 */
3496
3497 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003498 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003499 msrs->pinbased_ctls_low,
3500 msrs->pinbased_ctls_high);
3501 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003502 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003503 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003504 PIN_BASED_EXT_INTR_MASK |
3505 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003506 PIN_BASED_VIRTUAL_NMIS |
3507 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003508 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003509 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003510 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003511
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003512 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003513 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003514 msrs->exit_ctls_low,
3515 msrs->exit_ctls_high);
3516 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003517 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003518
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003519 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003520#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003521 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003522#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003523 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003525 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003526 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003527 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3528
Jan Kiszka2996fca2014-06-16 13:59:43 +02003529 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003530 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003531
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003532 /* entry controls */
3533 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003534 msrs->entry_ctls_low,
3535 msrs->entry_ctls_high);
3536 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003537 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003538 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003539#ifdef CONFIG_X86_64
3540 VM_ENTRY_IA32E_MODE |
3541#endif
3542 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003544 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003545
Jan Kiszka2996fca2014-06-16 13:59:43 +02003546 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003547 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003548
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003549 /* cpu-based controls */
3550 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003551 msrs->procbased_ctls_low,
3552 msrs->procbased_ctls_high);
3553 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003554 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003555 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003556 CPU_BASED_VIRTUAL_INTR_PENDING |
3557 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003558 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3559 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3560 CPU_BASED_CR3_STORE_EXITING |
3561#ifdef CONFIG_X86_64
3562 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3563#endif
3564 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003565 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3566 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3567 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3568 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003569 /*
3570 * We can allow some features even when not supported by the
3571 * hardware. For example, L1 can specify an MSR bitmap - and we
3572 * can use it to avoid exits to L1 - even when L0 runs L2
3573 * without MSR bitmaps.
3574 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003575 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003576 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003577 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003578
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003579 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003580 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003581 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3582
Paolo Bonzini80154d72017-08-24 13:55:35 +02003583 /*
3584 * secondary cpu-based controls. Do not include those that
3585 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3586 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003587 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003588 msrs->secondary_ctls_low,
3589 msrs->secondary_ctls_high);
3590 msrs->secondary_ctls_low = 0;
3591 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003592 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003593 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003594 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003595 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003596 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003597
Liran Alon32c7acf2018-06-23 02:35:11 +03003598 /*
3599 * We can emulate "VMCS shadowing," even if the hardware
3600 * doesn't support it.
3601 */
3602 msrs->secondary_ctls_high |=
3603 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003604
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003605 if (enable_ept) {
3606 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003608 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003609 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003610 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003611 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003613 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003614 msrs->ept_caps &= vmx_capability.ept;
3615 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003616 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3617 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003618 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003620 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003621 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003622 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003623 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003624
Bandan Das27c42a12017-08-03 15:54:42 -04003625 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003626 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003627 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003628 /*
3629 * Advertise EPTP switching unconditionally
3630 * since we emulate it
3631 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003632 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003634 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003635 }
3636
Paolo Bonzinief697a72016-03-18 16:58:38 +01003637 /*
3638 * Old versions of KVM use the single-context version without
3639 * checking for support, so declare that it is supported even
3640 * though it is treated as global context. The alternative is
3641 * not failing the single-context invvpid, and it is worse.
3642 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003643 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003644 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003645 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003647 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003648 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003649
Radim Krčmář0790ec12015-03-17 14:02:32 +01003650 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003651 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003652 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3653
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003654 if (flexpriority_enabled)
3655 msrs->secondary_ctls_high |=
3656 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3657
Jan Kiszkac18911a2013-03-13 16:06:41 +01003658 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003659 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003660 msrs->misc_low,
3661 msrs->misc_high);
3662 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3663 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003664 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003665 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003666 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003667 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003668
3669 /*
3670 * This MSR reports some information about VMX support. We
3671 * should return information about the VMX we emulate for the
3672 * guest, and the VMCS structure we give it - not about the
3673 * VMX support of the underlying hardware.
3674 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003675 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003676 VMCS12_REVISION |
3677 VMX_BASIC_TRUE_CTLS |
3678 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3679 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3680
3681 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003682 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003683
3684 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003685 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003686 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3687 * We picked the standard core2 setting.
3688 */
3689#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3690#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003691 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3692 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003693
3694 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003695 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3696 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003697
3698 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003699 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700}
3701
David Matlack38991522016-11-29 18:14:08 -08003702/*
3703 * if fixed0[i] == 1: val[i] must be 1
3704 * if fixed1[i] == 0: val[i] must be 0
3705 */
3706static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3707{
3708 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003709}
3710
3711static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3712{
David Matlack38991522016-11-29 18:14:08 -08003713 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003714}
3715
3716static inline u64 vmx_control_msr(u32 low, u32 high)
3717{
3718 return low | ((u64)high << 32);
3719}
3720
David Matlack62cc6b9d2016-11-29 18:14:07 -08003721static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3722{
3723 superset &= mask;
3724 subset &= mask;
3725
3726 return (superset | subset) == superset;
3727}
3728
3729static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3730{
3731 const u64 feature_and_reserved =
3732 /* feature (except bit 48; see below) */
3733 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3734 /* reserved */
3735 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003736 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003737
3738 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3739 return -EINVAL;
3740
3741 /*
3742 * KVM does not emulate a version of VMX that constrains physical
3743 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3744 */
3745 if (data & BIT_ULL(48))
3746 return -EINVAL;
3747
3748 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3749 vmx_basic_vmcs_revision_id(data))
3750 return -EINVAL;
3751
3752 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3753 return -EINVAL;
3754
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003755 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003756 return 0;
3757}
3758
3759static int
3760vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3761{
3762 u64 supported;
3763 u32 *lowp, *highp;
3764
3765 switch (msr_index) {
3766 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003767 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3768 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003769 break;
3770 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003771 lowp = &vmx->nested.msrs.procbased_ctls_low;
3772 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003773 break;
3774 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 lowp = &vmx->nested.msrs.exit_ctls_low;
3776 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003777 break;
3778 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003779 lowp = &vmx->nested.msrs.entry_ctls_low;
3780 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003781 break;
3782 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003783 lowp = &vmx->nested.msrs.secondary_ctls_low;
3784 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003785 break;
3786 default:
3787 BUG();
3788 }
3789
3790 supported = vmx_control_msr(*lowp, *highp);
3791
3792 /* Check must-be-1 bits are still 1. */
3793 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3794 return -EINVAL;
3795
3796 /* Check must-be-0 bits are still 0. */
3797 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3798 return -EINVAL;
3799
3800 *lowp = data;
3801 *highp = data >> 32;
3802 return 0;
3803}
3804
3805static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3806{
3807 const u64 feature_and_reserved_bits =
3808 /* feature */
3809 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3810 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3811 /* reserved */
3812 GENMASK_ULL(13, 9) | BIT_ULL(31);
3813 u64 vmx_misc;
3814
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003815 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3816 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003817
3818 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3819 return -EINVAL;
3820
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003821 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003822 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3823 vmx_misc_preemption_timer_rate(data) !=
3824 vmx_misc_preemption_timer_rate(vmx_misc))
3825 return -EINVAL;
3826
3827 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3828 return -EINVAL;
3829
3830 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3831 return -EINVAL;
3832
3833 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3834 return -EINVAL;
3835
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003836 vmx->nested.msrs.misc_low = data;
3837 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003838
3839 /*
3840 * If L1 has read-only VM-exit information fields, use the
3841 * less permissive vmx_vmwrite_bitmap to specify write
3842 * permissions for the shadow VMCS.
3843 */
3844 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3845 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3846
David Matlack62cc6b9d2016-11-29 18:14:07 -08003847 return 0;
3848}
3849
3850static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3851{
3852 u64 vmx_ept_vpid_cap;
3853
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003854 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3855 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003856
3857 /* Every bit is either reserved or a feature bit. */
3858 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3859 return -EINVAL;
3860
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003861 vmx->nested.msrs.ept_caps = data;
3862 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003863 return 0;
3864}
3865
3866static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3867{
3868 u64 *msr;
3869
3870 switch (msr_index) {
3871 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003872 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003873 break;
3874 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003875 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003876 break;
3877 default:
3878 BUG();
3879 }
3880
3881 /*
3882 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3883 * must be 1 in the restored value.
3884 */
3885 if (!is_bitwise_subset(data, *msr, -1ULL))
3886 return -EINVAL;
3887
3888 *msr = data;
3889 return 0;
3890}
3891
3892/*
3893 * Called when userspace is restoring VMX MSRs.
3894 *
3895 * Returns 0 on success, non-0 otherwise.
3896 */
3897static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3898{
3899 struct vcpu_vmx *vmx = to_vmx(vcpu);
3900
Jim Mattsona943ac52018-05-29 09:11:32 -07003901 /*
3902 * Don't allow changes to the VMX capability MSRs while the vCPU
3903 * is in VMX operation.
3904 */
3905 if (vmx->nested.vmxon)
3906 return -EBUSY;
3907
David Matlack62cc6b9d2016-11-29 18:14:07 -08003908 switch (msr_index) {
3909 case MSR_IA32_VMX_BASIC:
3910 return vmx_restore_vmx_basic(vmx, data);
3911 case MSR_IA32_VMX_PINBASED_CTLS:
3912 case MSR_IA32_VMX_PROCBASED_CTLS:
3913 case MSR_IA32_VMX_EXIT_CTLS:
3914 case MSR_IA32_VMX_ENTRY_CTLS:
3915 /*
3916 * The "non-true" VMX capability MSRs are generated from the
3917 * "true" MSRs, so we do not support restoring them directly.
3918 *
3919 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3920 * should restore the "true" MSRs with the must-be-1 bits
3921 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3922 * DEFAULT SETTINGS".
3923 */
3924 return -EINVAL;
3925 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3926 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3927 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3928 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3929 case MSR_IA32_VMX_PROCBASED_CTLS2:
3930 return vmx_restore_control_msr(vmx, msr_index, data);
3931 case MSR_IA32_VMX_MISC:
3932 return vmx_restore_vmx_misc(vmx, data);
3933 case MSR_IA32_VMX_CR0_FIXED0:
3934 case MSR_IA32_VMX_CR4_FIXED0:
3935 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3936 case MSR_IA32_VMX_CR0_FIXED1:
3937 case MSR_IA32_VMX_CR4_FIXED1:
3938 /*
3939 * These MSRs are generated based on the vCPU's CPUID, so we
3940 * do not support restoring them directly.
3941 */
3942 return -EINVAL;
3943 case MSR_IA32_VMX_EPT_VPID_CAP:
3944 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3945 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003946 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003947 return 0;
3948 default:
3949 /*
3950 * The rest of the VMX capability MSRs do not support restore.
3951 */
3952 return -EINVAL;
3953 }
3954}
3955
Jan Kiszkacae50132014-01-04 18:47:22 +01003956/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003957static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003958{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003959 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003960 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003961 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003962 break;
3963 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3964 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003965 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003966 msrs->pinbased_ctls_low,
3967 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003968 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3969 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003970 break;
3971 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3972 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003973 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003974 msrs->procbased_ctls_low,
3975 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003976 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3977 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003978 break;
3979 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3980 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003981 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003982 msrs->exit_ctls_low,
3983 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003984 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3985 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003986 break;
3987 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3988 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003989 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003990 msrs->entry_ctls_low,
3991 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003992 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3993 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 break;
3995 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003996 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003997 msrs->misc_low,
3998 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004000 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 break;
4009 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004010 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004011 break;
4012 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004013 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
4015 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004016 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004017 msrs->secondary_ctls_low,
4018 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004019 break;
4020 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004021 *pdata = msrs->ept_caps |
4022 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004023 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004024 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004025 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004026 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004027 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004028 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004029 }
4030
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004031 return 0;
4032}
4033
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004034static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4035 uint64_t val)
4036{
4037 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4038
4039 return !(val & ~valid_bits);
4040}
4041
Tom Lendacky801e4592018-02-21 13:39:51 -06004042static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4043{
Paolo Bonzini13893092018-02-26 13:40:09 +01004044 switch (msr->index) {
4045 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4046 if (!nested)
4047 return 1;
4048 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4049 default:
4050 return 1;
4051 }
4052
4053 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004054}
4055
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004056/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 * Reads an msr value (of 'msr_index') into 'pdata'.
4058 * Returns 0 on success, non-0 otherwise.
4059 * Assumes vcpu_load() was already called.
4060 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004061static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004063 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004064 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004066 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004067#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 break;
4071 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004072 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004074 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004075 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004076 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004077#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004079 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004080 case MSR_IA32_SPEC_CTRL:
4081 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004082 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4083 return 1;
4084
4085 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4086 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004087 case MSR_IA32_ARCH_CAPABILITIES:
4088 if (!msr_info->host_initiated &&
4089 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4090 return 1;
4091 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4092 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004093 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004094 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 break;
4096 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004097 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 break;
4099 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004100 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004102 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004103 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004104 (!msr_info->host_initiated &&
4105 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004106 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004107 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004108 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004109 case MSR_IA32_MCG_EXT_CTL:
4110 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004111 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004112 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004113 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004114 msr_info->data = vcpu->arch.mcg_ext_ctl;
4115 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004116 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004117 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004118 break;
4119 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4120 if (!nested_vmx_allowed(vcpu))
4121 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004122 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4123 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004124 case MSR_IA32_XSS:
4125 if (!vmx_xsaves_supported())
4126 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004127 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004128 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004129 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004130 if (!msr_info->host_initiated &&
4131 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004132 return 1;
4133 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004135 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004136 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004137 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004138 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004140 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 }
4142
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143 return 0;
4144}
4145
Jan Kiszkacae50132014-01-04 18:47:22 +01004146static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4147
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148/*
4149 * Writes msr value into into the appropriate "register".
4150 * Returns 0 on success, non-0 otherwise.
4151 * Assumes vcpu_load() was already called.
4152 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004153static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004156 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004157 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004158 u32 msr_index = msr_info->index;
4159 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004160
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004162 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004163 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004164 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004165#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004167 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168 vmcs_writel(GUEST_FS_BASE, data);
4169 break;
4170 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004171 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172 vmcs_writel(GUEST_GS_BASE, data);
4173 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004174 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004175 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004176 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177#endif
4178 case MSR_IA32_SYSENTER_CS:
4179 vmcs_write32(GUEST_SYSENTER_CS, data);
4180 break;
4181 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004182 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 break;
4184 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004185 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004187 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004188 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004189 (!msr_info->host_initiated &&
4190 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004191 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004192 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004193 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004194 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004195 vmcs_write64(GUEST_BNDCFGS, data);
4196 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004197 case MSR_IA32_SPEC_CTRL:
4198 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004199 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4200 return 1;
4201
4202 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004203 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004204 return 1;
4205
4206 vmx->spec_ctrl = data;
4207
4208 if (!data)
4209 break;
4210
4211 /*
4212 * For non-nested:
4213 * When it's written (to non-zero) for the first time, pass
4214 * it through.
4215 *
4216 * For nested:
4217 * The handling of the MSR bitmap for L2 guests is done in
4218 * nested_vmx_merge_msr_bitmap. We should not touch the
4219 * vmcs02.msr_bitmap here since it gets completely overwritten
4220 * in the merging. We update the vmcs01 here for L1 as well
4221 * since it will end up touching the MSR anyway now.
4222 */
4223 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4224 MSR_IA32_SPEC_CTRL,
4225 MSR_TYPE_RW);
4226 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004227 case MSR_IA32_PRED_CMD:
4228 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004229 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4230 return 1;
4231
4232 if (data & ~PRED_CMD_IBPB)
4233 return 1;
4234
4235 if (!data)
4236 break;
4237
4238 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4239
4240 /*
4241 * For non-nested:
4242 * When it's written (to non-zero) for the first time, pass
4243 * it through.
4244 *
4245 * For nested:
4246 * The handling of the MSR bitmap for L2 guests is done in
4247 * nested_vmx_merge_msr_bitmap. We should not touch the
4248 * vmcs02.msr_bitmap here since it gets completely overwritten
4249 * in the merging.
4250 */
4251 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4252 MSR_TYPE_W);
4253 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004254 case MSR_IA32_ARCH_CAPABILITIES:
4255 if (!msr_info->host_initiated)
4256 return 1;
4257 vmx->arch_capabilities = data;
4258 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004259 case MSR_IA32_CR_PAT:
4260 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004261 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4262 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004263 vmcs_write64(GUEST_IA32_PAT, data);
4264 vcpu->arch.pat = data;
4265 break;
4266 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004267 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004268 break;
Will Auldba904632012-11-29 12:42:50 -08004269 case MSR_IA32_TSC_ADJUST:
4270 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004271 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004272 case MSR_IA32_MCG_EXT_CTL:
4273 if ((!msr_info->host_initiated &&
4274 !(to_vmx(vcpu)->msr_ia32_feature_control &
4275 FEATURE_CONTROL_LMCE)) ||
4276 (data & ~MCG_EXT_CTL_LMCE_EN))
4277 return 1;
4278 vcpu->arch.mcg_ext_ctl = data;
4279 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004280 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004281 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004282 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004283 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4284 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004285 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004286 if (msr_info->host_initiated && data == 0)
4287 vmx_leave_nested(vcpu);
4288 break;
4289 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004290 if (!msr_info->host_initiated)
4291 return 1; /* they are read-only */
4292 if (!nested_vmx_allowed(vcpu))
4293 return 1;
4294 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004295 case MSR_IA32_XSS:
4296 if (!vmx_xsaves_supported())
4297 return 1;
4298 /*
4299 * The only supported bit as of Skylake is bit 8, but
4300 * it is not supported on KVM.
4301 */
4302 if (data != 0)
4303 return 1;
4304 vcpu->arch.ia32_xss = data;
4305 if (vcpu->arch.ia32_xss != host_xss)
4306 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004307 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004308 else
4309 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4310 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004311 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004312 if (!msr_info->host_initiated &&
4313 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004314 return 1;
4315 /* Check reserved bit, higher 32 bits should be zero */
4316 if ((data >> 32) != 0)
4317 return 1;
4318 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004320 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004321 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004322 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004323 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004324 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4325 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004326 ret = kvm_set_shared_msr(msr->index, msr->data,
4327 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004328 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004329 if (ret)
4330 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004331 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004332 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004334 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335 }
4336
Eddie Dong2cc51562007-05-21 07:28:09 +03004337 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338}
4339
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004340static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004342 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4343 switch (reg) {
4344 case VCPU_REGS_RSP:
4345 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4346 break;
4347 case VCPU_REGS_RIP:
4348 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4349 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004350 case VCPU_EXREG_PDPTR:
4351 if (enable_ept)
4352 ept_save_pdptrs(vcpu);
4353 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004354 default:
4355 break;
4356 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357}
4358
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359static __init int cpu_has_kvm_support(void)
4360{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004361 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362}
4363
4364static __init int vmx_disabled_by_bios(void)
4365{
4366 u64 msr;
4367
4368 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004369 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004370 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004371 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4372 && tboot_enabled())
4373 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004374 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004375 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004376 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004377 && !tboot_enabled()) {
4378 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004379 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004380 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004381 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004382 /* launched w/o TXT and VMX disabled */
4383 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4384 && !tboot_enabled())
4385 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004386 }
4387
4388 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004389}
4390
Dongxiao Xu7725b892010-05-11 18:29:38 +08004391static void kvm_cpu_vmxon(u64 addr)
4392{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004393 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004394 intel_pt_handle_vmx(1);
4395
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004396 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08004397}
4398
Radim Krčmář13a34e02014-08-28 15:13:03 +02004399static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400{
4401 int cpu = raw_smp_processor_id();
4402 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004403 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004404
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004405 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004406 return -EBUSY;
4407
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004408 /*
4409 * This can happen if we hot-added a CPU but failed to allocate
4410 * VP assist page for it.
4411 */
4412 if (static_branch_unlikely(&enable_evmcs) &&
4413 !hv_get_vp_assist_page(cpu))
4414 return -EFAULT;
4415
Nadav Har'Eld462b812011-05-24 15:26:10 +03004416 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004417 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4418 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004419
4420 /*
4421 * Now we can enable the vmclear operation in kdump
4422 * since the loaded_vmcss_on_cpu list on this cpu
4423 * has been initialized.
4424 *
4425 * Though the cpu is not in VMX operation now, there
4426 * is no problem to enable the vmclear operation
4427 * for the loaded_vmcss_on_cpu list is empty!
4428 */
4429 crash_enable_local_vmclear(cpu);
4430
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004432
4433 test_bits = FEATURE_CONTROL_LOCKED;
4434 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4435 if (tboot_enabled())
4436 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4437
4438 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004440 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4441 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004442 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004443 if (enable_ept)
4444 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004445
4446 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004447}
4448
Nadav Har'Eld462b812011-05-24 15:26:10 +03004449static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004450{
4451 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004452 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004453
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4455 loaded_vmcss_on_cpu_link)
4456 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004457}
4458
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004459
4460/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4461 * tricks.
4462 */
4463static void kvm_cpu_vmxoff(void)
4464{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004465 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004466
4467 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004468 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004469}
4470
Radim Krčmář13a34e02014-08-28 15:13:03 +02004471static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004473 vmclear_local_loaded_vmcss();
4474 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475}
4476
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004477static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004478 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004479{
4480 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004481 u32 ctl = ctl_min | ctl_opt;
4482
4483 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4484
4485 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4486 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4487
4488 /* Ensure minimum (required) set of control bits are supported. */
4489 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004490 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004491
4492 *result = ctl;
4493 return 0;
4494}
4495
Avi Kivity110312c2010-12-21 12:54:20 +02004496static __init bool allow_1_setting(u32 msr, u32 ctl)
4497{
4498 u32 vmx_msr_low, vmx_msr_high;
4499
4500 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4501 return vmx_msr_high & ctl;
4502}
4503
Yang, Sheng002c7f72007-07-31 14:23:01 +03004504static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004505{
4506 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004507 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004508 u32 _pin_based_exec_control = 0;
4509 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004510 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004511 u32 _vmexit_control = 0;
4512 u32 _vmentry_control = 0;
4513
Paolo Bonzini13893092018-02-26 13:40:09 +01004514 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304515 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516#ifdef CONFIG_X86_64
4517 CPU_BASED_CR8_LOAD_EXITING |
4518 CPU_BASED_CR8_STORE_EXITING |
4519#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004520 CPU_BASED_CR3_LOAD_EXITING |
4521 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004522 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004523 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004524 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004525 CPU_BASED_MWAIT_EXITING |
4526 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004527 CPU_BASED_INVLPG_EXITING |
4528 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004529
Sheng Yangf78e0e22007-10-29 09:40:42 +08004530 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004531 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004532 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004533 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4534 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004535 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004536#ifdef CONFIG_X86_64
4537 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4538 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4539 ~CPU_BASED_CR8_STORE_EXITING;
4540#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004541 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004542 min2 = 0;
4543 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004544 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004545 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004546 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004547 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004548 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004549 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004550 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004551 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004552 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004553 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004554 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004555 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004556 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004557 SECONDARY_EXEC_RDSEED_EXITING |
4558 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004559 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004560 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004561 SECONDARY_EXEC_ENABLE_VMFUNC |
4562 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004563 if (adjust_vmx_controls(min2, opt2,
4564 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004565 &_cpu_based_2nd_exec_control) < 0)
4566 return -EIO;
4567 }
4568#ifndef CONFIG_X86_64
4569 if (!(_cpu_based_2nd_exec_control &
4570 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4571 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4572#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004573
4574 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4575 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004576 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004577 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4578 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004579
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004580 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4581 &vmx_capability.ept, &vmx_capability.vpid);
4582
Sheng Yangd56f5462008-04-25 10:13:16 +08004583 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004584 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4585 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004586 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4587 CPU_BASED_CR3_STORE_EXITING |
4588 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004589 } else if (vmx_capability.ept) {
4590 vmx_capability.ept = 0;
4591 pr_warn_once("EPT CAP should not exist if not support "
4592 "1-setting enable EPT VM-execution control\n");
4593 }
4594 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4595 vmx_capability.vpid) {
4596 vmx_capability.vpid = 0;
4597 pr_warn_once("VPID CAP should not exist if not support "
4598 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004599 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004600
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004601 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004602#ifdef CONFIG_X86_64
4603 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4604#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004605 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004606 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004607 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4608 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004609 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004610
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004611 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4612 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4613 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004614 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4615 &_pin_based_exec_control) < 0)
4616 return -EIO;
4617
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004618 if (cpu_has_broken_vmx_preemption_timer())
4619 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004620 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004621 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004622 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4623
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004624 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004625 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004626 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4627 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004628 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004630 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631
4632 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4633 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004634 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004635
4636#ifdef CONFIG_X86_64
4637 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4638 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004639 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004640#endif
4641
4642 /* Require Write-Back (WB) memory type for VMCS accesses. */
4643 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645
Yang, Sheng002c7f72007-07-31 14:23:01 +03004646 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004647 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004648 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004649
Liran Alon2307af12018-06-29 22:59:04 +03004650 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004651
Yang, Sheng002c7f72007-07-31 14:23:01 +03004652 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4653 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004654 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004655 vmcs_conf->vmexit_ctrl = _vmexit_control;
4656 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004657
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004658 if (static_branch_unlikely(&enable_evmcs))
4659 evmcs_sanitize_exec_ctrls(vmcs_conf);
4660
Avi Kivity110312c2010-12-21 12:54:20 +02004661 cpu_has_load_ia32_efer =
4662 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4663 VM_ENTRY_LOAD_IA32_EFER)
4664 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4665 VM_EXIT_LOAD_IA32_EFER);
4666
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004667 cpu_has_load_perf_global_ctrl =
4668 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4669 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4670 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4671 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4672
4673 /*
4674 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004675 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004676 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4677 *
4678 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4679 *
4680 * AAK155 (model 26)
4681 * AAP115 (model 30)
4682 * AAT100 (model 37)
4683 * BC86,AAY89,BD102 (model 44)
4684 * BA97 (model 46)
4685 *
4686 */
4687 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4688 switch (boot_cpu_data.x86_model) {
4689 case 26:
4690 case 30:
4691 case 37:
4692 case 44:
4693 case 46:
4694 cpu_has_load_perf_global_ctrl = false;
4695 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4696 "does not work properly. Using workaround\n");
4697 break;
4698 default:
4699 break;
4700 }
4701 }
4702
Borislav Petkov782511b2016-04-04 22:25:03 +02004703 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004704 rdmsrl(MSR_IA32_XSS, host_xss);
4705
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004706 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004707}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708
Liran Alon491a6032018-06-23 02:35:12 +03004709static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710{
4711 int node = cpu_to_node(cpu);
4712 struct page *pages;
4713 struct vmcs *vmcs;
4714
Vlastimil Babka96db8002015-09-08 15:03:50 -07004715 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716 if (!pages)
4717 return NULL;
4718 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004719 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004720
4721 /* KVM supports Enlightened VMCS v1 only */
4722 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004723 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004724 else
Liran Alon392b2f22018-06-23 02:35:01 +03004725 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004726
Liran Alon491a6032018-06-23 02:35:12 +03004727 if (shadow)
4728 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729 return vmcs;
4730}
4731
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732static void free_vmcs(struct vmcs *vmcs)
4733{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004734 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735}
4736
Nadav Har'Eld462b812011-05-24 15:26:10 +03004737/*
4738 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4739 */
4740static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4741{
4742 if (!loaded_vmcs->vmcs)
4743 return;
4744 loaded_vmcs_clear(loaded_vmcs);
4745 free_vmcs(loaded_vmcs->vmcs);
4746 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004747 if (loaded_vmcs->msr_bitmap)
4748 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004749 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004750}
4751
Liran Alon491a6032018-06-23 02:35:12 +03004752static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004753{
Liran Alon491a6032018-06-23 02:35:12 +03004754 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004755}
4756
4757static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4758{
Liran Alon491a6032018-06-23 02:35:12 +03004759 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004760 if (!loaded_vmcs->vmcs)
4761 return -ENOMEM;
4762
4763 loaded_vmcs->shadow_vmcs = NULL;
4764 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004765
4766 if (cpu_has_vmx_msr_bitmap()) {
4767 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4768 if (!loaded_vmcs->msr_bitmap)
4769 goto out_vmcs;
4770 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004771
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004772 if (IS_ENABLED(CONFIG_HYPERV) &&
4773 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004774 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4775 struct hv_enlightened_vmcs *evmcs =
4776 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4777
4778 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4779 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004780 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004781
4782 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4783
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004784 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004785
4786out_vmcs:
4787 free_loaded_vmcs(loaded_vmcs);
4788 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004789}
4790
Sam Ravnborg39959582007-06-01 00:47:13 -07004791static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792{
4793 int cpu;
4794
Zachary Amsden3230bb42009-09-29 11:38:37 -10004795 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004797 per_cpu(vmxarea, cpu) = NULL;
4798 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799}
4800
Jim Mattsond37f4262017-12-22 12:12:16 -08004801enum vmcs_field_width {
4802 VMCS_FIELD_WIDTH_U16 = 0,
4803 VMCS_FIELD_WIDTH_U64 = 1,
4804 VMCS_FIELD_WIDTH_U32 = 2,
4805 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004806};
4807
Jim Mattsond37f4262017-12-22 12:12:16 -08004808static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004809{
4810 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004811 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004812 return (field >> 13) & 0x3 ;
4813}
4814
4815static inline int vmcs_field_readonly(unsigned long field)
4816{
4817 return (((field >> 10) & 0x3) == 1);
4818}
4819
Bandan Dasfe2b2012014-04-21 15:20:14 -04004820static void init_vmcs_shadow_fields(void)
4821{
4822 int i, j;
4823
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004824 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4825 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004826 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004827 (i + 1 == max_shadow_read_only_fields ||
4828 shadow_read_only_fields[i + 1] != field + 1))
4829 pr_err("Missing field from shadow_read_only_field %x\n",
4830 field + 1);
4831
4832 clear_bit(field, vmx_vmread_bitmap);
4833#ifdef CONFIG_X86_64
4834 if (field & 1)
4835 continue;
4836#endif
4837 if (j < i)
4838 shadow_read_only_fields[j] = field;
4839 j++;
4840 }
4841 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004842
4843 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004844 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004845 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004846 (i + 1 == max_shadow_read_write_fields ||
4847 shadow_read_write_fields[i + 1] != field + 1))
4848 pr_err("Missing field from shadow_read_write_field %x\n",
4849 field + 1);
4850
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004851 /*
4852 * PML and the preemption timer can be emulated, but the
4853 * processor cannot vmwrite to fields that don't exist
4854 * on bare metal.
4855 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004856 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004857 case GUEST_PML_INDEX:
4858 if (!cpu_has_vmx_pml())
4859 continue;
4860 break;
4861 case VMX_PREEMPTION_TIMER_VALUE:
4862 if (!cpu_has_vmx_preemption_timer())
4863 continue;
4864 break;
4865 case GUEST_INTR_STATUS:
4866 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004867 continue;
4868 break;
4869 default:
4870 break;
4871 }
4872
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004873 clear_bit(field, vmx_vmwrite_bitmap);
4874 clear_bit(field, vmx_vmread_bitmap);
4875#ifdef CONFIG_X86_64
4876 if (field & 1)
4877 continue;
4878#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004879 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004880 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004881 j++;
4882 }
4883 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004884}
4885
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886static __init int alloc_kvm_area(void)
4887{
4888 int cpu;
4889
Zachary Amsden3230bb42009-09-29 11:38:37 -10004890 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891 struct vmcs *vmcs;
4892
Liran Alon491a6032018-06-23 02:35:12 +03004893 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 if (!vmcs) {
4895 free_kvm_area();
4896 return -ENOMEM;
4897 }
4898
Liran Alon2307af12018-06-29 22:59:04 +03004899 /*
4900 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4901 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4902 * revision_id reported by MSR_IA32_VMX_BASIC.
4903 *
4904 * However, even though not explictly documented by
4905 * TLFS, VMXArea passed as VMXON argument should
4906 * still be marked with revision_id reported by
4907 * physical CPU.
4908 */
4909 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004910 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004911
Avi Kivity6aa8b732006-12-10 02:21:36 -08004912 per_cpu(vmxarea, cpu) = vmcs;
4913 }
4914 return 0;
4915}
4916
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004917static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004918 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004919{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004920 if (!emulate_invalid_guest_state) {
4921 /*
4922 * CS and SS RPL should be equal during guest entry according
4923 * to VMX spec, but in reality it is not always so. Since vcpu
4924 * is in the middle of the transition from real mode to
4925 * protected mode it is safe to assume that RPL 0 is a good
4926 * default value.
4927 */
4928 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004929 save->selector &= ~SEGMENT_RPL_MASK;
4930 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004931 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004932 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004933 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004934}
4935
4936static void enter_pmode(struct kvm_vcpu *vcpu)
4937{
4938 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004939 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940
Gleb Natapovd99e4152012-12-20 16:57:45 +02004941 /*
4942 * Update real mode segment cache. It may be not up-to-date if sement
4943 * register was written while vcpu was in a guest mode.
4944 */
4945 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4948 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4949 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4951
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004952 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953
Avi Kivity2fb92db2011-04-27 19:42:18 +03004954 vmx_segment_cache_clear(vmx);
4955
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004956 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957
4958 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004959 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4960 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961 vmcs_writel(GUEST_RFLAGS, flags);
4962
Rusty Russell66aee912007-07-17 23:34:16 +10004963 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4964 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965
4966 update_exception_bitmap(vcpu);
4967
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004968 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4969 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4970 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4971 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4972 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4973 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974}
4975
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004976static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977{
Mathias Krause772e0312012-08-30 01:30:19 +02004978 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004979 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980
Gleb Natapovd99e4152012-12-20 16:57:45 +02004981 var.dpl = 0x3;
4982 if (seg == VCPU_SREG_CS)
4983 var.type = 0x3;
4984
4985 if (!emulate_invalid_guest_state) {
4986 var.selector = var.base >> 4;
4987 var.base = var.base & 0xffff0;
4988 var.limit = 0xffff;
4989 var.g = 0;
4990 var.db = 0;
4991 var.present = 1;
4992 var.s = 1;
4993 var.l = 0;
4994 var.unusable = 0;
4995 var.type = 0x3;
4996 var.avl = 0;
4997 if (save->base & 0xf)
4998 printk_once(KERN_WARNING "kvm: segment base is not "
4999 "paragraph aligned when entering "
5000 "protected mode (seg=%d)", seg);
5001 }
5002
5003 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005004 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005005 vmcs_write32(sf->limit, var.limit);
5006 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007}
5008
5009static void enter_rmode(struct kvm_vcpu *vcpu)
5010{
5011 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005012 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005013 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5019 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005022
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005023 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005024
Gleb Natapov776e58e2011-03-13 12:34:27 +02005025 /*
5026 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005027 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005028 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005029 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005030 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5031 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005032
Avi Kivity2fb92db2011-04-27 19:42:18 +03005033 vmx_segment_cache_clear(vmx);
5034
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005035 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5038
5039 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005040 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005042 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005043
5044 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005045 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 update_exception_bitmap(vcpu);
5047
Gleb Natapovd99e4152012-12-20 16:57:45 +02005048 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5049 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5050 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5051 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5052 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5053 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005054
Eddie Dong8668a3c2007-10-10 14:26:45 +08005055 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056}
5057
Amit Shah401d10d2009-02-20 22:53:37 +05305058static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5059{
5060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005061 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5062
5063 if (!msr)
5064 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305065
Avi Kivityf6801df2010-01-21 15:31:50 +02005066 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305067 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005068 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305069 msr->data = efer;
5070 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005071 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305072
5073 msr->data = efer & ~EFER_LME;
5074 }
5075 setup_msrs(vmx);
5076}
5077
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005078#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079
5080static void enter_lmode(struct kvm_vcpu *vcpu)
5081{
5082 u32 guest_tr_ar;
5083
Avi Kivity2fb92db2011-04-27 19:42:18 +03005084 vmx_segment_cache_clear(to_vmx(vcpu));
5085
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005087 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005088 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5089 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005090 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005091 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5092 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005093 }
Avi Kivityda38f432010-07-06 11:30:49 +03005094 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095}
5096
5097static void exit_lmode(struct kvm_vcpu *vcpu)
5098{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005099 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005100 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005101}
5102
5103#endif
5104
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005105static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5106 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005107{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005108 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005109 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005110 return;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005111 ept_sync_context(construct_eptp(vcpu,
5112 vcpu->arch.mmu->root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005113 } else {
5114 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005115 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005116}
5117
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005118static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005119{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005120 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005121}
5122
Junaid Shahidfaff8752018-06-29 13:10:05 -07005123static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5124{
5125 int vpid = to_vmx(vcpu)->vpid;
5126
5127 if (!vpid_sync_vcpu_addr(vpid, addr))
5128 vpid_sync_context(vpid);
5129
5130 /*
5131 * If VPIDs are not supported or enabled, then the above is a no-op.
5132 * But we don't really need a TLB flush in that case anyway, because
5133 * each VM entry/exit includes an implicit flush when VPID is 0.
5134 */
5135}
5136
Avi Kivitye8467fd2009-12-29 18:43:06 +02005137static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5138{
5139 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5140
5141 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5142 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5143}
5144
Avi Kivityaff48ba2010-12-05 18:56:11 +02005145static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5146{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005147 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005148 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5149 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5150}
5151
Anthony Liguori25c4c272007-04-27 09:29:21 +03005152static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005153{
Avi Kivityfc78f512009-12-07 12:16:48 +02005154 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5155
5156 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5157 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005158}
5159
Sheng Yang14394422008-04-28 12:24:45 +08005160static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5161{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005162 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5163
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005164 if (!test_bit(VCPU_EXREG_PDPTR,
5165 (unsigned long *)&vcpu->arch.regs_dirty))
5166 return;
5167
Sheng Yang14394422008-04-28 12:24:45 +08005168 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005169 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5170 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5171 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5172 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005173 }
5174}
5175
Avi Kivity8f5d5492009-05-31 18:41:29 +03005176static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5177{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005178 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5179
Avi Kivity8f5d5492009-05-31 18:41:29 +03005180 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005181 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5182 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5183 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5184 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005185 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005186
5187 __set_bit(VCPU_EXREG_PDPTR,
5188 (unsigned long *)&vcpu->arch.regs_avail);
5189 __set_bit(VCPU_EXREG_PDPTR,
5190 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005191}
5192
David Matlack38991522016-11-29 18:14:08 -08005193static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5194{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005195 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5196 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005197 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5198
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005199 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005200 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5201 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5202 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5203
5204 return fixed_bits_valid(val, fixed0, fixed1);
5205}
5206
5207static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5208{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005209 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5210 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005211
5212 return fixed_bits_valid(val, fixed0, fixed1);
5213}
5214
5215static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5216{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005217 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5218 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005219
5220 return fixed_bits_valid(val, fixed0, fixed1);
5221}
5222
5223/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5224#define nested_guest_cr4_valid nested_cr4_valid
5225#define nested_host_cr4_valid nested_cr4_valid
5226
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005227static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005228
5229static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5230 unsigned long cr0,
5231 struct kvm_vcpu *vcpu)
5232{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005233 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5234 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005235 if (!(cr0 & X86_CR0_PG)) {
5236 /* From paging/starting to nonpaging */
5237 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005238 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005239 (CPU_BASED_CR3_LOAD_EXITING |
5240 CPU_BASED_CR3_STORE_EXITING));
5241 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005242 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005243 } else if (!is_paging(vcpu)) {
5244 /* From nonpaging to paging */
5245 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005246 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005247 ~(CPU_BASED_CR3_LOAD_EXITING |
5248 CPU_BASED_CR3_STORE_EXITING));
5249 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005250 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005251 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005252
5253 if (!(cr0 & X86_CR0_WP))
5254 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005255}
5256
Avi Kivity6aa8b732006-12-10 02:21:36 -08005257static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5258{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005259 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005260 unsigned long hw_cr0;
5261
Sean Christopherson3de63472018-07-13 08:42:30 -07005262 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005263 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005264 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005265 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005266 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005267
Gleb Natapov218e7632013-01-21 15:36:45 +02005268 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5269 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005270
Gleb Natapov218e7632013-01-21 15:36:45 +02005271 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5272 enter_rmode(vcpu);
5273 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005275#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005276 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005277 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005279 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005280 exit_lmode(vcpu);
5281 }
5282#endif
5283
Sean Christophersonb4d18512018-03-05 12:04:40 -08005284 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005285 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5286
Avi Kivity6aa8b732006-12-10 02:21:36 -08005287 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005288 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005289 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005290
5291 /* depends on vcpu->arch.cr0 to be set to a new value */
5292 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005293}
5294
Yu Zhang855feb62017-08-24 20:27:55 +08005295static int get_ept_level(struct kvm_vcpu *vcpu)
5296{
5297 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5298 return 5;
5299 return 4;
5300}
5301
Peter Feiner995f00a2017-06-30 17:26:32 -07005302static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005303{
Yu Zhang855feb62017-08-24 20:27:55 +08005304 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005305
Yu Zhang855feb62017-08-24 20:27:55 +08005306 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005307
Peter Feiner995f00a2017-06-30 17:26:32 -07005308 if (enable_ept_ad_bits &&
5309 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005310 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005311 eptp |= (root_hpa & PAGE_MASK);
5312
5313 return eptp;
5314}
5315
Avi Kivity6aa8b732006-12-10 02:21:36 -08005316static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5317{
Tianyu Lan877ad952018-07-19 08:40:23 +00005318 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005319 unsigned long guest_cr3;
5320 u64 eptp;
5321
5322 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005323 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005324 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005325 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005326
5327 if (kvm_x86_ops->tlb_remote_flush) {
5328 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5329 to_vmx(vcpu)->ept_pointer = eptp;
5330 to_kvm_vmx(kvm)->ept_pointers_match
5331 = EPT_POINTERS_CHECK;
5332 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5333 }
5334
Sean Christophersone90008d2018-03-05 12:04:37 -08005335 if (enable_unrestricted_guest || is_paging(vcpu) ||
5336 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005337 guest_cr3 = kvm_read_cr3(vcpu);
5338 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005339 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005340 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005341 }
5342
Sheng Yang14394422008-04-28 12:24:45 +08005343 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005344}
5345
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005346static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005347{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005348 /*
5349 * Pass through host's Machine Check Enable value to hw_cr4, which
5350 * is in force while we are in guest mode. Do not let guests control
5351 * this bit, even if host CR4.MCE == 0.
5352 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005353 unsigned long hw_cr4;
5354
5355 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5356 if (enable_unrestricted_guest)
5357 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5358 else if (to_vmx(vcpu)->rmode.vm86_active)
5359 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5360 else
5361 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005362
Sean Christopherson64f7a112018-04-30 10:01:06 -07005363 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5364 if (cr4 & X86_CR4_UMIP) {
5365 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005366 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005367 hw_cr4 &= ~X86_CR4_UMIP;
5368 } else if (!is_guest_mode(vcpu) ||
5369 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5370 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5371 SECONDARY_EXEC_DESC);
5372 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005373
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005374 if (cr4 & X86_CR4_VMXE) {
5375 /*
5376 * To use VMXON (and later other VMX instructions), a guest
5377 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5378 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005379 * is here. We operate under the default treatment of SMM,
5380 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005381 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005382 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005383 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005384 }
David Matlack38991522016-11-29 18:14:08 -08005385
5386 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005387 return 1;
5388
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005389 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005390
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005391 if (!enable_unrestricted_guest) {
5392 if (enable_ept) {
5393 if (!is_paging(vcpu)) {
5394 hw_cr4 &= ~X86_CR4_PAE;
5395 hw_cr4 |= X86_CR4_PSE;
5396 } else if (!(cr4 & X86_CR4_PAE)) {
5397 hw_cr4 &= ~X86_CR4_PAE;
5398 }
5399 }
5400
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005401 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005402 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5403 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5404 * to be manually disabled when guest switches to non-paging
5405 * mode.
5406 *
5407 * If !enable_unrestricted_guest, the CPU is always running
5408 * with CR0.PG=1 and CR4 needs to be modified.
5409 * If enable_unrestricted_guest, the CPU automatically
5410 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005411 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005412 if (!is_paging(vcpu))
5413 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5414 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005415
Sheng Yang14394422008-04-28 12:24:45 +08005416 vmcs_writel(CR4_READ_SHADOW, cr4);
5417 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005418 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005419}
5420
Avi Kivity6aa8b732006-12-10 02:21:36 -08005421static void vmx_get_segment(struct kvm_vcpu *vcpu,
5422 struct kvm_segment *var, int seg)
5423{
Avi Kivitya9179492011-01-03 14:28:52 +02005424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005425 u32 ar;
5426
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005427 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005428 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005429 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005430 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005431 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005432 var->base = vmx_read_guest_seg_base(vmx, seg);
5433 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5434 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005435 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005436 var->base = vmx_read_guest_seg_base(vmx, seg);
5437 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5438 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5439 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005440 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005441 var->type = ar & 15;
5442 var->s = (ar >> 4) & 1;
5443 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005444 /*
5445 * Some userspaces do not preserve unusable property. Since usable
5446 * segment has to be present according to VMX spec we can use present
5447 * property to amend userspace bug by making unusable segment always
5448 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5449 * segment as unusable.
5450 */
5451 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452 var->avl = (ar >> 12) & 1;
5453 var->l = (ar >> 13) & 1;
5454 var->db = (ar >> 14) & 1;
5455 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005456}
5457
Avi Kivitya9179492011-01-03 14:28:52 +02005458static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5459{
Avi Kivitya9179492011-01-03 14:28:52 +02005460 struct kvm_segment s;
5461
5462 if (to_vmx(vcpu)->rmode.vm86_active) {
5463 vmx_get_segment(vcpu, &s, seg);
5464 return s.base;
5465 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005466 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005467}
5468
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005469static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005470{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005471 struct vcpu_vmx *vmx = to_vmx(vcpu);
5472
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005473 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005474 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005475 else {
5476 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005477 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005478 }
Avi Kivity69c73022011-03-07 15:26:44 +02005479}
5480
Avi Kivity653e3102007-05-07 10:55:37 +03005481static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005482{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005483 u32 ar;
5484
Avi Kivityf0495f92012-06-07 17:06:10 +03005485 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005486 ar = 1 << 16;
5487 else {
5488 ar = var->type & 15;
5489 ar |= (var->s & 1) << 4;
5490 ar |= (var->dpl & 3) << 5;
5491 ar |= (var->present & 1) << 7;
5492 ar |= (var->avl & 1) << 12;
5493 ar |= (var->l & 1) << 13;
5494 ar |= (var->db & 1) << 14;
5495 ar |= (var->g & 1) << 15;
5496 }
Avi Kivity653e3102007-05-07 10:55:37 +03005497
5498 return ar;
5499}
5500
5501static void vmx_set_segment(struct kvm_vcpu *vcpu,
5502 struct kvm_segment *var, int seg)
5503{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005504 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005505 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005506
Avi Kivity2fb92db2011-04-27 19:42:18 +03005507 vmx_segment_cache_clear(vmx);
5508
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005509 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5510 vmx->rmode.segs[seg] = *var;
5511 if (seg == VCPU_SREG_TR)
5512 vmcs_write16(sf->selector, var->selector);
5513 else if (var->s)
5514 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005515 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005516 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005517
Avi Kivity653e3102007-05-07 10:55:37 +03005518 vmcs_writel(sf->base, var->base);
5519 vmcs_write32(sf->limit, var->limit);
5520 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005521
5522 /*
5523 * Fix the "Accessed" bit in AR field of segment registers for older
5524 * qemu binaries.
5525 * IA32 arch specifies that at the time of processor reset the
5526 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005527 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005528 * state vmexit when "unrestricted guest" mode is turned on.
5529 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5530 * tree. Newer qemu binaries with that qemu fix would not need this
5531 * kvm hack.
5532 */
5533 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005534 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005535
Gleb Natapovf924d662012-12-12 19:10:55 +02005536 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005537
5538out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005539 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005540}
5541
Avi Kivity6aa8b732006-12-10 02:21:36 -08005542static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5543{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005544 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545
5546 *db = (ar >> 14) & 1;
5547 *l = (ar >> 13) & 1;
5548}
5549
Gleb Natapov89a27f42010-02-16 10:51:48 +02005550static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005551{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005552 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5553 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005554}
5555
Gleb Natapov89a27f42010-02-16 10:51:48 +02005556static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005557{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005558 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5559 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005560}
5561
Gleb Natapov89a27f42010-02-16 10:51:48 +02005562static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005563{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005564 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5565 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005566}
5567
Gleb Natapov89a27f42010-02-16 10:51:48 +02005568static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005569{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005570 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5571 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005572}
5573
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005574static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5575{
5576 struct kvm_segment var;
5577 u32 ar;
5578
5579 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005580 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005581 if (seg == VCPU_SREG_CS)
5582 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005583 ar = vmx_segment_access_rights(&var);
5584
5585 if (var.base != (var.selector << 4))
5586 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005587 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005588 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005589 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005590 return false;
5591
5592 return true;
5593}
5594
5595static bool code_segment_valid(struct kvm_vcpu *vcpu)
5596{
5597 struct kvm_segment cs;
5598 unsigned int cs_rpl;
5599
5600 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005601 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005602
Avi Kivity1872a3f2009-01-04 23:26:52 +02005603 if (cs.unusable)
5604 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005605 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005606 return false;
5607 if (!cs.s)
5608 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005609 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005610 if (cs.dpl > cs_rpl)
5611 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005612 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005613 if (cs.dpl != cs_rpl)
5614 return false;
5615 }
5616 if (!cs.present)
5617 return false;
5618
5619 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5620 return true;
5621}
5622
5623static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5624{
5625 struct kvm_segment ss;
5626 unsigned int ss_rpl;
5627
5628 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005629 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005630
Avi Kivity1872a3f2009-01-04 23:26:52 +02005631 if (ss.unusable)
5632 return true;
5633 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005634 return false;
5635 if (!ss.s)
5636 return false;
5637 if (ss.dpl != ss_rpl) /* DPL != RPL */
5638 return false;
5639 if (!ss.present)
5640 return false;
5641
5642 return true;
5643}
5644
5645static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5646{
5647 struct kvm_segment var;
5648 unsigned int rpl;
5649
5650 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005651 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005652
Avi Kivity1872a3f2009-01-04 23:26:52 +02005653 if (var.unusable)
5654 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005655 if (!var.s)
5656 return false;
5657 if (!var.present)
5658 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005659 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005660 if (var.dpl < rpl) /* DPL < RPL */
5661 return false;
5662 }
5663
5664 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5665 * rights flags
5666 */
5667 return true;
5668}
5669
5670static bool tr_valid(struct kvm_vcpu *vcpu)
5671{
5672 struct kvm_segment tr;
5673
5674 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5675
Avi Kivity1872a3f2009-01-04 23:26:52 +02005676 if (tr.unusable)
5677 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005678 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005679 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005680 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005681 return false;
5682 if (!tr.present)
5683 return false;
5684
5685 return true;
5686}
5687
5688static bool ldtr_valid(struct kvm_vcpu *vcpu)
5689{
5690 struct kvm_segment ldtr;
5691
5692 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5693
Avi Kivity1872a3f2009-01-04 23:26:52 +02005694 if (ldtr.unusable)
5695 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005696 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005697 return false;
5698 if (ldtr.type != 2)
5699 return false;
5700 if (!ldtr.present)
5701 return false;
5702
5703 return true;
5704}
5705
5706static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5707{
5708 struct kvm_segment cs, ss;
5709
5710 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5711 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5712
Nadav Amitb32a9912015-03-29 16:33:04 +03005713 return ((cs.selector & SEGMENT_RPL_MASK) ==
5714 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005715}
5716
5717/*
5718 * Check if guest state is valid. Returns true if valid, false if
5719 * not.
5720 * We assume that registers are always usable
5721 */
5722static bool guest_state_valid(struct kvm_vcpu *vcpu)
5723{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005724 if (enable_unrestricted_guest)
5725 return true;
5726
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005727 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005728 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005729 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5730 return false;
5731 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5732 return false;
5733 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5734 return false;
5735 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5736 return false;
5737 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5738 return false;
5739 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5740 return false;
5741 } else {
5742 /* protected mode guest state checks */
5743 if (!cs_ss_rpl_check(vcpu))
5744 return false;
5745 if (!code_segment_valid(vcpu))
5746 return false;
5747 if (!stack_segment_valid(vcpu))
5748 return false;
5749 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5750 return false;
5751 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5752 return false;
5753 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5754 return false;
5755 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5756 return false;
5757 if (!tr_valid(vcpu))
5758 return false;
5759 if (!ldtr_valid(vcpu))
5760 return false;
5761 }
5762 /* TODO:
5763 * - Add checks on RIP
5764 * - Add checks on RFLAGS
5765 */
5766
5767 return true;
5768}
5769
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005770static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5771{
5772 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5773}
5774
Mike Dayd77c26f2007-10-08 09:02:08 -04005775static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005776{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005777 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005778 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005779 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005780
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005781 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005782 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005783 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5784 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005785 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005786 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005787 r = kvm_write_guest_page(kvm, fn++, &data,
5788 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005789 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005790 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005791 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5792 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005793 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005794 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5795 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005796 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005797 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005798 r = kvm_write_guest_page(kvm, fn, &data,
5799 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5800 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005801out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005802 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005803 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005804}
5805
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005806static int init_rmode_identity_map(struct kvm *kvm)
5807{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005808 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005809 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005810 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005811 u32 tmp;
5812
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005813 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005814 mutex_lock(&kvm->slots_lock);
5815
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005816 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005817 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005818
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005819 if (!kvm_vmx->ept_identity_map_addr)
5820 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5821 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005822
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005823 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005824 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005825 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005826 goto out2;
5827
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005828 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005829 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5830 if (r < 0)
5831 goto out;
5832 /* Set up identity-mapping pagetable for EPT in real mode */
5833 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5834 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5835 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5836 r = kvm_write_guest_page(kvm, identity_map_pfn,
5837 &tmp, i * sizeof(tmp), sizeof(tmp));
5838 if (r < 0)
5839 goto out;
5840 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005841 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005842
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005843out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005844 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005845
5846out2:
5847 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005848 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005849}
5850
Avi Kivity6aa8b732006-12-10 02:21:36 -08005851static void seg_setup(int seg)
5852{
Mathias Krause772e0312012-08-30 01:30:19 +02005853 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005854 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005855
5856 vmcs_write16(sf->selector, 0);
5857 vmcs_writel(sf->base, 0);
5858 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005859 ar = 0x93;
5860 if (seg == VCPU_SREG_CS)
5861 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005862
5863 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005864}
5865
Sheng Yangf78e0e22007-10-29 09:40:42 +08005866static int alloc_apic_access_page(struct kvm *kvm)
5867{
Xiao Guangrong44841412012-09-07 14:14:20 +08005868 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005869 int r = 0;
5870
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005871 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005872 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005873 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005874 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5875 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005876 if (r)
5877 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005878
Tang Chen73a6d942014-09-11 13:38:00 +08005879 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005880 if (is_error_page(page)) {
5881 r = -EFAULT;
5882 goto out;
5883 }
5884
Tang Chenc24ae0d2014-09-24 15:57:58 +08005885 /*
5886 * Do not pin the page in memory, so that memory hot-unplug
5887 * is able to migrate it.
5888 */
5889 put_page(page);
5890 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005891out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005892 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005893 return r;
5894}
5895
Wanpeng Li991e7a02015-09-16 17:30:05 +08005896static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005897{
5898 int vpid;
5899
Avi Kivity919818a2009-03-23 18:01:29 +02005900 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005901 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005902 spin_lock(&vmx_vpid_lock);
5903 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005904 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005905 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005906 else
5907 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005908 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005909 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005910}
5911
Wanpeng Li991e7a02015-09-16 17:30:05 +08005912static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005913{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005914 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005915 return;
5916 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005917 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005918 spin_unlock(&vmx_vpid_lock);
5919}
5920
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005921static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5922 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005923{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005924 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005925
5926 if (!cpu_has_vmx_msr_bitmap())
5927 return;
5928
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005929 if (static_branch_unlikely(&enable_evmcs))
5930 evmcs_touch_msr_bitmap();
5931
Sheng Yang25c5f222008-03-28 13:18:56 +08005932 /*
5933 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5934 * have the write-low and read-high bitmap offsets the wrong way round.
5935 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5936 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005937 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005938 if (type & MSR_TYPE_R)
5939 /* read-low */
5940 __clear_bit(msr, msr_bitmap + 0x000 / f);
5941
5942 if (type & MSR_TYPE_W)
5943 /* write-low */
5944 __clear_bit(msr, msr_bitmap + 0x800 / f);
5945
Sheng Yang25c5f222008-03-28 13:18:56 +08005946 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5947 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005948 if (type & MSR_TYPE_R)
5949 /* read-high */
5950 __clear_bit(msr, msr_bitmap + 0x400 / f);
5951
5952 if (type & MSR_TYPE_W)
5953 /* write-high */
5954 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5955
5956 }
5957}
5958
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005959static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5960 u32 msr, int type)
5961{
5962 int f = sizeof(unsigned long);
5963
5964 if (!cpu_has_vmx_msr_bitmap())
5965 return;
5966
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005967 if (static_branch_unlikely(&enable_evmcs))
5968 evmcs_touch_msr_bitmap();
5969
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005970 /*
5971 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5972 * have the write-low and read-high bitmap offsets the wrong way round.
5973 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5974 */
5975 if (msr <= 0x1fff) {
5976 if (type & MSR_TYPE_R)
5977 /* read-low */
5978 __set_bit(msr, msr_bitmap + 0x000 / f);
5979
5980 if (type & MSR_TYPE_W)
5981 /* write-low */
5982 __set_bit(msr, msr_bitmap + 0x800 / f);
5983
5984 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5985 msr &= 0x1fff;
5986 if (type & MSR_TYPE_R)
5987 /* read-high */
5988 __set_bit(msr, msr_bitmap + 0x400 / f);
5989
5990 if (type & MSR_TYPE_W)
5991 /* write-high */
5992 __set_bit(msr, msr_bitmap + 0xc00 / f);
5993
5994 }
5995}
5996
5997static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5998 u32 msr, int type, bool value)
5999{
6000 if (value)
6001 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6002 else
6003 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6004}
6005
Wincy Vanf2b93282015-02-03 23:56:03 +08006006/*
6007 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6008 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6009 */
6010static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6011 unsigned long *msr_bitmap_nested,
6012 u32 msr, int type)
6013{
6014 int f = sizeof(unsigned long);
6015
Wincy Vanf2b93282015-02-03 23:56:03 +08006016 /*
6017 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6018 * have the write-low and read-high bitmap offsets the wrong way round.
6019 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6020 */
6021 if (msr <= 0x1fff) {
6022 if (type & MSR_TYPE_R &&
6023 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6024 /* read-low */
6025 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6026
6027 if (type & MSR_TYPE_W &&
6028 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6029 /* write-low */
6030 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6031
6032 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6033 msr &= 0x1fff;
6034 if (type & MSR_TYPE_R &&
6035 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6036 /* read-high */
6037 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6038
6039 if (type & MSR_TYPE_W &&
6040 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6041 /* write-high */
6042 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6043
6044 }
6045}
6046
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006047static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006048{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006049 u8 mode = 0;
6050
6051 if (cpu_has_secondary_exec_ctrls() &&
6052 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6053 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6054 mode |= MSR_BITMAP_MODE_X2APIC;
6055 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6056 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6057 }
6058
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006059 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006060}
6061
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006062#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6063
6064static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6065 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006066{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006067 int msr;
6068
6069 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6070 unsigned word = msr / BITS_PER_LONG;
6071 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6072 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006073 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006074
6075 if (mode & MSR_BITMAP_MODE_X2APIC) {
6076 /*
6077 * TPR reads and writes can be virtualized even if virtual interrupt
6078 * delivery is not in use.
6079 */
6080 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6081 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6082 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6083 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6084 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6085 }
6086 }
6087}
6088
6089static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6090{
6091 struct vcpu_vmx *vmx = to_vmx(vcpu);
6092 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6093 u8 mode = vmx_msr_bitmap_mode(vcpu);
6094 u8 changed = mode ^ vmx->msr_bitmap_mode;
6095
6096 if (!changed)
6097 return;
6098
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006099 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6100 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6101
6102 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006103}
6104
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006105static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006106{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006107 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006108}
6109
David Matlackc9f04402017-08-01 14:00:40 -07006110static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6111{
6112 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6113 gfn_t gfn;
6114
6115 /*
6116 * Don't need to mark the APIC access page dirty; it is never
6117 * written to by the CPU during APIC virtualization.
6118 */
6119
6120 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6121 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6122 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6123 }
6124
6125 if (nested_cpu_has_posted_intr(vmcs12)) {
6126 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6127 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6128 }
6129}
6130
6131
David Hildenbrand6342c502017-01-25 11:58:58 +01006132static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006133{
6134 struct vcpu_vmx *vmx = to_vmx(vcpu);
6135 int max_irr;
6136 void *vapic_page;
6137 u16 status;
6138
David Matlackc9f04402017-08-01 14:00:40 -07006139 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6140 return;
Wincy Van705699a2015-02-03 23:58:17 +08006141
David Matlackc9f04402017-08-01 14:00:40 -07006142 vmx->nested.pi_pending = false;
6143 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6144 return;
Wincy Van705699a2015-02-03 23:58:17 +08006145
David Matlackc9f04402017-08-01 14:00:40 -07006146 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6147 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006148 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006149 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6150 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006151 kunmap(vmx->nested.virtual_apic_page);
6152
6153 status = vmcs_read16(GUEST_INTR_STATUS);
6154 if ((u8)max_irr > ((u8)status & 0xff)) {
6155 status &= ~0xff;
6156 status |= (u8)max_irr;
6157 vmcs_write16(GUEST_INTR_STATUS, status);
6158 }
6159 }
David Matlackc9f04402017-08-01 14:00:40 -07006160
6161 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006162}
6163
Paolo Bonzini7e712682018-10-03 13:44:26 +02006164static u8 vmx_get_rvi(void)
6165{
6166 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6167}
6168
Liran Alone6c67d82018-09-04 10:56:52 +03006169static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6170{
6171 struct vcpu_vmx *vmx = to_vmx(vcpu);
6172 void *vapic_page;
6173 u32 vppr;
6174 int rvi;
6175
6176 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6177 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6178 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6179 return false;
6180
Paolo Bonzini7e712682018-10-03 13:44:26 +02006181 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006182
6183 vapic_page = kmap(vmx->nested.virtual_apic_page);
6184 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6185 kunmap(vmx->nested.virtual_apic_page);
6186
6187 return ((rvi & 0xf0) > (vppr & 0xf0));
6188}
6189
Wincy Van06a55242017-04-28 13:13:59 +08006190static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6191 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006192{
6193#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006194 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6195
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006196 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006197 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006198 * The vector of interrupt to be delivered to vcpu had
6199 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006200 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006201 * Following cases will be reached in this block, and
6202 * we always send a notification event in all cases as
6203 * explained below.
6204 *
6205 * Case 1: vcpu keeps in non-root mode. Sending a
6206 * notification event posts the interrupt to vcpu.
6207 *
6208 * Case 2: vcpu exits to root mode and is still
6209 * runnable. PIR will be synced to vIRR before the
6210 * next vcpu entry. Sending a notification event in
6211 * this case has no effect, as vcpu is not in root
6212 * mode.
6213 *
6214 * Case 3: vcpu exits to root mode and is blocked.
6215 * vcpu_block() has already synced PIR to vIRR and
6216 * never blocks vcpu if vIRR is not cleared. Therefore,
6217 * a blocked vcpu here does not wait for any requested
6218 * interrupts in PIR, and sending a notification event
6219 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006220 */
Feng Wu28b835d2015-09-18 22:29:54 +08006221
Wincy Van06a55242017-04-28 13:13:59 +08006222 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006223 return true;
6224 }
6225#endif
6226 return false;
6227}
6228
Wincy Van705699a2015-02-03 23:58:17 +08006229static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6230 int vector)
6231{
6232 struct vcpu_vmx *vmx = to_vmx(vcpu);
6233
6234 if (is_guest_mode(vcpu) &&
6235 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006236 /*
6237 * If a posted intr is not recognized by hardware,
6238 * we will accomplish it in the next vmentry.
6239 */
6240 vmx->nested.pi_pending = true;
6241 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006242 /* the PIR and ON have been set by L1. */
6243 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6244 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006245 return 0;
6246 }
6247 return -1;
6248}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006249/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006250 * Send interrupt to vcpu via posted interrupt way.
6251 * 1. If target vcpu is running(non-root mode), send posted interrupt
6252 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6253 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6254 * interrupt from PIR in next vmentry.
6255 */
6256static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6257{
6258 struct vcpu_vmx *vmx = to_vmx(vcpu);
6259 int r;
6260
Wincy Van705699a2015-02-03 23:58:17 +08006261 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6262 if (!r)
6263 return;
6264
Yang Zhanga20ed542013-04-11 19:25:15 +08006265 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6266 return;
6267
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006268 /* If a previous notification has sent the IPI, nothing to do. */
6269 if (pi_test_and_set_on(&vmx->pi_desc))
6270 return;
6271
Wincy Van06a55242017-04-28 13:13:59 +08006272 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006273 kvm_vcpu_kick(vcpu);
6274}
6275
Avi Kivity6aa8b732006-12-10 02:21:36 -08006276/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006277 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6278 * will not change in the lifetime of the guest.
6279 * Note that host-state that does change is set elsewhere. E.g., host-state
6280 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6281 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006282static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006283{
6284 u32 low32, high32;
6285 unsigned long tmpl;
6286 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006287 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006288
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006289 cr0 = read_cr0();
6290 WARN_ON(cr0 & X86_CR0_TS);
6291 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006292
6293 /*
6294 * Save the most likely value for this task's CR3 in the VMCS.
6295 * We can't use __get_current_cr3_fast() because we're not atomic.
6296 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006297 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006298 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006299 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006300
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006301 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006302 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006303 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006304 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006305
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006306 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006307#ifdef CONFIG_X86_64
6308 /*
6309 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006310 * vmx_prepare_switch_to_host(), in case userspace uses
6311 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006312 */
6313 vmcs_write16(HOST_DS_SELECTOR, 0);
6314 vmcs_write16(HOST_ES_SELECTOR, 0);
6315#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006316 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6317 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006318#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006319 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6320 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6321
Juergen Gross87930012017-09-04 12:25:27 +02006322 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006323 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006324 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006325
Avi Kivity83287ea422012-09-16 15:10:57 +03006326 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006327
6328 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6329 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6330 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6331 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6332
6333 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6334 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6335 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6336 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07006337
6338 if (cpu_has_load_ia32_efer)
6339 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006340}
6341
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006342static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6343{
6344 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6345 if (enable_ept)
6346 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006347 if (is_guest_mode(&vmx->vcpu))
6348 vmx->vcpu.arch.cr4_guest_owned_bits &=
6349 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006350 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6351}
6352
Yang Zhang01e439b2013-04-11 19:25:12 +08006353static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6354{
6355 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6356
Andrey Smetanind62caab2015-11-10 15:36:33 +03006357 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006358 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006359
6360 if (!enable_vnmi)
6361 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6362
Yunhong Jiang64672c92016-06-13 14:19:59 -07006363 /* Enable the preemption timer dynamically */
6364 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006365 return pin_based_exec_ctrl;
6366}
6367
Andrey Smetanind62caab2015-11-10 15:36:33 +03006368static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6369{
6370 struct vcpu_vmx *vmx = to_vmx(vcpu);
6371
6372 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006373 if (cpu_has_secondary_exec_ctrls()) {
6374 if (kvm_vcpu_apicv_active(vcpu))
6375 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6376 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6377 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6378 else
6379 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6380 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6381 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6382 }
6383
6384 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006385 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006386}
6387
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006388static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6389{
6390 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006391
6392 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6393 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6394
Paolo Bonzini35754c92015-07-29 12:05:37 +02006395 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006396 exec_control &= ~CPU_BASED_TPR_SHADOW;
6397#ifdef CONFIG_X86_64
6398 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6399 CPU_BASED_CR8_LOAD_EXITING;
6400#endif
6401 }
6402 if (!enable_ept)
6403 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6404 CPU_BASED_CR3_LOAD_EXITING |
6405 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006406 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6407 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6408 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006409 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6410 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006411 return exec_control;
6412}
6413
Jim Mattson45ec3682017-08-23 16:32:04 -07006414static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006415{
Jim Mattson45ec3682017-08-23 16:32:04 -07006416 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006417 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006418}
6419
Jim Mattson75f4fc82017-08-23 16:32:03 -07006420static bool vmx_rdseed_supported(void)
6421{
6422 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006423 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006424}
6425
Paolo Bonzini80154d72017-08-24 13:55:35 +02006426static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006427{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006428 struct kvm_vcpu *vcpu = &vmx->vcpu;
6429
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006430 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006431
Paolo Bonzini80154d72017-08-24 13:55:35 +02006432 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006433 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6434 if (vmx->vpid == 0)
6435 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6436 if (!enable_ept) {
6437 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6438 enable_unrestricted_guest = 0;
6439 }
6440 if (!enable_unrestricted_guest)
6441 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006442 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006443 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006444 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006445 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6446 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006447 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006448
6449 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6450 * in vmx_set_cr4. */
6451 exec_control &= ~SECONDARY_EXEC_DESC;
6452
Abel Gordonabc4fc52013-04-18 14:35:25 +03006453 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6454 (handle_vmptrld).
6455 We can NOT enable shadow_vmcs here because we don't have yet
6456 a current VMCS12
6457 */
6458 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006459
6460 if (!enable_pml)
6461 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006462
Paolo Bonzini3db13482017-08-24 14:48:03 +02006463 if (vmx_xsaves_supported()) {
6464 /* Exposing XSAVES only when XSAVE is exposed */
6465 bool xsaves_enabled =
6466 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6467 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6468
6469 if (!xsaves_enabled)
6470 exec_control &= ~SECONDARY_EXEC_XSAVES;
6471
6472 if (nested) {
6473 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006474 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006475 SECONDARY_EXEC_XSAVES;
6476 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006477 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006478 ~SECONDARY_EXEC_XSAVES;
6479 }
6480 }
6481
Paolo Bonzini80154d72017-08-24 13:55:35 +02006482 if (vmx_rdtscp_supported()) {
6483 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6484 if (!rdtscp_enabled)
6485 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6486
6487 if (nested) {
6488 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006489 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006490 SECONDARY_EXEC_RDTSCP;
6491 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006492 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006493 ~SECONDARY_EXEC_RDTSCP;
6494 }
6495 }
6496
6497 if (vmx_invpcid_supported()) {
6498 /* Exposing INVPCID only when PCID is exposed */
6499 bool invpcid_enabled =
6500 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6501 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6502
6503 if (!invpcid_enabled) {
6504 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6505 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6506 }
6507
6508 if (nested) {
6509 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006510 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006511 SECONDARY_EXEC_ENABLE_INVPCID;
6512 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006513 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006514 ~SECONDARY_EXEC_ENABLE_INVPCID;
6515 }
6516 }
6517
Jim Mattson45ec3682017-08-23 16:32:04 -07006518 if (vmx_rdrand_supported()) {
6519 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6520 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006521 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006522
6523 if (nested) {
6524 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006525 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006526 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006527 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006528 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006529 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006530 }
6531 }
6532
Jim Mattson75f4fc82017-08-23 16:32:03 -07006533 if (vmx_rdseed_supported()) {
6534 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6535 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006536 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006537
6538 if (nested) {
6539 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006540 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006541 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006542 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006543 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006544 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006545 }
6546 }
6547
Paolo Bonzini80154d72017-08-24 13:55:35 +02006548 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006549}
6550
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006551static void ept_set_mmio_spte_mask(void)
6552{
6553 /*
6554 * EPT Misconfigurations can be generated if the value of bits 2:0
6555 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006556 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006557 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6558 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006559}
6560
Wanpeng Lif53cd632014-12-02 19:14:58 +08006561#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006562/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006563 * Sets up the vmcs for emulated real mode.
6564 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006565static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006566{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006567 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006568
Abel Gordon4607c2d2013-04-18 14:35:55 +03006569 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006570 /*
6571 * At vCPU creation, "VMWRITE to any supported field
6572 * in the VMCS" is supported, so use the more
6573 * permissive vmx_vmread_bitmap to specify both read
6574 * and write permissions for the shadow VMCS.
6575 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006576 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006577 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006578 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006579 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006580 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006581
Avi Kivity6aa8b732006-12-10 02:21:36 -08006582 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6583
Avi Kivity6aa8b732006-12-10 02:21:36 -08006584 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006585 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006586 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006587
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006588 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006589
Dan Williamsdfa169b2016-06-02 11:17:24 -07006590 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006591 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006592 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006593 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006594 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006595
Andrey Smetanind62caab2015-11-10 15:36:33 +03006596 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006597 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6598 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6599 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6600 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6601
6602 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006603
Li RongQing0bcf2612015-12-03 13:29:34 +08006604 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006605 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006606 }
6607
Wanpeng Lib31c1142018-03-12 04:53:04 -07006608 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006609 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006610 vmx->ple_window = ple_window;
6611 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006612 }
6613
Xiao Guangrongc3707952011-07-12 03:28:04 +08006614 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6615 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006616 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6617
Avi Kivity9581d442010-10-19 16:46:55 +02006618 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6619 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006620 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006621 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6622 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006623
Bandan Das2a499e42017-08-03 15:54:41 -04006624 if (cpu_has_vmx_vmfunc())
6625 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6626
Eddie Dong2cc51562007-05-21 07:28:09 +03006627 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6628 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006629 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006630 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006631 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006632
Radim Krčmář74545702015-04-27 15:11:25 +02006633 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6634 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006635
Paolo Bonzini03916db2014-07-24 14:21:57 +02006636 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637 u32 index = vmx_msr_index[i];
6638 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006639 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006640
6641 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6642 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006643 if (wrmsr_safe(index, data_low, data_high) < 0)
6644 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006645 vmx->guest_msrs[j].index = i;
6646 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006647 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006648 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006649 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006650
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006651 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006652
6653 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006654
6655 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006656 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006657
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006658 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6659 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6660
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006661 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006662
Wanpeng Lif53cd632014-12-02 19:14:58 +08006663 if (vmx_xsaves_supported())
6664 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6665
Peter Feiner4e595162016-07-07 14:49:58 -07006666 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006667 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6668 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6669 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006670
6671 if (cpu_has_vmx_encls_vmexit())
6672 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006673}
6674
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006675static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006676{
6677 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006678 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006679 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006680
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006681 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006682 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006683
Wanpeng Li518e7b92018-02-28 14:03:31 +08006684 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006685 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006686 kvm_set_cr8(vcpu, 0);
6687
6688 if (!init_event) {
6689 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6690 MSR_IA32_APICBASE_ENABLE;
6691 if (kvm_vcpu_is_reset_bsp(vcpu))
6692 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6693 apic_base_msr.host_initiated = true;
6694 kvm_set_apic_base(vcpu, &apic_base_msr);
6695 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006696
Avi Kivity2fb92db2011-04-27 19:42:18 +03006697 vmx_segment_cache_clear(vmx);
6698
Avi Kivity5706be02008-08-20 15:07:31 +03006699 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006700 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006701 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006702
6703 seg_setup(VCPU_SREG_DS);
6704 seg_setup(VCPU_SREG_ES);
6705 seg_setup(VCPU_SREG_FS);
6706 seg_setup(VCPU_SREG_GS);
6707 seg_setup(VCPU_SREG_SS);
6708
6709 vmcs_write16(GUEST_TR_SELECTOR, 0);
6710 vmcs_writel(GUEST_TR_BASE, 0);
6711 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6712 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6713
6714 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6715 vmcs_writel(GUEST_LDTR_BASE, 0);
6716 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6717 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6718
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006719 if (!init_event) {
6720 vmcs_write32(GUEST_SYSENTER_CS, 0);
6721 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6722 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6723 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6724 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006725
Wanpeng Lic37c2872017-11-20 14:52:21 -08006726 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006727 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006728
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006729 vmcs_writel(GUEST_GDTR_BASE, 0);
6730 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6731
6732 vmcs_writel(GUEST_IDTR_BASE, 0);
6733 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6734
Anthony Liguori443381a2010-12-06 10:53:38 -06006735 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006736 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006737 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006738 if (kvm_mpx_supported())
6739 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006740
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006741 setup_msrs(vmx);
6742
Avi Kivity6aa8b732006-12-10 02:21:36 -08006743 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6744
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006745 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006746 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006747 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006748 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006749 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006750 vmcs_write32(TPR_THRESHOLD, 0);
6751 }
6752
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006753 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006754
Sheng Yang2384d2b2008-01-17 15:14:33 +08006755 if (vmx->vpid != 0)
6756 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6757
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006758 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006759 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006760 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006761 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006762 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006763
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006764 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006765
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006766 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006767 if (init_event)
6768 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006769}
6770
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006771/*
6772 * In nested virtualization, check if L1 asked to exit on external interrupts.
6773 * For most existing hypervisors, this will always return true.
6774 */
6775static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6776{
6777 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6778 PIN_BASED_EXT_INTR_MASK;
6779}
6780
Bandan Das77b0f5d2014-04-19 18:17:45 -04006781/*
6782 * In nested virtualization, check if L1 has set
6783 * VM_EXIT_ACK_INTR_ON_EXIT
6784 */
6785static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6786{
6787 return get_vmcs12(vcpu)->vm_exit_controls &
6788 VM_EXIT_ACK_INTR_ON_EXIT;
6789}
6790
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006791static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6792{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006793 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006794}
6795
Jan Kiszkac9a79532014-03-07 20:03:15 +01006796static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006797{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006798 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6799 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006800}
6801
Jan Kiszkac9a79532014-03-07 20:03:15 +01006802static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006803{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006804 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006805 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006806 enable_irq_window(vcpu);
6807 return;
6808 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006809
Paolo Bonzini47c01522016-12-19 11:44:07 +01006810 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6811 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006812}
6813
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006814static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006815{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006816 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006817 uint32_t intr;
6818 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006819
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006820 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006821
Avi Kivityfa89a812008-09-01 15:57:51 +03006822 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006823 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006824 int inc_eip = 0;
6825 if (vcpu->arch.interrupt.soft)
6826 inc_eip = vcpu->arch.event_exit_inst_len;
6827 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006828 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006829 return;
6830 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006831 intr = irq | INTR_INFO_VALID_MASK;
6832 if (vcpu->arch.interrupt.soft) {
6833 intr |= INTR_TYPE_SOFT_INTR;
6834 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6835 vmx->vcpu.arch.event_exit_inst_len);
6836 } else
6837 intr |= INTR_TYPE_EXT_INTR;
6838 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006839
6840 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006841}
6842
Sheng Yangf08864b2008-05-15 18:23:25 +08006843static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6844{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006845 struct vcpu_vmx *vmx = to_vmx(vcpu);
6846
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006847 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006848 /*
6849 * Tracking the NMI-blocked state in software is built upon
6850 * finding the next open IRQ window. This, in turn, depends on
6851 * well-behaving guests: They have to keep IRQs disabled at
6852 * least as long as the NMI handler runs. Otherwise we may
6853 * cause NMI nesting, maybe breaking the guest. But as this is
6854 * highly unlikely, we can live with the residual risk.
6855 */
6856 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6857 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6858 }
6859
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006860 ++vcpu->stat.nmi_injections;
6861 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006862
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006863 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006864 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006865 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006866 return;
6867 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006868
Sheng Yangf08864b2008-05-15 18:23:25 +08006869 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6870 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006871
6872 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006873}
6874
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006875static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6876{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006877 struct vcpu_vmx *vmx = to_vmx(vcpu);
6878 bool masked;
6879
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006880 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006881 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006882 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006883 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006884 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6885 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6886 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006887}
6888
6889static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6890{
6891 struct vcpu_vmx *vmx = to_vmx(vcpu);
6892
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006893 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006894 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6895 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6896 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6897 }
6898 } else {
6899 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6900 if (masked)
6901 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6902 GUEST_INTR_STATE_NMI);
6903 else
6904 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6905 GUEST_INTR_STATE_NMI);
6906 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006907}
6908
Jan Kiszka2505dc92013-04-14 12:12:47 +02006909static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6910{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006911 if (to_vmx(vcpu)->nested.nested_run_pending)
6912 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006913
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006914 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006915 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6916 return 0;
6917
Jan Kiszka2505dc92013-04-14 12:12:47 +02006918 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6919 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6920 | GUEST_INTR_STATE_NMI));
6921}
6922
Gleb Natapov78646122009-03-23 12:12:11 +02006923static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6924{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006925 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6926 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006927 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6928 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006929}
6930
Izik Eiduscbc94022007-10-25 00:29:55 +02006931static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6932{
6933 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006934
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006935 if (enable_unrestricted_guest)
6936 return 0;
6937
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006938 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6939 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006940 if (ret)
6941 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006942 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006943 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006944}
6945
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006946static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6947{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006948 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006949 return 0;
6950}
6951
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006952static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006953{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006954 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006955 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006956 /*
6957 * Update instruction length as we may reinject the exception
6958 * from user space while in guest debugging mode.
6959 */
6960 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6961 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006962 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006963 return false;
6964 /* fall through */
6965 case DB_VECTOR:
6966 if (vcpu->guest_debug &
6967 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6968 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006969 /* fall through */
6970 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006971 case OF_VECTOR:
6972 case BR_VECTOR:
6973 case UD_VECTOR:
6974 case DF_VECTOR:
6975 case SS_VECTOR:
6976 case GP_VECTOR:
6977 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006978 return true;
6979 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006980 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006981 return false;
6982}
6983
6984static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6985 int vec, u32 err_code)
6986{
6987 /*
6988 * Instruction with address size override prefix opcode 0x67
6989 * Cause the #SS fault with 0 error code in VM86 mode.
6990 */
6991 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006992 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006993 if (vcpu->arch.halt_request) {
6994 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006995 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006996 }
6997 return 1;
6998 }
6999 return 0;
7000 }
7001
7002 /*
7003 * Forward all other exceptions that are valid in real mode.
7004 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7005 * the required debugging infrastructure rework.
7006 */
7007 kvm_queue_exception(vcpu, vec);
7008 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007009}
7010
Andi Kleena0861c02009-06-08 17:37:09 +08007011/*
7012 * Trigger machine check on the host. We assume all the MSRs are already set up
7013 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7014 * We pass a fake environment to the machine check handler because we want
7015 * the guest to be always treated like user space, no matter what context
7016 * it used internally.
7017 */
7018static void kvm_machine_check(void)
7019{
7020#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7021 struct pt_regs regs = {
7022 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7023 .flags = X86_EFLAGS_IF,
7024 };
7025
7026 do_machine_check(&regs, 0);
7027#endif
7028}
7029
Avi Kivity851ba692009-08-24 11:10:17 +03007030static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007031{
7032 /* already handled by vcpu_run */
7033 return 1;
7034}
7035
Avi Kivity851ba692009-08-24 11:10:17 +03007036static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007037{
Avi Kivity1155f762007-11-22 11:30:47 +02007038 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007039 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007040 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007041 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007042 u32 vect_info;
7043 enum emulation_result er;
7044
Avi Kivity1155f762007-11-22 11:30:47 +02007045 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007046 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007047
Andi Kleena0861c02009-06-08 17:37:09 +08007048 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007049 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007050
Jim Mattsonef85b672016-12-12 11:01:37 -08007051 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007052 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007053
Wanpeng Li082d06e2018-04-03 16:28:48 -07007054 if (is_invalid_opcode(intr_info))
7055 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007056
Avi Kivity6aa8b732006-12-10 02:21:36 -08007057 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007058 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007060
Liran Alon9e869482018-03-12 13:12:51 +02007061 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7062 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007063 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007064 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7065 if (er == EMULATE_USER_EXIT)
7066 return 0;
7067 else if (er != EMULATE_DONE)
7068 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7069 return 1;
7070 }
7071
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007072 /*
7073 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7074 * MMIO, it is better to report an internal error.
7075 * See the comments in vmx_handle_exit.
7076 */
7077 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7078 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7079 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7080 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007081 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007082 vcpu->run->internal.data[0] = vect_info;
7083 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007084 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007085 return 0;
7086 }
7087
Avi Kivity6aa8b732006-12-10 02:21:36 -08007088 if (is_page_fault(intr_info)) {
7089 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007090 /* EPT won't cause page fault directly */
7091 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007092 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007093 }
7094
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007095 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007096
7097 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7098 return handle_rmode_exception(vcpu, ex_no, error_code);
7099
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007100 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007101 case AC_VECTOR:
7102 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7103 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007104 case DB_VECTOR:
7105 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7106 if (!(vcpu->guest_debug &
7107 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007108 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007109 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007110 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007111 skip_emulated_instruction(vcpu);
7112
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007113 kvm_queue_exception(vcpu, DB_VECTOR);
7114 return 1;
7115 }
7116 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7117 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7118 /* fall through */
7119 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007120 /*
7121 * Update instruction length as we may reinject #BP from
7122 * user space while in guest debugging mode. Reading it for
7123 * #DB as well causes no harm, it is not used in that case.
7124 */
7125 vmx->vcpu.arch.event_exit_inst_len =
7126 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007128 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007129 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7130 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007131 break;
7132 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007133 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7134 kvm_run->ex.exception = ex_no;
7135 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007136 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007137 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007138 return 0;
7139}
7140
Avi Kivity851ba692009-08-24 11:10:17 +03007141static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007143 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007144 return 1;
7145}
7146
Avi Kivity851ba692009-08-24 11:10:17 +03007147static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007148{
Avi Kivity851ba692009-08-24 11:10:17 +03007149 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007150 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007151 return 0;
7152}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007153
Avi Kivity851ba692009-08-24 11:10:17 +03007154static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007155{
He, Qingbfdaab02007-09-12 14:18:28 +08007156 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007157 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007158 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007159
He, Qingbfdaab02007-09-12 14:18:28 +08007160 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007161 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007162
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007163 ++vcpu->stat.io_exits;
7164
Sean Christopherson432baf62018-03-08 08:57:26 -08007165 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007166 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007167
7168 port = exit_qualification >> 16;
7169 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007170 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007171
Sean Christophersondca7f122018-03-08 08:57:27 -08007172 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007173}
7174
Ingo Molnar102d8322007-02-19 14:37:47 +02007175static void
7176vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7177{
7178 /*
7179 * Patch in the VMCALL instruction:
7180 */
7181 hypercall[0] = 0x0f;
7182 hypercall[1] = 0x01;
7183 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007184}
7185
Guo Chao0fa06072012-06-28 15:16:19 +08007186/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007187static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7188{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007189 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007190 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7191 unsigned long orig_val = val;
7192
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007193 /*
7194 * We get here when L2 changed cr0 in a way that did not change
7195 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007196 * but did change L0 shadowed bits. So we first calculate the
7197 * effective cr0 value that L1 would like to write into the
7198 * hardware. It consists of the L2-owned bits from the new
7199 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007200 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007201 val = (val & ~vmcs12->cr0_guest_host_mask) |
7202 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7203
David Matlack38991522016-11-29 18:14:08 -08007204 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007205 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007206
7207 if (kvm_set_cr0(vcpu, val))
7208 return 1;
7209 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007210 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007211 } else {
7212 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007213 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007214 return 1;
David Matlack38991522016-11-29 18:14:08 -08007215
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007216 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007217 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007218}
7219
7220static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7221{
7222 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007223 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7224 unsigned long orig_val = val;
7225
7226 /* analogously to handle_set_cr0 */
7227 val = (val & ~vmcs12->cr4_guest_host_mask) |
7228 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7229 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007230 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007231 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007232 return 0;
7233 } else
7234 return kvm_set_cr4(vcpu, val);
7235}
7236
Paolo Bonzini0367f202016-07-12 10:44:55 +02007237static int handle_desc(struct kvm_vcpu *vcpu)
7238{
7239 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007240 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007241}
7242
Avi Kivity851ba692009-08-24 11:10:17 +03007243static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007244{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007245 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246 int cr;
7247 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007248 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007249 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007250
He, Qingbfdaab02007-09-12 14:18:28 +08007251 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007252 cr = exit_qualification & 15;
7253 reg = (exit_qualification >> 8) & 15;
7254 switch ((exit_qualification >> 4) & 3) {
7255 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007256 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007257 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007258 switch (cr) {
7259 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007260 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007261 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007262 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007263 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007264 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007265 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007266 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007267 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007268 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007269 case 8: {
7270 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007271 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007272 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007273 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007274 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007275 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007276 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007277 return ret;
7278 /*
7279 * TODO: we might be squashing a
7280 * KVM_GUESTDBG_SINGLESTEP-triggered
7281 * KVM_EXIT_DEBUG here.
7282 */
Avi Kivity851ba692009-08-24 11:10:17 +03007283 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007284 return 0;
7285 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007286 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007288 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007289 WARN_ONCE(1, "Guest should always own CR0.TS");
7290 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007291 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007292 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007293 case 1: /*mov from cr*/
7294 switch (cr) {
7295 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007296 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007297 val = kvm_read_cr3(vcpu);
7298 kvm_register_write(vcpu, reg, val);
7299 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007300 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007301 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007302 val = kvm_get_cr8(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 }
7307 break;
7308 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007309 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007310 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007311 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007312
Kyle Huey6affcbe2016-11-29 12:40:40 -08007313 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007314 default:
7315 break;
7316 }
Avi Kivity851ba692009-08-24 11:10:17 +03007317 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007318 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007319 (int)(exit_qualification >> 4) & 3, cr);
7320 return 0;
7321}
7322
Avi Kivity851ba692009-08-24 11:10:17 +03007323static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007324{
He, Qingbfdaab02007-09-12 14:18:28 +08007325 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007326 int dr, dr7, reg;
7327
7328 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7329 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7330
7331 /* First, if DR does not exist, trigger UD */
7332 if (!kvm_require_dr(vcpu, dr))
7333 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007334
Jan Kiszkaf2483412010-01-20 18:20:20 +01007335 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007336 if (!kvm_require_cpl(vcpu, 0))
7337 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007338 dr7 = vmcs_readl(GUEST_DR7);
7339 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007340 /*
7341 * As the vm-exit takes precedence over the debug trap, we
7342 * need to emulate the latter, either for the host or the
7343 * guest debugging itself.
7344 */
7345 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007346 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007347 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007348 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007349 vcpu->run->debug.arch.exception = DB_VECTOR;
7350 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007351 return 0;
7352 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007353 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007354 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007355 kvm_queue_exception(vcpu, DB_VECTOR);
7356 return 1;
7357 }
7358 }
7359
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007360 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007361 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7362 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007363
7364 /*
7365 * No more DR vmexits; force a reload of the debug registers
7366 * and reenter on this instruction. The next vmexit will
7367 * retrieve the full state of the debug registers.
7368 */
7369 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7370 return 1;
7371 }
7372
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007373 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7374 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007375 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007376
7377 if (kvm_get_dr(vcpu, dr, &val))
7378 return 1;
7379 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007380 } else
Nadav Amit57773922014-06-18 17:19:23 +03007381 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007382 return 1;
7383
Kyle Huey6affcbe2016-11-29 12:40:40 -08007384 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007385}
7386
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007387static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7388{
7389 return vcpu->arch.dr6;
7390}
7391
7392static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7393{
7394}
7395
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007396static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7397{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007398 get_debugreg(vcpu->arch.db[0], 0);
7399 get_debugreg(vcpu->arch.db[1], 1);
7400 get_debugreg(vcpu->arch.db[2], 2);
7401 get_debugreg(vcpu->arch.db[3], 3);
7402 get_debugreg(vcpu->arch.dr6, 6);
7403 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7404
7405 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007406 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007407}
7408
Gleb Natapov020df072010-04-13 10:05:23 +03007409static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7410{
7411 vmcs_writel(GUEST_DR7, val);
7412}
7413
Avi Kivity851ba692009-08-24 11:10:17 +03007414static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007415{
Kyle Huey6a908b62016-11-29 12:40:37 -08007416 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007417}
7418
Avi Kivity851ba692009-08-24 11:10:17 +03007419static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007420{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007421 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007422 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007423
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007424 msr_info.index = ecx;
7425 msr_info.host_initiated = false;
7426 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007427 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007428 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007429 return 1;
7430 }
7431
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007432 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007433
Avi Kivity6aa8b732006-12-10 02:21:36 -08007434 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007435 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7436 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007437 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007438}
7439
Avi Kivity851ba692009-08-24 11:10:17 +03007440static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007441{
Will Auld8fe8ab42012-11-29 12:42:12 -08007442 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007443 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7444 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7445 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007446
Will Auld8fe8ab42012-11-29 12:42:12 -08007447 msr.data = data;
7448 msr.index = ecx;
7449 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007450 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007451 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007452 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007453 return 1;
7454 }
7455
Avi Kivity59200272010-01-25 19:47:02 +02007456 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007457 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007458}
7459
Avi Kivity851ba692009-08-24 11:10:17 +03007460static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007461{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007462 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007463 return 1;
7464}
7465
Avi Kivity851ba692009-08-24 11:10:17 +03007466static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007468 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7469 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007470
Avi Kivity3842d132010-07-27 12:30:24 +03007471 kvm_make_request(KVM_REQ_EVENT, vcpu);
7472
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007473 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007474 return 1;
7475}
7476
Avi Kivity851ba692009-08-24 11:10:17 +03007477static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007478{
Avi Kivityd3bef152007-06-05 15:53:05 +03007479 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007480}
7481
Avi Kivity851ba692009-08-24 11:10:17 +03007482static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007483{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007484 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007485}
7486
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007487static int handle_invd(struct kvm_vcpu *vcpu)
7488{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007489 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007490}
7491
Avi Kivity851ba692009-08-24 11:10:17 +03007492static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007493{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007494 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007495
7496 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007497 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007498}
7499
Avi Kivityfee84b02011-11-10 14:57:25 +02007500static int handle_rdpmc(struct kvm_vcpu *vcpu)
7501{
7502 int err;
7503
7504 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007505 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007506}
7507
Avi Kivity851ba692009-08-24 11:10:17 +03007508static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007509{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007510 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007511}
7512
Dexuan Cui2acf9232010-06-10 11:27:12 +08007513static int handle_xsetbv(struct kvm_vcpu *vcpu)
7514{
7515 u64 new_bv = kvm_read_edx_eax(vcpu);
7516 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7517
7518 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007519 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007520 return 1;
7521}
7522
Wanpeng Lif53cd632014-12-02 19:14:58 +08007523static int handle_xsaves(struct kvm_vcpu *vcpu)
7524{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007525 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007526 WARN(1, "this should never happen\n");
7527 return 1;
7528}
7529
7530static int handle_xrstors(struct kvm_vcpu *vcpu)
7531{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007532 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007533 WARN(1, "this should never happen\n");
7534 return 1;
7535}
7536
Avi Kivity851ba692009-08-24 11:10:17 +03007537static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007538{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007539 if (likely(fasteoi)) {
7540 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7541 int access_type, offset;
7542
7543 access_type = exit_qualification & APIC_ACCESS_TYPE;
7544 offset = exit_qualification & APIC_ACCESS_OFFSET;
7545 /*
7546 * Sane guest uses MOV to write EOI, with written value
7547 * not cared. So make a short-circuit here by avoiding
7548 * heavy instruction emulation.
7549 */
7550 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7551 (offset == APIC_EOI)) {
7552 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007553 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007554 }
7555 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007556 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007557}
7558
Yang Zhangc7c9c562013-01-25 10:18:51 +08007559static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7560{
7561 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7562 int vector = exit_qualification & 0xff;
7563
7564 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7565 kvm_apic_set_eoi_accelerated(vcpu, vector);
7566 return 1;
7567}
7568
Yang Zhang83d4c282013-01-25 10:18:49 +08007569static int handle_apic_write(struct kvm_vcpu *vcpu)
7570{
7571 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7572 u32 offset = exit_qualification & 0xfff;
7573
7574 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7575 kvm_apic_write_nodecode(vcpu, offset);
7576 return 1;
7577}
7578
Avi Kivity851ba692009-08-24 11:10:17 +03007579static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007580{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007581 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007582 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007583 bool has_error_code = false;
7584 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007585 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007586 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007587
7588 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007589 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007590 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007591
7592 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7593
7594 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007595 if (reason == TASK_SWITCH_GATE && idt_v) {
7596 switch (type) {
7597 case INTR_TYPE_NMI_INTR:
7598 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007599 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007600 break;
7601 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007602 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007603 kvm_clear_interrupt_queue(vcpu);
7604 break;
7605 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007606 if (vmx->idt_vectoring_info &
7607 VECTORING_INFO_DELIVER_CODE_MASK) {
7608 has_error_code = true;
7609 error_code =
7610 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7611 }
7612 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007613 case INTR_TYPE_SOFT_EXCEPTION:
7614 kvm_clear_exception_queue(vcpu);
7615 break;
7616 default:
7617 break;
7618 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007619 }
Izik Eidus37817f22008-03-24 23:14:53 +02007620 tss_selector = exit_qualification;
7621
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007622 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7623 type != INTR_TYPE_EXT_INTR &&
7624 type != INTR_TYPE_NMI_INTR))
7625 skip_emulated_instruction(vcpu);
7626
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007627 if (kvm_task_switch(vcpu, tss_selector,
7628 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7629 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007630 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7631 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7632 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007633 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007634 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007635
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007636 /*
7637 * TODO: What about debug traps on tss switch?
7638 * Are we supposed to inject them and update dr6?
7639 */
7640
7641 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007642}
7643
Avi Kivity851ba692009-08-24 11:10:17 +03007644static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007645{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007646 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007647 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007648 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007649
Sheng Yangf9c617f2009-03-25 10:08:52 +08007650 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007651
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007652 /*
7653 * EPT violation happened while executing iret from NMI,
7654 * "blocked by NMI" bit has to be set before next VM entry.
7655 * There are errata that may cause this bit to not be set:
7656 * AAK134, BY25.
7657 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007658 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007659 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007660 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007661 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7662
Sheng Yang14394422008-04-28 12:24:45 +08007663 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007664 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007665
Junaid Shahid27959a42016-12-06 16:46:10 -08007666 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007667 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007668 ? PFERR_USER_MASK : 0;
7669 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007670 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007671 ? PFERR_WRITE_MASK : 0;
7672 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007673 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007674 ? PFERR_FETCH_MASK : 0;
7675 /* ept page table entry is present? */
7676 error_code |= (exit_qualification &
7677 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7678 EPT_VIOLATION_EXECUTABLE))
7679 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007680
Paolo Bonzinieebed242016-11-28 14:39:58 +01007681 error_code |= (exit_qualification & 0x100) != 0 ?
7682 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007683
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007684 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007685 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007686}
7687
Avi Kivity851ba692009-08-24 11:10:17 +03007688static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007689{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007690 gpa_t gpa;
7691
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007692 /*
7693 * A nested guest cannot optimize MMIO vmexits, because we have an
7694 * nGPA here instead of the required GPA.
7695 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007696 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007697 if (!is_guest_mode(vcpu) &&
7698 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007699 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007700 /*
7701 * Doing kvm_skip_emulated_instruction() depends on undefined
7702 * behavior: Intel's manual doesn't mandate
7703 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7704 * occurs and while on real hardware it was observed to be set,
7705 * other hypervisors (namely Hyper-V) don't set it, we end up
7706 * advancing IP with some random value. Disable fast mmio when
7707 * running nested and keep it for real hardware in hope that
7708 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7709 */
7710 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7711 return kvm_skip_emulated_instruction(vcpu);
7712 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007713 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007714 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007715 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007716
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007717 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007718}
7719
Avi Kivity851ba692009-08-24 11:10:17 +03007720static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007721{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007722 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007723 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7724 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007725 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007726 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007727
7728 return 1;
7729}
7730
Mohammed Gamal80ced182009-09-01 12:48:18 +02007731static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007732{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007733 struct vcpu_vmx *vmx = to_vmx(vcpu);
7734 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007735 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007736 u32 cpu_exec_ctrl;
7737 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007738 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007739
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007740 /*
7741 * We should never reach the point where we are emulating L2
7742 * due to invalid guest state as that means we incorrectly
7743 * allowed a nested VMEntry with an invalid vmcs12.
7744 */
7745 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7746
Avi Kivity49e9d552010-09-19 14:34:08 +02007747 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7748 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007749
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007750 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007751 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007752 return handle_interrupt_window(&vmx->vcpu);
7753
Radim Krčmář72875d82017-04-26 22:32:19 +02007754 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007755 return 1;
7756
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007757 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007758
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007759 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007760 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007761 ret = 0;
7762 goto out;
7763 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007764
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007765 if (err != EMULATE_DONE)
7766 goto emulation_error;
7767
7768 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7769 vcpu->arch.exception.pending)
7770 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007771
Gleb Natapov8d76c492013-05-08 18:38:44 +03007772 if (vcpu->arch.halt_request) {
7773 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007774 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007775 goto out;
7776 }
7777
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007778 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007779 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007780 if (need_resched())
7781 schedule();
7782 }
7783
Mohammed Gamal80ced182009-09-01 12:48:18 +02007784out:
7785 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007786
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007787emulation_error:
7788 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7789 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7790 vcpu->run->internal.ndata = 0;
7791 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007792}
7793
7794static void grow_ple_window(struct kvm_vcpu *vcpu)
7795{
7796 struct vcpu_vmx *vmx = to_vmx(vcpu);
7797 int old = vmx->ple_window;
7798
Babu Mogerc8e88712018-03-16 16:37:24 -04007799 vmx->ple_window = __grow_ple_window(old, ple_window,
7800 ple_window_grow,
7801 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007802
7803 if (vmx->ple_window != old)
7804 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007805
7806 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007807}
7808
7809static void shrink_ple_window(struct kvm_vcpu *vcpu)
7810{
7811 struct vcpu_vmx *vmx = to_vmx(vcpu);
7812 int old = vmx->ple_window;
7813
Babu Mogerc8e88712018-03-16 16:37:24 -04007814 vmx->ple_window = __shrink_ple_window(old, ple_window,
7815 ple_window_shrink,
7816 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007817
7818 if (vmx->ple_window != old)
7819 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007820
7821 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007822}
7823
7824/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007825 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7826 */
7827static void wakeup_handler(void)
7828{
7829 struct kvm_vcpu *vcpu;
7830 int cpu = smp_processor_id();
7831
7832 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7833 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7834 blocked_vcpu_list) {
7835 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7836
7837 if (pi_test_on(pi_desc) == 1)
7838 kvm_vcpu_kick(vcpu);
7839 }
7840 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7841}
7842
Peng Haoe01bca22018-04-07 05:47:32 +08007843static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007844{
7845 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7846 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7847 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7848 0ull, VMX_EPT_EXECUTABLE_MASK,
7849 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007850 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007851
7852 ept_set_mmio_spte_mask();
7853 kvm_enable_tdp();
7854}
7855
Tiejun Chenf2c76482014-10-28 10:14:47 +08007856static __init int hardware_setup(void)
7857{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007858 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007859 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007860
7861 rdmsrl_safe(MSR_EFER, &host_efer);
7862
7863 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7864 kvm_define_shared_msr(i, vmx_msr_index[i]);
7865
Radim Krčmář23611332016-09-29 22:41:33 +02007866 for (i = 0; i < VMX_BITMAP_NR; i++) {
7867 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7868 if (!vmx_bitmap[i])
7869 goto out;
7870 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007871
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007872 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7873 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7874
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007875 if (setup_vmcs_config(&vmcs_config) < 0) {
7876 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007877 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007878 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007879
7880 if (boot_cpu_has(X86_FEATURE_NX))
7881 kvm_enable_efer_bits(EFER_NX);
7882
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007883 if (boot_cpu_has(X86_FEATURE_MPX)) {
7884 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7885 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7886 }
7887
Wanpeng Li08d839c2017-03-23 05:30:08 -07007888 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7889 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007890 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007891
Tiejun Chenf2c76482014-10-28 10:14:47 +08007892 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007893 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007894 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007895 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007896 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007897
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007898 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007899 enable_ept_ad_bits = 0;
7900
Wanpeng Li8ad81822017-10-09 15:51:53 -07007901 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007902 enable_unrestricted_guest = 0;
7903
Paolo Bonziniad15a292015-01-30 16:18:49 +01007904 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007905 flexpriority_enabled = 0;
7906
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007907 if (!cpu_has_virtual_nmis())
7908 enable_vnmi = 0;
7909
Paolo Bonziniad15a292015-01-30 16:18:49 +01007910 /*
7911 * set_apic_access_page_addr() is used to reload apic access
7912 * page upon invalidation. No need to do anything if not
7913 * using the APIC_ACCESS_ADDR VMCS field.
7914 */
7915 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007916 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007917
7918 if (!cpu_has_vmx_tpr_shadow())
7919 kvm_x86_ops->update_cr8_intercept = NULL;
7920
7921 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7922 kvm_disable_largepages();
7923
Tianyu Lan877ad952018-07-19 08:40:23 +00007924#if IS_ENABLED(CONFIG_HYPERV)
7925 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7926 && enable_ept)
7927 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7928#endif
7929
Wanpeng Li0f107682017-09-28 18:06:24 -07007930 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007931 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007932 ple_window = 0;
7933 ple_window_grow = 0;
7934 ple_window_max = 0;
7935 ple_window_shrink = 0;
7936 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007937
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007938 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007939 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007940 kvm_x86_ops->sync_pir_to_irr = NULL;
7941 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007942
Haozhong Zhang64903d62015-10-20 15:39:09 +08007943 if (cpu_has_vmx_tsc_scaling()) {
7944 kvm_has_tsc_control = true;
7945 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7946 kvm_tsc_scaling_ratio_frac_bits = 48;
7947 }
7948
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007949 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7950
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007951 if (enable_ept)
7952 vmx_enable_tdp();
7953 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007954 kvm_disable_tdp();
7955
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007956 if (!nested) {
7957 kvm_x86_ops->get_nested_state = NULL;
7958 kvm_x86_ops->set_nested_state = NULL;
7959 }
7960
Kai Huang843e4332015-01-28 10:54:28 +08007961 /*
7962 * Only enable PML when hardware supports PML feature, and both EPT
7963 * and EPT A/D bit features are enabled -- PML depends on them to work.
7964 */
7965 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7966 enable_pml = 0;
7967
7968 if (!enable_pml) {
7969 kvm_x86_ops->slot_enable_log_dirty = NULL;
7970 kvm_x86_ops->slot_disable_log_dirty = NULL;
7971 kvm_x86_ops->flush_log_dirty = NULL;
7972 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7973 }
7974
Sean Christophersond264ee02018-08-27 15:21:12 -07007975 if (!cpu_has_vmx_preemption_timer())
7976 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7977
Yunhong Jiang64672c92016-06-13 14:19:59 -07007978 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7979 u64 vmx_msr;
7980
7981 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7982 cpu_preemption_timer_multi =
7983 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7984 } else {
7985 kvm_x86_ops->set_hv_timer = NULL;
7986 kvm_x86_ops->cancel_hv_timer = NULL;
7987 }
7988
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007989 if (!cpu_has_vmx_shadow_vmcs())
7990 enable_shadow_vmcs = 0;
7991 if (enable_shadow_vmcs)
7992 init_vmcs_shadow_fields();
7993
Feng Wubf9f6ac2015-09-18 22:29:55 +08007994 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007995 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007996
Ashok Rajc45dcc72016-06-22 14:59:56 +08007997 kvm_mce_cap_supported |= MCG_LMCE_P;
7998
Tiejun Chenf2c76482014-10-28 10:14:47 +08007999 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008000
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008001out:
Radim Krčmář23611332016-09-29 22:41:33 +02008002 for (i = 0; i < VMX_BITMAP_NR; i++)
8003 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008004
8005 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008006}
8007
8008static __exit void hardware_unsetup(void)
8009{
Radim Krčmář23611332016-09-29 22:41:33 +02008010 int i;
8011
8012 for (i = 0; i < VMX_BITMAP_NR; i++)
8013 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008014
Tiejun Chenf2c76482014-10-28 10:14:47 +08008015 free_kvm_area();
8016}
8017
Avi Kivity6aa8b732006-12-10 02:21:36 -08008018/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008019 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8020 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8021 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008022static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008023{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008024 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008025 grow_ple_window(vcpu);
8026
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008027 /*
8028 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8029 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8030 * never set PAUSE_EXITING and just set PLE if supported,
8031 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8032 */
8033 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008034 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008035}
8036
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008037static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008038{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008039 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008040}
8041
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008042static int handle_mwait(struct kvm_vcpu *vcpu)
8043{
8044 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8045 return handle_nop(vcpu);
8046}
8047
Jim Mattson45ec3682017-08-23 16:32:04 -07008048static int handle_invalid_op(struct kvm_vcpu *vcpu)
8049{
8050 kvm_queue_exception(vcpu, UD_VECTOR);
8051 return 1;
8052}
8053
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008054static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8055{
8056 return 1;
8057}
8058
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008059static int handle_monitor(struct kvm_vcpu *vcpu)
8060{
8061 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8062 return handle_nop(vcpu);
8063}
8064
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008065/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008066 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008067 * set the success or error code of an emulated VMX instruction (as specified
8068 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
8069 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008070 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008071static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008072{
8073 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8074 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8075 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008076 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008077}
8078
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008079static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008080{
8081 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8082 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8083 X86_EFLAGS_SF | X86_EFLAGS_OF))
8084 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008085 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008086}
8087
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008088static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
8089 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008090{
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008091 /*
8092 * failValid writes the error number to the current VMCS, which
8093 * can't be done if there isn't a current VMCS.
8094 */
8095 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8096 return nested_vmx_failInvalid(vcpu);
8097
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008098 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8099 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8100 X86_EFLAGS_SF | X86_EFLAGS_OF))
8101 | X86_EFLAGS_ZF);
8102 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8103 /*
8104 * We don't need to force a shadow sync because
8105 * VM_INSTRUCTION_ERROR is not shadowed
8106 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008107 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008108}
Abel Gordon145c28d2013-04-18 14:36:55 +03008109
Wincy Vanff651cb2014-12-11 08:52:58 +03008110static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8111{
8112 /* TODO: not to reset guest simply here. */
8113 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008114 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008115}
8116
Jan Kiszkaf4124502014-03-07 20:03:13 +01008117static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8118{
8119 struct vcpu_vmx *vmx =
8120 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8121
8122 vmx->nested.preemption_timer_expired = true;
8123 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8124 kvm_vcpu_kick(&vmx->vcpu);
8125
8126 return HRTIMER_NORESTART;
8127}
8128
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008129/*
Bandan Das19677e32014-05-06 02:19:15 -04008130 * Decode the memory-address operand of a vmx instruction, as recorded on an
8131 * exit caused by such an instruction (run by a guest hypervisor).
8132 * On success, returns 0. When the operand is invalid, returns 1 and throws
8133 * #UD or #GP.
8134 */
8135static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8136 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008137 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008138{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008139 gva_t off;
8140 bool exn;
8141 struct kvm_segment s;
8142
Bandan Das19677e32014-05-06 02:19:15 -04008143 /*
8144 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8145 * Execution", on an exit, vmx_instruction_info holds most of the
8146 * addressing components of the operand. Only the displacement part
8147 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8148 * For how an actual address is calculated from all these components,
8149 * refer to Vol. 1, "Operand Addressing".
8150 */
8151 int scaling = vmx_instruction_info & 3;
8152 int addr_size = (vmx_instruction_info >> 7) & 7;
8153 bool is_reg = vmx_instruction_info & (1u << 10);
8154 int seg_reg = (vmx_instruction_info >> 15) & 7;
8155 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8156 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8157 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8158 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8159
8160 if (is_reg) {
8161 kvm_queue_exception(vcpu, UD_VECTOR);
8162 return 1;
8163 }
8164
8165 /* Addr = segment_base + offset */
8166 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008167 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008168 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008169 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008170 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008171 off += kvm_register_read(vcpu, index_reg)<<scaling;
8172 vmx_get_segment(vcpu, &s, seg_reg);
8173 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008174
8175 if (addr_size == 1) /* 32 bit */
8176 *ret &= 0xffffffff;
8177
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008178 /* Checks for #GP/#SS exceptions. */
8179 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008180 if (is_long_mode(vcpu)) {
8181 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8182 * non-canonical form. This is the only check on the memory
8183 * destination for long mode!
8184 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008185 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008186 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008187 /* Protected mode: apply checks for segment validity in the
8188 * following order:
8189 * - segment type check (#GP(0) may be thrown)
8190 * - usability check (#GP(0)/#SS(0))
8191 * - limit check (#GP(0)/#SS(0))
8192 */
8193 if (wr)
8194 /* #GP(0) if the destination operand is located in a
8195 * read-only data segment or any code segment.
8196 */
8197 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8198 else
8199 /* #GP(0) if the source operand is located in an
8200 * execute-only code segment
8201 */
8202 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008203 if (exn) {
8204 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8205 return 1;
8206 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008207 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8208 */
8209 exn = (s.unusable != 0);
8210 /* Protected mode: #GP(0)/#SS(0) if the memory
8211 * operand is outside the segment limit.
8212 */
8213 exn = exn || (off + sizeof(u64) > s.limit);
8214 }
8215 if (exn) {
8216 kvm_queue_exception_e(vcpu,
8217 seg_reg == VCPU_SREG_SS ?
8218 SS_VECTOR : GP_VECTOR,
8219 0);
8220 return 1;
8221 }
8222
Bandan Das19677e32014-05-06 02:19:15 -04008223 return 0;
8224}
8225
Radim Krčmářcbf71272017-05-19 15:48:51 +02008226static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008227{
8228 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008229 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008230
8231 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008232 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008233 return 1;
8234
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008235 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008236 kvm_inject_page_fault(vcpu, &e);
8237 return 1;
8238 }
8239
Bandan Das3573e222014-05-06 02:19:16 -04008240 return 0;
8241}
8242
Liran Alonabfc52c2018-06-23 02:35:13 +03008243/*
8244 * Allocate a shadow VMCS and associate it with the currently loaded
8245 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8246 * VMCS is also VMCLEARed, so that it is ready for use.
8247 */
8248static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8249{
8250 struct vcpu_vmx *vmx = to_vmx(vcpu);
8251 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8252
8253 /*
8254 * We should allocate a shadow vmcs for vmcs01 only when L1
8255 * executes VMXON and free it when L1 executes VMXOFF.
8256 * As it is invalid to execute VMXON twice, we shouldn't reach
8257 * here when vmcs01 already have an allocated shadow vmcs.
8258 */
8259 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8260
8261 if (!loaded_vmcs->shadow_vmcs) {
8262 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8263 if (loaded_vmcs->shadow_vmcs)
8264 vmcs_clear(loaded_vmcs->shadow_vmcs);
8265 }
8266 return loaded_vmcs->shadow_vmcs;
8267}
8268
Jim Mattsone29acc52016-11-30 12:03:43 -08008269static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8270{
8271 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008272 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008273
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008274 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8275 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008276 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008277
8278 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8279 if (!vmx->nested.cached_vmcs12)
8280 goto out_cached_vmcs12;
8281
Liran Alon61ada742018-06-23 02:35:08 +03008282 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8283 if (!vmx->nested.cached_shadow_vmcs12)
8284 goto out_cached_shadow_vmcs12;
8285
Liran Alonabfc52c2018-06-23 02:35:13 +03008286 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8287 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008288
Jim Mattsone29acc52016-11-30 12:03:43 -08008289 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8290 HRTIMER_MODE_REL_PINNED);
8291 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8292
Roman Kagan63aff652018-07-19 21:59:07 +03008293 vmx->nested.vpid02 = allocate_vpid();
8294
Sean Christopherson9d6105b22018-09-26 09:23:51 -07008295 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008296 vmx->nested.vmxon = true;
8297 return 0;
8298
8299out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008300 kfree(vmx->nested.cached_shadow_vmcs12);
8301
8302out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008303 kfree(vmx->nested.cached_vmcs12);
8304
8305out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008306 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008307
Jim Mattsonde3a0022017-11-27 17:22:25 -06008308out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008309 return -ENOMEM;
8310}
8311
Bandan Das3573e222014-05-06 02:19:16 -04008312/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008313 * Emulate the VMXON instruction.
8314 * Currently, we just remember that VMX is active, and do not save or even
8315 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8316 * do not currently need to store anything in that guest-allocated memory
8317 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8318 * argument is different from the VMXON pointer (which the spec says they do).
8319 */
8320static int handle_vmon(struct kvm_vcpu *vcpu)
8321{
Jim Mattsone29acc52016-11-30 12:03:43 -08008322 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008323 gpa_t vmptr;
8324 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008325 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008326 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8327 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008328
Jim Mattson70f3aac2017-04-26 08:53:46 -07008329 /*
8330 * The Intel VMX Instruction Reference lists a bunch of bits that are
8331 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8332 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8333 * Otherwise, we should fail with #UD. But most faulting conditions
8334 * have already been checked by hardware, prior to the VM-exit for
8335 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8336 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008337 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008338 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008339 kvm_queue_exception(vcpu, UD_VECTOR);
8340 return 1;
8341 }
8342
Felix Wilhelm727ba742018-06-11 09:43:44 +02008343 /* CPL=0 must be checked manually. */
8344 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008345 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008346 return 1;
8347 }
8348
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008349 if (vmx->nested.vmxon)
8350 return nested_vmx_failValid(vcpu,
8351 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008352
Haozhong Zhang3b840802016-06-22 14:59:54 +08008353 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008354 != VMXON_NEEDED_FEATURES) {
8355 kvm_inject_gp(vcpu, 0);
8356 return 1;
8357 }
8358
Radim Krčmářcbf71272017-05-19 15:48:51 +02008359 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008360 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008361
8362 /*
8363 * SDM 3: 24.11.5
8364 * The first 4 bytes of VMXON region contain the supported
8365 * VMCS revision identifier
8366 *
8367 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8368 * which replaces physical address width with 32
8369 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008370 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8371 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008372
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008373 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008374 if (is_error_page(page))
8375 return nested_vmx_failInvalid(vcpu);
8376
Radim Krčmářcbf71272017-05-19 15:48:51 +02008377 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8378 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008379 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008380 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008381 }
8382 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008383 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008384
8385 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008386 ret = enter_vmx_operation(vcpu);
8387 if (ret)
8388 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008389
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008390 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008391}
8392
8393/*
8394 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8395 * for running VMX instructions (except VMXON, whose prerequisites are
8396 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008397 * Note that many of these exceptions have priority over VM exits, so they
8398 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008399 */
8400static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8401{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008402 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008403 kvm_queue_exception(vcpu, UD_VECTOR);
8404 return 0;
8405 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008406
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008407 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008408 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008409 return 0;
8410 }
8411
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008412 return 1;
8413}
8414
David Matlack8ca44e82017-08-01 14:00:39 -07008415static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8416{
8417 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8418 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8419}
8420
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008421static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008422{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008423 struct vcpu_vmx *vmx = to_vmx(vcpu);
8424
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008425 if (vmx->nested.current_vmptr == -1ull)
8426 return;
8427
Abel Gordon012f83c2013-04-18 14:39:25 +03008428 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008429 /* copy to memory all shadowed fields in case
8430 they were modified */
8431 copy_shadow_to_vmcs12(vmx);
8432 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008433 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008434 }
Wincy Van705699a2015-02-03 23:58:17 +08008435 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008436
8437 /* Flush VMCS12 to guest memory */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008438 kvm_vcpu_write_guest_page(vcpu,
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008439 vmx->nested.current_vmptr >> PAGE_SHIFT,
8440 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008441
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008442 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8443
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008444 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008445}
8446
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008447/*
8448 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8449 * just stops using VMX.
8450 */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008451static void free_nested(struct kvm_vcpu *vcpu)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008452{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008453 struct vcpu_vmx *vmx = to_vmx(vcpu);
8454
Wanpeng Lib7455822017-11-22 14:04:00 -08008455 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008456 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008457
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008458 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008459 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008460 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008461 vmx->nested.posted_intr_nv = -1;
8462 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008463 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008464 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008465 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8466 free_vmcs(vmx->vmcs01.shadow_vmcs);
8467 vmx->vmcs01.shadow_vmcs = NULL;
8468 }
David Matlack4f2777b2016-07-13 17:16:37 -07008469 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008470 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008471 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008472 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008473 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008474 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008475 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008476 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008477 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008478 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008479 }
Wincy Van705699a2015-02-03 23:58:17 +08008480 if (vmx->nested.pi_desc_page) {
8481 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008482 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008483 vmx->nested.pi_desc_page = NULL;
8484 vmx->nested.pi_desc = NULL;
8485 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008486
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008487 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8488
Jim Mattsonde3a0022017-11-27 17:22:25 -06008489 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008490}
8491
8492/* Emulate the VMXOFF instruction */
8493static int handle_vmoff(struct kvm_vcpu *vcpu)
8494{
8495 if (!nested_vmx_check_permission(vcpu))
8496 return 1;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008497 free_nested(vcpu);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008498 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008499}
8500
Nadav Har'El27d6c862011-05-25 23:06:59 +03008501/* Emulate the VMCLEAR instruction */
8502static int handle_vmclear(struct kvm_vcpu *vcpu)
8503{
8504 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008505 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008506 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008507
8508 if (!nested_vmx_check_permission(vcpu))
8509 return 1;
8510
Radim Krčmářcbf71272017-05-19 15:48:51 +02008511 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008512 return 1;
8513
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008514 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8515 return nested_vmx_failValid(vcpu,
8516 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008517
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008518 if (vmptr == vmx->nested.vmxon_ptr)
8519 return nested_vmx_failValid(vcpu,
8520 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008521
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008522 if (vmptr == vmx->nested.current_vmptr)
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008523 nested_release_vmcs12(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008524
Jim Mattson587d7e722017-03-02 12:41:48 -08008525 kvm_vcpu_write_guest(vcpu,
8526 vmptr + offsetof(struct vmcs12, launch_state),
8527 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008528
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008529 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008530}
8531
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008532static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8533
8534/* Emulate the VMLAUNCH instruction */
8535static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8536{
8537 return nested_vmx_run(vcpu, true);
8538}
8539
8540/* Emulate the VMRESUME instruction */
8541static int handle_vmresume(struct kvm_vcpu *vcpu)
8542{
8543
8544 return nested_vmx_run(vcpu, false);
8545}
8546
Nadav Har'El49f705c2011-05-25 23:08:30 +03008547/*
8548 * Read a vmcs12 field. Since these can have varying lengths and we return
8549 * one type, we chose the biggest type (u64) and zero-extend the return value
8550 * to that size. Note that the caller, handle_vmread, might need to use only
8551 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8552 * 64-bit fields are to be returned).
8553 */
Liran Alone2536742018-06-23 02:35:02 +03008554static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008555 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008556{
8557 short offset = vmcs_field_to_offset(field);
8558 char *p;
8559
8560 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008561 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562
Liran Alone2536742018-06-23 02:35:02 +03008563 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564
Jim Mattsond37f4262017-12-22 12:12:16 -08008565 switch (vmcs_field_width(field)) {
8566 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008569 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008570 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008571 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008572 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008573 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008574 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008575 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008576 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008577 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008578 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008579 WARN_ON(1);
8580 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008581 }
8582}
8583
Abel Gordon20b97fe2013-04-18 14:36:25 +03008584
Liran Alone2536742018-06-23 02:35:02 +03008585static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008586 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008587 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008588 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008589 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008590 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008591
Jim Mattsond37f4262017-12-22 12:12:16 -08008592 switch (vmcs_field_width(field)) {
8593 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 *(u16 *)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_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008597 *(u32 *)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_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008600 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008601 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008602 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008603 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008604 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008605 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008606 WARN_ON(1);
8607 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008608 }
8609
8610}
8611
Jim Mattsonf4160e42018-05-29 09:11:33 -07008612/*
8613 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8614 * they have been modified by the L1 guest. Note that the "read-only"
8615 * VM-exit information fields are actually writable if the vCPU is
8616 * configured to support "VMWRITE to any supported field in the VMCS."
8617 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008618static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8619{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008620 const u16 *fields[] = {
8621 shadow_read_write_fields,
8622 shadow_read_only_fields
8623 };
8624 const int max_fields[] = {
8625 max_shadow_read_write_fields,
8626 max_shadow_read_only_fields
8627 };
8628 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008629 unsigned long field;
8630 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008631 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008632
Jan Kiszka282da872014-10-08 18:05:39 +02008633 preempt_disable();
8634
Abel Gordon16f5b902013-04-18 14:38:25 +03008635 vmcs_load(shadow_vmcs);
8636
Jim Mattsonf4160e42018-05-29 09:11:33 -07008637 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8638 for (i = 0; i < max_fields[q]; i++) {
8639 field = fields[q][i];
8640 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008641 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008642 }
8643 /*
8644 * Skip the VM-exit information fields if they are read-only.
8645 */
8646 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8647 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008648 }
8649
8650 vmcs_clear(shadow_vmcs);
8651 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008652
8653 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008654}
8655
Abel Gordonc3114422013-04-18 14:38:55 +03008656static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8657{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008658 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008659 shadow_read_write_fields,
8660 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008661 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008662 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008663 max_shadow_read_write_fields,
8664 max_shadow_read_only_fields
8665 };
8666 int i, q;
8667 unsigned long field;
8668 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008669 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008670
8671 vmcs_load(shadow_vmcs);
8672
Mathias Krausec2bae892013-06-26 20:36:21 +02008673 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008674 for (i = 0; i < max_fields[q]; i++) {
8675 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008676 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008677 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008678 }
8679 }
8680
8681 vmcs_clear(shadow_vmcs);
8682 vmcs_load(vmx->loaded_vmcs->vmcs);
8683}
8684
Nadav Har'El49f705c2011-05-25 23:08:30 +03008685static int handle_vmread(struct kvm_vcpu *vcpu)
8686{
8687 unsigned long field;
8688 u64 field_value;
8689 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8690 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8691 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008692 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008693
Kyle Hueyeb277562016-11-29 12:40:39 -08008694 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008695 return 1;
8696
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008697 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8698 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008699
Liran Alon6d894f42018-06-23 02:35:09 +03008700 if (!is_guest_mode(vcpu))
8701 vmcs12 = get_vmcs12(vcpu);
8702 else {
8703 /*
8704 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8705 * to shadowed-field sets the ALU flags for VMfailInvalid.
8706 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008707 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8708 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008709 vmcs12 = get_shadow_vmcs12(vcpu);
8710 }
8711
Nadav Har'El49f705c2011-05-25 23:08:30 +03008712 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008713 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008714 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008715 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
8716 return nested_vmx_failValid(vcpu,
8717 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8718
Nadav Har'El49f705c2011-05-25 23:08:30 +03008719 /*
8720 * Now copy part of this value to register or memory, as requested.
8721 * Note that the number of bits actually copied is 32 or 64 depending
8722 * on the guest's mode (32 or 64 bit), not on the given field's length.
8723 */
8724 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008725 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008726 field_value);
8727 } else {
8728 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008729 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008730 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008731 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008732 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8733 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008734 }
8735
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008736 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008737}
8738
8739
8740static int handle_vmwrite(struct kvm_vcpu *vcpu)
8741{
8742 unsigned long field;
8743 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008744 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008745 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8746 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008747
Nadav Har'El49f705c2011-05-25 23:08:30 +03008748 /* The value to write might be 32 or 64 bits, depending on L1's long
8749 * mode, and eventually we need to write that into a field of several
8750 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008751 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008752 * bits into the vmcs12 field.
8753 */
8754 u64 field_value = 0;
8755 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008756 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008757
Kyle Hueyeb277562016-11-29 12:40:39 -08008758 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008759 return 1;
8760
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008761 if (vmx->nested.current_vmptr == -1ull)
8762 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008763
Nadav Har'El49f705c2011-05-25 23:08:30 +03008764 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008765 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008766 (((vmx_instruction_info) >> 3) & 0xf));
8767 else {
8768 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008769 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008770 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008771 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8772 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008773 kvm_inject_page_fault(vcpu, &e);
8774 return 1;
8775 }
8776 }
8777
8778
Nadav Amit27e6fb52014-06-18 17:19:26 +03008779 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008780 /*
8781 * If the vCPU supports "VMWRITE to any supported field in the
8782 * VMCS," then the "read-only" fields are actually read/write.
8783 */
8784 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008785 !nested_cpu_has_vmwrite_any_field(vcpu))
8786 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008787 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008788
Liran Alon6d894f42018-06-23 02:35:09 +03008789 if (!is_guest_mode(vcpu))
8790 vmcs12 = get_vmcs12(vcpu);
8791 else {
8792 /*
8793 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8794 * to shadowed-field sets the ALU flags for VMfailInvalid.
8795 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008796 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8797 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008798 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008799 }
8800
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008801 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
8802 return nested_vmx_failValid(vcpu,
8803 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008804
Liran Alon6d894f42018-06-23 02:35:09 +03008805 /*
8806 * Do not track vmcs12 dirty-state if in guest-mode
8807 * as we actually dirty shadow vmcs12 instead of vmcs12.
8808 */
8809 if (!is_guest_mode(vcpu)) {
8810 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008811#define SHADOW_FIELD_RW(x) case x:
8812#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008813 /*
8814 * The fields that can be updated by L1 without a vmexit are
8815 * always updated in the vmcs02, the others go down the slow
8816 * path of prepare_vmcs02.
8817 */
8818 break;
8819 default:
8820 vmx->nested.dirty_vmcs12 = true;
8821 break;
8822 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008823 }
8824
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008825 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008826}
8827
Jim Mattsona8bc2842016-11-30 12:03:44 -08008828static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8829{
8830 vmx->nested.current_vmptr = vmptr;
8831 if (enable_shadow_vmcs) {
8832 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8833 SECONDARY_EXEC_SHADOW_VMCS);
8834 vmcs_write64(VMCS_LINK_POINTER,
8835 __pa(vmx->vmcs01.shadow_vmcs));
8836 vmx->nested.sync_shadow_vmcs = true;
8837 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008838 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008839}
8840
Nadav Har'El63846662011-05-25 23:07:29 +03008841/* Emulate the VMPTRLD instruction */
8842static int handle_vmptrld(struct kvm_vcpu *vcpu)
8843{
8844 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008845 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008846
8847 if (!nested_vmx_check_permission(vcpu))
8848 return 1;
8849
Radim Krčmářcbf71272017-05-19 15:48:51 +02008850 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008851 return 1;
8852
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008853 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8854 return nested_vmx_failValid(vcpu,
8855 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008856
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008857 if (vmptr == vmx->nested.vmxon_ptr)
8858 return nested_vmx_failValid(vcpu,
8859 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008860
Nadav Har'El63846662011-05-25 23:07:29 +03008861 if (vmx->nested.current_vmptr != vmptr) {
8862 struct vmcs12 *new_vmcs12;
8863 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008864 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008865 if (is_error_page(page))
8866 return nested_vmx_failInvalid(vcpu);
8867
Nadav Har'El63846662011-05-25 23:07:29 +03008868 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008869 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008870 (new_vmcs12->hdr.shadow_vmcs &&
8871 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008872 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008873 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008874 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03008875 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03008876 }
Nadav Har'El63846662011-05-25 23:07:29 +03008877
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008878 nested_release_vmcs12(vcpu);
8879
David Matlack4f2777b2016-07-13 17:16:37 -07008880 /*
8881 * Load VMCS12 from guest memory since it is not already
8882 * cached.
8883 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008884 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8885 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008886 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008887
Jim Mattsona8bc2842016-11-30 12:03:44 -08008888 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008889 }
8890
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008891 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008892}
8893
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008894/* Emulate the VMPTRST instruction */
8895static int handle_vmptrst(struct kvm_vcpu *vcpu)
8896{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008897 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8898 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8899 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008900 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008901 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008902
8903 if (!nested_vmx_check_permission(vcpu))
8904 return 1;
8905
Sean Christopherson0a06d422018-07-19 10:31:00 -07008906 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008907 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008908 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008909 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8910 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008911 kvm_inject_page_fault(vcpu, &e);
8912 return 1;
8913 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008914 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008915}
8916
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008917/* Emulate the INVEPT instruction */
8918static int handle_invept(struct kvm_vcpu *vcpu)
8919{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008920 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008921 u32 vmx_instruction_info, types;
8922 unsigned long type;
8923 gva_t gva;
8924 struct x86_exception e;
8925 struct {
8926 u64 eptp, gpa;
8927 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008928
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008929 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008930 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008931 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008932 kvm_queue_exception(vcpu, UD_VECTOR);
8933 return 1;
8934 }
8935
8936 if (!nested_vmx_check_permission(vcpu))
8937 return 1;
8938
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008939 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008940 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008941
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008942 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008943
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008944 if (type >= 32 || !(types & (1 << type)))
8945 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008946 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008947
8948 /* According to the Intel VMX instruction reference, the memory
8949 * operand is read even if it isn't needed (e.g., for type==global)
8950 */
8951 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008952 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008953 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008954 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008955 kvm_inject_page_fault(vcpu, &e);
8956 return 1;
8957 }
8958
8959 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008960 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008961 /*
8962 * TODO: track mappings and invalidate
8963 * single context requests appropriately
8964 */
8965 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008966 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008967 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008968 break;
8969 default:
8970 BUG_ON(1);
8971 break;
8972 }
8973
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008974 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008975}
8976
Liran Alon3d5bdae2018-10-08 23:42:18 +03008977static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
8978{
8979 struct vcpu_vmx *vmx = to_vmx(vcpu);
8980
8981 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
8982}
8983
Petr Matouseka642fc32014-09-23 20:22:30 +02008984static int handle_invvpid(struct kvm_vcpu *vcpu)
8985{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008986 struct vcpu_vmx *vmx = to_vmx(vcpu);
8987 u32 vmx_instruction_info;
8988 unsigned long type, types;
8989 gva_t gva;
8990 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008991 struct {
8992 u64 vpid;
8993 u64 gla;
8994 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03008995 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008996
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008997 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008998 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008999 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009000 kvm_queue_exception(vcpu, UD_VECTOR);
9001 return 1;
9002 }
9003
9004 if (!nested_vmx_check_permission(vcpu))
9005 return 1;
9006
9007 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9008 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9009
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009010 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009011 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009012
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009013 if (type >= 32 || !(types & (1 << type)))
9014 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009015 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009016
9017 /* according to the intel vmx instruction reference, the memory
9018 * operand is read even if it isn't needed (e.g., for type==global)
9019 */
9020 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9021 vmx_instruction_info, false, &gva))
9022 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009023 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009024 kvm_inject_page_fault(vcpu, &e);
9025 return 1;
9026 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009027 if (operand.vpid >> 16)
9028 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009029 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009030
Liran Alon3d5bdae2018-10-08 23:42:18 +03009031 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009032 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009033 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009034 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009035 is_noncanonical_address(operand.gla, vcpu))
9036 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009037 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03009038 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009039 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009040 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009041 } else
Liran Alon327c0722018-10-08 23:42:19 +03009042 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009043 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009044 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009045 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009046 if (!operand.vpid)
9047 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009048 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03009049 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009050 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009051 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009052 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009053 break;
9054 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009055 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009056 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009057 }
9058
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009059 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009060}
9061
Junaid Shahideb4b2482018-06-27 14:59:14 -07009062static int handle_invpcid(struct kvm_vcpu *vcpu)
9063{
9064 u32 vmx_instruction_info;
9065 unsigned long type;
9066 bool pcid_enabled;
9067 gva_t gva;
9068 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009069 unsigned i;
9070 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009071 struct {
9072 u64 pcid;
9073 u64 gla;
9074 } operand;
9075
9076 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9077 kvm_queue_exception(vcpu, UD_VECTOR);
9078 return 1;
9079 }
9080
9081 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9082 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9083
9084 if (type > 3) {
9085 kvm_inject_gp(vcpu, 0);
9086 return 1;
9087 }
9088
9089 /* According to the Intel instruction reference, the memory operand
9090 * is read even if it isn't needed (e.g., for type==all)
9091 */
9092 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9093 vmx_instruction_info, false, &gva))
9094 return 1;
9095
9096 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9097 kvm_inject_page_fault(vcpu, &e);
9098 return 1;
9099 }
9100
9101 if (operand.pcid >> 12 != 0) {
9102 kvm_inject_gp(vcpu, 0);
9103 return 1;
9104 }
9105
9106 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9107
9108 switch (type) {
9109 case INVPCID_TYPE_INDIV_ADDR:
9110 if ((!pcid_enabled && (operand.pcid != 0)) ||
9111 is_noncanonical_address(operand.gla, vcpu)) {
9112 kvm_inject_gp(vcpu, 0);
9113 return 1;
9114 }
9115 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9116 return kvm_skip_emulated_instruction(vcpu);
9117
9118 case INVPCID_TYPE_SINGLE_CTXT:
9119 if (!pcid_enabled && (operand.pcid != 0)) {
9120 kvm_inject_gp(vcpu, 0);
9121 return 1;
9122 }
9123
9124 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9125 kvm_mmu_sync_roots(vcpu);
9126 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9127 }
9128
Junaid Shahidb94742c2018-06-27 14:59:20 -07009129 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009130 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07009131 == operand.pcid)
9132 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009133
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02009134 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009135 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009136 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009137 * given PCID, then nothing needs to be done here because a
9138 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009139 */
9140
9141 return kvm_skip_emulated_instruction(vcpu);
9142
9143 case INVPCID_TYPE_ALL_NON_GLOBAL:
9144 /*
9145 * Currently, KVM doesn't mark global entries in the shadow
9146 * page tables, so a non-global flush just degenerates to a
9147 * global flush. If needed, we could optimize this later by
9148 * keeping track of global entries in shadow page tables.
9149 */
9150
9151 /* fall-through */
9152 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9153 kvm_mmu_unload(vcpu);
9154 return kvm_skip_emulated_instruction(vcpu);
9155
9156 default:
9157 BUG(); /* We have already checked above that type <= 3 */
9158 }
9159}
9160
Kai Huang843e4332015-01-28 10:54:28 +08009161static int handle_pml_full(struct kvm_vcpu *vcpu)
9162{
9163 unsigned long exit_qualification;
9164
9165 trace_kvm_pml_full(vcpu->vcpu_id);
9166
9167 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9168
9169 /*
9170 * PML buffer FULL happened while executing iret from NMI,
9171 * "blocked by NMI" bit has to be set before next VM entry.
9172 */
9173 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009174 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009175 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9176 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9177 GUEST_INTR_STATE_NMI);
9178
9179 /*
9180 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9181 * here.., and there's no userspace involvement needed for PML.
9182 */
9183 return 1;
9184}
9185
Yunhong Jiang64672c92016-06-13 14:19:59 -07009186static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9187{
Sean Christophersond264ee02018-08-27 15:21:12 -07009188 if (!to_vmx(vcpu)->req_immediate_exit)
9189 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009190 return 1;
9191}
9192
Bandan Das41ab9372017-08-03 15:54:43 -04009193static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9194{
9195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009196 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9197
9198 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009199 switch (address & VMX_EPTP_MT_MASK) {
9200 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009201 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009202 return false;
9203 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009204 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009205 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009206 return false;
9207 break;
9208 default:
9209 return false;
9210 }
9211
David Hildenbrandbb97a012017-08-10 23:15:28 +02009212 /* only 4 levels page-walk length are valid */
9213 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009214 return false;
9215
9216 /* Reserved bits should not be set */
9217 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9218 return false;
9219
9220 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009221 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009222 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009223 return false;
9224 }
9225
9226 return true;
9227}
9228
9229static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9230 struct vmcs12 *vmcs12)
9231{
9232 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9233 u64 address;
9234 bool accessed_dirty;
9235 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9236
9237 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9238 !nested_cpu_has_ept(vmcs12))
9239 return 1;
9240
9241 if (index >= VMFUNC_EPTP_ENTRIES)
9242 return 1;
9243
9244
9245 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9246 &address, index * 8, 8))
9247 return 1;
9248
David Hildenbrandbb97a012017-08-10 23:15:28 +02009249 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009250
9251 /*
9252 * If the (L2) guest does a vmfunc to the currently
9253 * active ept pointer, we don't have to do anything else
9254 */
9255 if (vmcs12->ept_pointer != address) {
9256 if (!valid_ept_address(vcpu, address))
9257 return 1;
9258
9259 kvm_mmu_unload(vcpu);
9260 mmu->ept_ad = accessed_dirty;
Vitaly Kuznetsov36d95942018-10-08 21:28:10 +02009261 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Bandan Das41ab9372017-08-03 15:54:43 -04009262 vmcs12->ept_pointer = address;
9263 /*
9264 * TODO: Check what's the correct approach in case
9265 * mmu reload fails. Currently, we just let the next
9266 * reload potentially fail
9267 */
9268 kvm_mmu_reload(vcpu);
9269 }
9270
9271 return 0;
9272}
9273
Bandan Das2a499e42017-08-03 15:54:41 -04009274static int handle_vmfunc(struct kvm_vcpu *vcpu)
9275{
Bandan Das27c42a12017-08-03 15:54:42 -04009276 struct vcpu_vmx *vmx = to_vmx(vcpu);
9277 struct vmcs12 *vmcs12;
9278 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9279
9280 /*
9281 * VMFUNC is only supported for nested guests, but we always enable the
9282 * secondary control for simplicity; for non-nested mode, fake that we
9283 * didn't by injecting #UD.
9284 */
9285 if (!is_guest_mode(vcpu)) {
9286 kvm_queue_exception(vcpu, UD_VECTOR);
9287 return 1;
9288 }
9289
9290 vmcs12 = get_vmcs12(vcpu);
9291 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9292 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009293
9294 switch (function) {
9295 case 0:
9296 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9297 goto fail;
9298 break;
9299 default:
9300 goto fail;
9301 }
9302 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009303
9304fail:
9305 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9306 vmcs_read32(VM_EXIT_INTR_INFO),
9307 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009308 return 1;
9309}
9310
Sean Christopherson0b665d32018-08-14 09:33:34 -07009311static int handle_encls(struct kvm_vcpu *vcpu)
9312{
9313 /*
9314 * SGX virtualization is not yet supported. There is no software
9315 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9316 * to prevent the guest from executing ENCLS.
9317 */
9318 kvm_queue_exception(vcpu, UD_VECTOR);
9319 return 1;
9320}
9321
Nadav Har'El0140cae2011-05-25 23:06:28 +03009322/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009323 * The exit handlers return 1 if the exit was handled fully and guest execution
9324 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9325 * to be done to userspace and return 0.
9326 */
Mathias Krause772e0312012-08-30 01:30:19 +02009327static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009328 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9329 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009330 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009331 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009332 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009333 [EXIT_REASON_CR_ACCESS] = handle_cr,
9334 [EXIT_REASON_DR_ACCESS] = handle_dr,
9335 [EXIT_REASON_CPUID] = handle_cpuid,
9336 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9337 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9338 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9339 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009340 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009341 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009342 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009343 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009344 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009345 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009346 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009347 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009348 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009349 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009350 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009351 [EXIT_REASON_VMOFF] = handle_vmoff,
9352 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009353 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9354 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009355 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009356 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009357 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009358 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009359 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009360 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009361 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9362 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009363 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9364 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009365 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009366 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009367 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009368 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009369 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009370 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009371 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009372 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009373 [EXIT_REASON_XSAVES] = handle_xsaves,
9374 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009375 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009376 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009377 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009378 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009379 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009380};
9381
9382static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009383 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009384
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009385static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9386 struct vmcs12 *vmcs12)
9387{
9388 unsigned long exit_qualification;
9389 gpa_t bitmap, last_bitmap;
9390 unsigned int port;
9391 int size;
9392 u8 b;
9393
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009394 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009395 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009396
9397 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9398
9399 port = exit_qualification >> 16;
9400 size = (exit_qualification & 7) + 1;
9401
9402 last_bitmap = (gpa_t)-1;
9403 b = -1;
9404
9405 while (size > 0) {
9406 if (port < 0x8000)
9407 bitmap = vmcs12->io_bitmap_a;
9408 else if (port < 0x10000)
9409 bitmap = vmcs12->io_bitmap_b;
9410 else
Joe Perches1d804d02015-03-30 16:46:09 -07009411 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009412 bitmap += (port & 0x7fff) / 8;
9413
9414 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009415 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009416 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009417 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009418 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009419
9420 port++;
9421 size--;
9422 last_bitmap = bitmap;
9423 }
9424
Joe Perches1d804d02015-03-30 16:46:09 -07009425 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009426}
9427
Nadav Har'El644d7112011-05-25 23:12:35 +03009428/*
9429 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9430 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9431 * disinterest in the current event (read or write a specific MSR) by using an
9432 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9433 */
9434static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9435 struct vmcs12 *vmcs12, u32 exit_reason)
9436{
9437 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9438 gpa_t bitmap;
9439
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009440 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009441 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009442
9443 /*
9444 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9445 * for the four combinations of read/write and low/high MSR numbers.
9446 * First we need to figure out which of the four to use:
9447 */
9448 bitmap = vmcs12->msr_bitmap;
9449 if (exit_reason == EXIT_REASON_MSR_WRITE)
9450 bitmap += 2048;
9451 if (msr_index >= 0xc0000000) {
9452 msr_index -= 0xc0000000;
9453 bitmap += 1024;
9454 }
9455
9456 /* Then read the msr_index'th bit from this bitmap: */
9457 if (msr_index < 1024*8) {
9458 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009459 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009460 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009461 return 1 & (b >> (msr_index & 7));
9462 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009463 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009464}
9465
9466/*
9467 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9468 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9469 * intercept (via guest_host_mask etc.) the current event.
9470 */
9471static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9472 struct vmcs12 *vmcs12)
9473{
9474 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9475 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009476 int reg;
9477 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009478
9479 switch ((exit_qualification >> 4) & 3) {
9480 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009481 reg = (exit_qualification >> 8) & 15;
9482 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009483 switch (cr) {
9484 case 0:
9485 if (vmcs12->cr0_guest_host_mask &
9486 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009487 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009488 break;
9489 case 3:
9490 if ((vmcs12->cr3_target_count >= 1 &&
9491 vmcs12->cr3_target_value0 == val) ||
9492 (vmcs12->cr3_target_count >= 2 &&
9493 vmcs12->cr3_target_value1 == val) ||
9494 (vmcs12->cr3_target_count >= 3 &&
9495 vmcs12->cr3_target_value2 == val) ||
9496 (vmcs12->cr3_target_count >= 4 &&
9497 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009498 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009499 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009500 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009501 break;
9502 case 4:
9503 if (vmcs12->cr4_guest_host_mask &
9504 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009505 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009506 break;
9507 case 8:
9508 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009509 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009510 break;
9511 }
9512 break;
9513 case 2: /* clts */
9514 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9515 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009516 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009517 break;
9518 case 1: /* mov from cr */
9519 switch (cr) {
9520 case 3:
9521 if (vmcs12->cpu_based_vm_exec_control &
9522 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009523 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009524 break;
9525 case 8:
9526 if (vmcs12->cpu_based_vm_exec_control &
9527 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009528 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009529 break;
9530 }
9531 break;
9532 case 3: /* lmsw */
9533 /*
9534 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9535 * cr0. Other attempted changes are ignored, with no exit.
9536 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009537 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009538 if (vmcs12->cr0_guest_host_mask & 0xe &
9539 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009540 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009541 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9542 !(vmcs12->cr0_read_shadow & 0x1) &&
9543 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009544 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009545 break;
9546 }
Joe Perches1d804d02015-03-30 16:46:09 -07009547 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009548}
9549
Liran Alona7cde482018-06-23 02:35:10 +03009550static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9551 struct vmcs12 *vmcs12, gpa_t bitmap)
9552{
9553 u32 vmx_instruction_info;
9554 unsigned long field;
9555 u8 b;
9556
9557 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9558 return true;
9559
9560 /* Decode instruction info and find the field to access */
9561 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9562 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9563
9564 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9565 if (field >> 15)
9566 return true;
9567
9568 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9569 return true;
9570
9571 return 1 & (b >> (field & 7));
9572}
9573
Nadav Har'El644d7112011-05-25 23:12:35 +03009574/*
9575 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9576 * should handle it ourselves in L0 (and then continue L2). Only call this
9577 * when in is_guest_mode (L2).
9578 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009579static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009580{
Nadav Har'El644d7112011-05-25 23:12:35 +03009581 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9582 struct vcpu_vmx *vmx = to_vmx(vcpu);
9583 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9584
Jim Mattson4f350c62017-09-14 16:31:44 -07009585 if (vmx->nested.nested_run_pending)
9586 return false;
9587
9588 if (unlikely(vmx->fail)) {
9589 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9590 vmcs_read32(VM_INSTRUCTION_ERROR));
9591 return true;
9592 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009593
David Matlackc9f04402017-08-01 14:00:40 -07009594 /*
9595 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009596 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9597 * Page). The CPU may write to these pages via their host
9598 * physical address while L2 is running, bypassing any
9599 * address-translation-based dirty tracking (e.g. EPT write
9600 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009601 *
9602 * Mark them dirty on every exit from L2 to prevent them from
9603 * getting out of sync with dirty tracking.
9604 */
9605 nested_mark_vmcs12_pages_dirty(vcpu);
9606
Jim Mattson4f350c62017-09-14 16:31:44 -07009607 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9608 vmcs_readl(EXIT_QUALIFICATION),
9609 vmx->idt_vectoring_info,
9610 intr_info,
9611 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9612 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009613
9614 switch (exit_reason) {
9615 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009616 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009617 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009618 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009619 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009620 else if (is_debug(intr_info) &&
9621 vcpu->guest_debug &
9622 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9623 return false;
9624 else if (is_breakpoint(intr_info) &&
9625 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9626 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009627 return vmcs12->exception_bitmap &
9628 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9629 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009630 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009631 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009632 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009633 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009634 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009635 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009636 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009637 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009638 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009639 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009640 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009641 case EXIT_REASON_HLT:
9642 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9643 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009644 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009645 case EXIT_REASON_INVLPG:
9646 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9647 case EXIT_REASON_RDPMC:
9648 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009649 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009650 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009651 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009652 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009653 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009654 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009655 case EXIT_REASON_VMREAD:
9656 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9657 vmcs12->vmread_bitmap);
9658 case EXIT_REASON_VMWRITE:
9659 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9660 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009661 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9662 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009663 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009664 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009665 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009666 /*
9667 * VMX instructions trap unconditionally. This allows L1 to
9668 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9669 */
Joe Perches1d804d02015-03-30 16:46:09 -07009670 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009671 case EXIT_REASON_CR_ACCESS:
9672 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9673 case EXIT_REASON_DR_ACCESS:
9674 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9675 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009676 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009677 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9678 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009679 case EXIT_REASON_MSR_READ:
9680 case EXIT_REASON_MSR_WRITE:
9681 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9682 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009683 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009684 case EXIT_REASON_MWAIT_INSTRUCTION:
9685 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009686 case EXIT_REASON_MONITOR_TRAP_FLAG:
9687 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009688 case EXIT_REASON_MONITOR_INSTRUCTION:
9689 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9690 case EXIT_REASON_PAUSE_INSTRUCTION:
9691 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9692 nested_cpu_has2(vmcs12,
9693 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9694 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009695 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009696 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009697 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009698 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009699 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009700 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009701 /*
9702 * The controls for "virtualize APIC accesses," "APIC-
9703 * register virtualization," and "virtual-interrupt
9704 * delivery" only come from vmcs12.
9705 */
Joe Perches1d804d02015-03-30 16:46:09 -07009706 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009707 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009708 /*
9709 * L0 always deals with the EPT violation. If nested EPT is
9710 * used, and the nested mmu code discovers that the address is
9711 * missing in the guest EPT table (EPT12), the EPT violation
9712 * will be injected with nested_ept_inject_page_fault()
9713 */
Joe Perches1d804d02015-03-30 16:46:09 -07009714 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009715 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009716 /*
9717 * L2 never uses directly L1's EPT, but rather L0's own EPT
9718 * table (shadow on EPT) or a merged EPT table that L0 built
9719 * (EPT on EPT). So any problems with the structure of the
9720 * table is L0's fault.
9721 */
Joe Perches1d804d02015-03-30 16:46:09 -07009722 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009723 case EXIT_REASON_INVPCID:
9724 return
9725 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9726 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009727 case EXIT_REASON_WBINVD:
9728 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9729 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009730 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009731 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9732 /*
9733 * This should never happen, since it is not possible to
9734 * set XSS to a non-zero value---neither in L1 nor in L2.
9735 * If if it were, XSS would have to be checked against
9736 * the XSS exit bitmap in vmcs12.
9737 */
9738 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009739 case EXIT_REASON_PREEMPTION_TIMER:
9740 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009741 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009742 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009743 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009744 case EXIT_REASON_VMFUNC:
9745 /* VM functions are emulated through L2->L0 vmexits. */
9746 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009747 case EXIT_REASON_ENCLS:
9748 /* SGX is never exposed to L1 */
9749 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009750 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009751 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009752 }
9753}
9754
Paolo Bonzini7313c692017-07-27 10:31:25 +02009755static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9756{
9757 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9758
9759 /*
9760 * At this point, the exit interruption info in exit_intr_info
9761 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9762 * we need to query the in-kernel LAPIC.
9763 */
9764 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9765 if ((exit_intr_info &
9766 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9767 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9768 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9769 vmcs12->vm_exit_intr_error_code =
9770 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9771 }
9772
9773 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9774 vmcs_readl(EXIT_QUALIFICATION));
9775 return 1;
9776}
9777
Avi Kivity586f9602010-11-18 13:09:54 +02009778static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9779{
9780 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9781 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9782}
9783
Kai Huanga3eaa862015-11-04 13:46:05 +08009784static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009785{
Kai Huanga3eaa862015-11-04 13:46:05 +08009786 if (vmx->pml_pg) {
9787 __free_page(vmx->pml_pg);
9788 vmx->pml_pg = NULL;
9789 }
Kai Huang843e4332015-01-28 10:54:28 +08009790}
9791
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009792static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009793{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009794 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009795 u64 *pml_buf;
9796 u16 pml_idx;
9797
9798 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9799
9800 /* Do nothing if PML buffer is empty */
9801 if (pml_idx == (PML_ENTITY_NUM - 1))
9802 return;
9803
9804 /* PML index always points to next available PML buffer entity */
9805 if (pml_idx >= PML_ENTITY_NUM)
9806 pml_idx = 0;
9807 else
9808 pml_idx++;
9809
9810 pml_buf = page_address(vmx->pml_pg);
9811 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9812 u64 gpa;
9813
9814 gpa = pml_buf[pml_idx];
9815 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009816 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009817 }
9818
9819 /* reset PML index */
9820 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9821}
9822
9823/*
9824 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9825 * Called before reporting dirty_bitmap to userspace.
9826 */
9827static void kvm_flush_pml_buffers(struct kvm *kvm)
9828{
9829 int i;
9830 struct kvm_vcpu *vcpu;
9831 /*
9832 * We only need to kick vcpu out of guest mode here, as PML buffer
9833 * is flushed at beginning of all VMEXITs, and it's obvious that only
9834 * vcpus running in guest are possible to have unflushed GPAs in PML
9835 * buffer.
9836 */
9837 kvm_for_each_vcpu(i, vcpu, kvm)
9838 kvm_vcpu_kick(vcpu);
9839}
9840
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009841static void vmx_dump_sel(char *name, uint32_t sel)
9842{
9843 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009844 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009845 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9846 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9847 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9848}
9849
9850static void vmx_dump_dtsel(char *name, uint32_t limit)
9851{
9852 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9853 name, vmcs_read32(limit),
9854 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9855}
9856
9857static void dump_vmcs(void)
9858{
9859 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9860 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9861 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9862 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9863 u32 secondary_exec_control = 0;
9864 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009865 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009866 int i, n;
9867
9868 if (cpu_has_secondary_exec_ctrls())
9869 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9870
9871 pr_err("*** Guest State ***\n");
9872 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9873 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9874 vmcs_readl(CR0_GUEST_HOST_MASK));
9875 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9876 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9877 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9878 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9879 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9880 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009881 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9882 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9883 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9884 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009885 }
9886 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9887 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9888 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9889 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9890 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9891 vmcs_readl(GUEST_SYSENTER_ESP),
9892 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9893 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9894 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9895 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9896 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9897 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9898 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9899 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9900 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9901 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9902 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9903 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9904 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009905 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9906 efer, vmcs_read64(GUEST_IA32_PAT));
9907 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9908 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009909 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009910 if (cpu_has_load_perf_global_ctrl &&
9911 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009912 pr_err("PerfGlobCtl = 0x%016llx\n",
9913 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009914 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009915 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009916 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9917 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9918 vmcs_read32(GUEST_ACTIVITY_STATE));
9919 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9920 pr_err("InterruptStatus = %04x\n",
9921 vmcs_read16(GUEST_INTR_STATUS));
9922
9923 pr_err("*** Host State ***\n");
9924 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9925 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9926 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9927 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9928 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9929 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9930 vmcs_read16(HOST_TR_SELECTOR));
9931 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9932 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9933 vmcs_readl(HOST_TR_BASE));
9934 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9935 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9936 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9937 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9938 vmcs_readl(HOST_CR4));
9939 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9940 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9941 vmcs_read32(HOST_IA32_SYSENTER_CS),
9942 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9943 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009944 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9945 vmcs_read64(HOST_IA32_EFER),
9946 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009947 if (cpu_has_load_perf_global_ctrl &&
9948 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009949 pr_err("PerfGlobCtl = 0x%016llx\n",
9950 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009951
9952 pr_err("*** Control State ***\n");
9953 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9954 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9955 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9956 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9957 vmcs_read32(EXCEPTION_BITMAP),
9958 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9959 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9960 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9961 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9962 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9963 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9964 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9965 vmcs_read32(VM_EXIT_INTR_INFO),
9966 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9967 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9968 pr_err(" reason=%08x qualification=%016lx\n",
9969 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9970 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9971 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9972 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009973 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009974 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009975 pr_err("TSC Multiplier = 0x%016llx\n",
9976 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009977 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9978 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9979 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9980 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9981 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009982 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009983 n = vmcs_read32(CR3_TARGET_COUNT);
9984 for (i = 0; i + 1 < n; i += 4)
9985 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9986 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9987 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9988 if (i < n)
9989 pr_err("CR3 target%u=%016lx\n",
9990 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9991 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9992 pr_err("PLE Gap=%08x Window=%08x\n",
9993 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9994 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9995 pr_err("Virtual processor ID = 0x%04x\n",
9996 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9997}
9998
Avi Kivity6aa8b732006-12-10 02:21:36 -08009999/*
10000 * The guest has exited. See if we can fix it or if we need userspace
10001 * assistance.
10002 */
Avi Kivity851ba692009-08-24 11:10:17 +030010003static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010004{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010006 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010007 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010008
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010009 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10010
Kai Huang843e4332015-01-28 10:54:28 +080010011 /*
10012 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10013 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10014 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10015 * mode as if vcpus is in root mode, the PML buffer must has been
10016 * flushed already.
10017 */
10018 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010019 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010020
Mohammed Gamal80ced182009-09-01 12:48:18 +020010021 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010022 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010023 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010024
Paolo Bonzini7313c692017-07-27 10:31:25 +020010025 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10026 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010027
Mohammed Gamal51207022010-05-31 22:40:54 +030010028 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010029 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010030 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10031 vcpu->run->fail_entry.hardware_entry_failure_reason
10032 = exit_reason;
10033 return 0;
10034 }
10035
Avi Kivity29bd8a72007-09-10 17:27:03 +030010036 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010037 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10038 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010039 = vmcs_read32(VM_INSTRUCTION_ERROR);
10040 return 0;
10041 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010042
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010043 /*
10044 * Note:
10045 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10046 * delivery event since it indicates guest is accessing MMIO.
10047 * The vm-exit can be triggered again after return to guest that
10048 * will cause infinite loop.
10049 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010050 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010051 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010052 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010053 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010054 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10055 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10056 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010057 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010058 vcpu->run->internal.data[0] = vectoring_info;
10059 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010060 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10061 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10062 vcpu->run->internal.ndata++;
10063 vcpu->run->internal.data[3] =
10064 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10065 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010066 return 0;
10067 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010068
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010069 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010070 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10071 if (vmx_interrupt_allowed(vcpu)) {
10072 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10073 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10074 vcpu->arch.nmi_pending) {
10075 /*
10076 * This CPU don't support us in finding the end of an
10077 * NMI-blocked window if the guest runs with IRQs
10078 * disabled. So we pull the trigger after 1 s of
10079 * futile waiting, but inform the user about this.
10080 */
10081 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10082 "state on VCPU %d after 1 s timeout\n",
10083 __func__, vcpu->vcpu_id);
10084 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10085 }
10086 }
10087
Avi Kivity6aa8b732006-12-10 02:21:36 -080010088 if (exit_reason < kvm_vmx_max_exit_handlers
10089 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010090 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010091 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010092 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10093 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010094 kvm_queue_exception(vcpu, UD_VECTOR);
10095 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010096 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010097}
10098
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010099/*
10100 * Software based L1D cache flush which is used when microcode providing
10101 * the cache control MSR is not loaded.
10102 *
10103 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10104 * flush it is required to read in 64 KiB because the replacement algorithm
10105 * is not exactly LRU. This could be sized at runtime via topology
10106 * information but as all relevant affected CPUs have 32KiB L1D cache size
10107 * there is no point in doing so.
10108 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010109static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010110{
10111 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010112
10113 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010114 * This code is only executed when the the flush mode is 'cond' or
10115 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010116 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010117 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010118 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010119
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010120 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010121 * Clear the per-vcpu flush bit, it gets set again
10122 * either from vcpu_run() or from one of the unsafe
10123 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010124 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010125 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010126 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010127
10128 /*
10129 * Clear the per-cpu flush bit, it gets set again from
10130 * the interrupt handlers.
10131 */
10132 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10133 kvm_clear_cpu_l1tf_flush_l1d();
10134
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010135 if (!flush_l1d)
10136 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010137 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010138
10139 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010140
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010141 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10142 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10143 return;
10144 }
10145
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010146 asm volatile(
10147 /* First ensure the pages are in the TLB */
10148 "xorl %%eax, %%eax\n"
10149 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010150 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010151 "addl $4096, %%eax\n\t"
10152 "cmpl %%eax, %[size]\n\t"
10153 "jne .Lpopulate_tlb\n\t"
10154 "xorl %%eax, %%eax\n\t"
10155 "cpuid\n\t"
10156 /* Now fill the cache */
10157 "xorl %%eax, %%eax\n"
10158 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010159 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010160 "addl $64, %%eax\n\t"
10161 "cmpl %%eax, %[size]\n\t"
10162 "jne .Lfill_cache\n\t"
10163 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010164 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010165 [size] "r" (size)
10166 : "eax", "ebx", "ecx", "edx");
10167}
10168
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010169static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010170{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010171 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10172
10173 if (is_guest_mode(vcpu) &&
10174 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10175 return;
10176
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010177 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010178 vmcs_write32(TPR_THRESHOLD, 0);
10179 return;
10180 }
10181
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010182 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010183}
10184
Jim Mattson8d860bb2018-05-09 16:56:05 -040010185static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010186{
10187 u32 sec_exec_control;
10188
Jim Mattson8d860bb2018-05-09 16:56:05 -040010189 if (!lapic_in_kernel(vcpu))
10190 return;
10191
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010192 if (!flexpriority_enabled &&
10193 !cpu_has_vmx_virtualize_x2apic_mode())
10194 return;
10195
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010196 /* Postpone execution until vmcs01 is the current VMCS. */
10197 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010198 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010199 return;
10200 }
10201
Yang Zhang8d146952013-01-25 10:18:50 +080010202 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010203 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10204 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010205
Jim Mattson8d860bb2018-05-09 16:56:05 -040010206 switch (kvm_get_apic_mode(vcpu)) {
10207 case LAPIC_MODE_INVALID:
10208 WARN_ONCE(true, "Invalid local APIC state");
10209 case LAPIC_MODE_DISABLED:
10210 break;
10211 case LAPIC_MODE_XAPIC:
10212 if (flexpriority_enabled) {
10213 sec_exec_control |=
10214 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10215 vmx_flush_tlb(vcpu, true);
10216 }
10217 break;
10218 case LAPIC_MODE_X2APIC:
10219 if (cpu_has_vmx_virtualize_x2apic_mode())
10220 sec_exec_control |=
10221 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10222 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010223 }
10224 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10225
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010226 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010227}
10228
Tang Chen38b99172014-09-24 15:57:54 +080010229static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10230{
Jim Mattsonab5df312018-05-09 17:02:03 -040010231 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010232 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010233 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010234 }
Tang Chen38b99172014-09-24 15:57:54 +080010235}
10236
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010237static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010238{
10239 u16 status;
10240 u8 old;
10241
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010242 if (max_isr == -1)
10243 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010244
10245 status = vmcs_read16(GUEST_INTR_STATUS);
10246 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010247 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010248 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010249 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010250 vmcs_write16(GUEST_INTR_STATUS, status);
10251 }
10252}
10253
10254static void vmx_set_rvi(int vector)
10255{
10256 u16 status;
10257 u8 old;
10258
Wei Wang4114c272014-11-05 10:53:43 +080010259 if (vector == -1)
10260 vector = 0;
10261
Yang Zhangc7c9c562013-01-25 10:18:51 +080010262 status = vmcs_read16(GUEST_INTR_STATUS);
10263 old = (u8)status & 0xff;
10264 if ((u8)vector != old) {
10265 status &= ~0xff;
10266 status |= (u8)vector;
10267 vmcs_write16(GUEST_INTR_STATUS, status);
10268 }
10269}
10270
10271static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10272{
Liran Alon851c1a182017-12-24 18:12:56 +020010273 /*
10274 * When running L2, updating RVI is only relevant when
10275 * vmcs12 virtual-interrupt-delivery enabled.
10276 * However, it can be enabled only when L1 also
10277 * intercepts external-interrupts and in that case
10278 * we should not update vmcs02 RVI but instead intercept
10279 * interrupt. Therefore, do nothing when running L2.
10280 */
10281 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010282 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010283}
10284
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010285static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010286{
10287 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010288 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010289 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010290
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010291 WARN_ON(!vcpu->arch.apicv_active);
10292 if (pi_test_on(&vmx->pi_desc)) {
10293 pi_clear_on(&vmx->pi_desc);
10294 /*
10295 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10296 * But on x86 this is just a compiler barrier anyway.
10297 */
10298 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010299 max_irr_updated =
10300 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10301
10302 /*
10303 * If we are running L2 and L1 has a new pending interrupt
10304 * which can be injected, we should re-evaluate
10305 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010306 * If L1 intercepts external-interrupts, we should
10307 * exit from L2 to L1. Otherwise, interrupt should be
10308 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010309 */
Liran Alon851c1a182017-12-24 18:12:56 +020010310 if (is_guest_mode(vcpu) && max_irr_updated) {
10311 if (nested_exit_on_intr(vcpu))
10312 kvm_vcpu_exiting_guest_mode(vcpu);
10313 else
10314 kvm_make_request(KVM_REQ_EVENT, vcpu);
10315 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010316 } else {
10317 max_irr = kvm_lapic_find_highest_irr(vcpu);
10318 }
10319 vmx_hwapic_irr_update(vcpu, max_irr);
10320 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010321}
10322
Paolo Bonzini7e712682018-10-03 13:44:26 +020010323static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10324{
10325 u8 rvi = vmx_get_rvi();
10326 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10327
10328 return ((rvi & 0xf0) > (vppr & 0xf0));
10329}
10330
Andrey Smetanin63086302015-11-10 15:36:32 +030010331static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010332{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010333 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010334 return;
10335
Yang Zhangc7c9c562013-01-25 10:18:51 +080010336 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10337 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10338 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10339 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10340}
10341
Paolo Bonzini967235d2016-12-19 14:03:45 +010010342static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10343{
10344 struct vcpu_vmx *vmx = to_vmx(vcpu);
10345
10346 pi_clear_on(&vmx->pi_desc);
10347 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10348}
10349
Avi Kivity51aa01d2010-07-20 14:31:20 +030010350static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010351{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010352 u32 exit_intr_info = 0;
10353 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010354
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010355 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10356 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010357 return;
10358
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010359 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10360 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10361 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010362
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010363 /* if exit due to PF check for async PF */
10364 if (is_page_fault(exit_intr_info))
10365 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10366
Andi Kleena0861c02009-06-08 17:37:09 +080010367 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010368 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10369 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010370 kvm_machine_check();
10371
Gleb Natapov20f65982009-05-11 13:35:55 +030010372 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010373 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010374 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010375 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010376 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010377 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010378}
Gleb Natapov20f65982009-05-11 13:35:55 +030010379
Yang Zhanga547c6d2013-04-11 19:25:10 +080010380static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10381{
10382 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10383
Yang Zhanga547c6d2013-04-11 19:25:10 +080010384 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10385 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10386 unsigned int vector;
10387 unsigned long entry;
10388 gate_desc *desc;
10389 struct vcpu_vmx *vmx = to_vmx(vcpu);
10390#ifdef CONFIG_X86_64
10391 unsigned long tmp;
10392#endif
10393
10394 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10395 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010396 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010397 asm volatile(
10398#ifdef CONFIG_X86_64
10399 "mov %%" _ASM_SP ", %[sp]\n\t"
10400 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10401 "push $%c[ss]\n\t"
10402 "push %[sp]\n\t"
10403#endif
10404 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010405 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010406 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010407 :
10408#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010409 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010410#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010411 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010412 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010413 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010414 [ss]"i"(__KERNEL_DS),
10415 [cs]"i"(__KERNEL_CS)
10416 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010417 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010418}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010419STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010420
Tom Lendackybc226f02018-05-10 22:06:39 +020010421static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010422{
Tom Lendackybc226f02018-05-10 22:06:39 +020010423 switch (index) {
10424 case MSR_IA32_SMBASE:
10425 /*
10426 * We cannot do SMM unless we can run the guest in big
10427 * real mode.
10428 */
10429 return enable_unrestricted_guest || emulate_invalid_guest_state;
10430 case MSR_AMD64_VIRT_SPEC_CTRL:
10431 /* This is AMD only. */
10432 return false;
10433 default:
10434 return true;
10435 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010436}
10437
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010438static bool vmx_mpx_supported(void)
10439{
10440 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10441 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10442}
10443
Wanpeng Li55412b22014-12-02 19:21:30 +080010444static bool vmx_xsaves_supported(void)
10445{
10446 return vmcs_config.cpu_based_2nd_exec_ctrl &
10447 SECONDARY_EXEC_XSAVES;
10448}
10449
Avi Kivity51aa01d2010-07-20 14:31:20 +030010450static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10451{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010452 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010453 bool unblock_nmi;
10454 u8 vector;
10455 bool idtv_info_valid;
10456
10457 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010458
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010459 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010460 if (vmx->loaded_vmcs->nmi_known_unmasked)
10461 return;
10462 /*
10463 * Can't use vmx->exit_intr_info since we're not sure what
10464 * the exit reason is.
10465 */
10466 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10467 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10468 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10469 /*
10470 * SDM 3: 27.7.1.2 (September 2008)
10471 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10472 * a guest IRET fault.
10473 * SDM 3: 23.2.2 (September 2008)
10474 * Bit 12 is undefined in any of the following cases:
10475 * If the VM exit sets the valid bit in the IDT-vectoring
10476 * information field.
10477 * If the VM exit is due to a double fault.
10478 */
10479 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10480 vector != DF_VECTOR && !idtv_info_valid)
10481 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10482 GUEST_INTR_STATE_NMI);
10483 else
10484 vmx->loaded_vmcs->nmi_known_unmasked =
10485 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10486 & GUEST_INTR_STATE_NMI);
10487 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10488 vmx->loaded_vmcs->vnmi_blocked_time +=
10489 ktime_to_ns(ktime_sub(ktime_get(),
10490 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010491}
10492
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010493static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010494 u32 idt_vectoring_info,
10495 int instr_len_field,
10496 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010497{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010498 u8 vector;
10499 int type;
10500 bool idtv_info_valid;
10501
10502 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010503
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010504 vcpu->arch.nmi_injected = false;
10505 kvm_clear_exception_queue(vcpu);
10506 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010507
10508 if (!idtv_info_valid)
10509 return;
10510
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010511 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010512
Avi Kivity668f6122008-07-02 09:28:55 +030010513 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10514 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010515
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010516 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010517 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010518 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010519 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010520 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010521 * Clear bit "block by NMI" before VM entry if a NMI
10522 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010523 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010524 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010525 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010526 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010527 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010528 /* fall through */
10529 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010530 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010531 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010532 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010533 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010534 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010535 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010536 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010537 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010538 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010539 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010540 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010541 break;
10542 default:
10543 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010544 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010545}
10546
Avi Kivity83422e12010-07-20 14:43:23 +030010547static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10548{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010549 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010550 VM_EXIT_INSTRUCTION_LEN,
10551 IDT_VECTORING_ERROR_CODE);
10552}
10553
Avi Kivityb463a6f2010-07-20 15:06:17 +030010554static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10555{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010556 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010557 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10558 VM_ENTRY_INSTRUCTION_LEN,
10559 VM_ENTRY_EXCEPTION_ERROR_CODE);
10560
10561 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10562}
10563
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010564static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10565{
10566 int i, nr_msrs;
10567 struct perf_guest_switch_msr *msrs;
10568
10569 msrs = perf_guest_get_msrs(&nr_msrs);
10570
10571 if (!msrs)
10572 return;
10573
10574 for (i = 0; i < nr_msrs; i++)
10575 if (msrs[i].host == msrs[i].guest)
10576 clear_atomic_switch_msr(vmx, msrs[i].msr);
10577 else
10578 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010579 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010580}
10581
Sean Christophersonf459a702018-08-27 15:21:11 -070010582static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10583{
10584 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10585 if (!vmx->loaded_vmcs->hv_timer_armed)
10586 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10587 PIN_BASED_VMX_PREEMPTION_TIMER);
10588 vmx->loaded_vmcs->hv_timer_armed = true;
10589}
10590
10591static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010592{
10593 struct vcpu_vmx *vmx = to_vmx(vcpu);
10594 u64 tscl;
10595 u32 delta_tsc;
10596
Sean Christophersond264ee02018-08-27 15:21:12 -070010597 if (vmx->req_immediate_exit) {
10598 vmx_arm_hv_timer(vmx, 0);
10599 return;
10600 }
10601
Sean Christophersonf459a702018-08-27 15:21:11 -070010602 if (vmx->hv_deadline_tsc != -1) {
10603 tscl = rdtsc();
10604 if (vmx->hv_deadline_tsc > tscl)
10605 /* set_hv_timer ensures the delta fits in 32-bits */
10606 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10607 cpu_preemption_timer_multi);
10608 else
10609 delta_tsc = 0;
10610
10611 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010612 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010613 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010614
Sean Christophersonf459a702018-08-27 15:21:11 -070010615 if (vmx->loaded_vmcs->hv_timer_armed)
10616 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10617 PIN_BASED_VMX_PREEMPTION_TIMER);
10618 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010619}
10620
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010621static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010622{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010623 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010624 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010625
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010626 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010627 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010628 vmx->loaded_vmcs->soft_vnmi_blocked))
10629 vmx->loaded_vmcs->entry_time = ktime_get();
10630
Avi Kivity104f2262010-11-18 13:12:52 +020010631 /* Don't enter VMX if guest state is invalid, let the exit handler
10632 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010633 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010634 return;
10635
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010636 if (vmx->ple_window_dirty) {
10637 vmx->ple_window_dirty = false;
10638 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10639 }
10640
Abel Gordon012f83c2013-04-18 14:39:25 +030010641 if (vmx->nested.sync_shadow_vmcs) {
10642 copy_vmcs12_to_shadow(vmx);
10643 vmx->nested.sync_shadow_vmcs = false;
10644 }
10645
Avi Kivity104f2262010-11-18 13:12:52 +020010646 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10647 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10648 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10649 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10650
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010651 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010652 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010653 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010654 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010655 }
10656
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010657 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010658 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010659 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010660 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010661 }
10662
Avi Kivity104f2262010-11-18 13:12:52 +020010663 /* When single-stepping over STI and MOV SS, we must clear the
10664 * corresponding interruptibility bits in the guest state. Otherwise
10665 * vmentry fails as it then expects bit 14 (BS) in pending debug
10666 * exceptions being set, but that's not correct for the guest debugging
10667 * case. */
10668 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10669 vmx_set_interrupt_shadow(vcpu, 0);
10670
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010671 if (static_cpu_has(X86_FEATURE_PKU) &&
10672 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10673 vcpu->arch.pkru != vmx->host_pkru)
10674 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010675
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010676 atomic_switch_perf_msrs(vmx);
10677
Sean Christophersonf459a702018-08-27 15:21:11 -070010678 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010679
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010680 /*
10681 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10682 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10683 * is no need to worry about the conditional branch over the wrmsr
10684 * being speculatively taken.
10685 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010686 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010687
Nadav Har'Eld462b812011-05-24 15:26:10 +030010688 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010689
10690 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10691 (unsigned long)&current_evmcs->host_rsp : 0;
10692
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010693 if (static_branch_unlikely(&vmx_l1d_should_flush))
10694 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010695
Avi Kivity104f2262010-11-18 13:12:52 +020010696 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010697 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010698 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10699 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10700 "push %%" _ASM_CX " \n\t"
10701 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010702 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010703 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010704 /* Avoid VMWRITE when Enlightened VMCS is in use */
10705 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10706 "jz 2f \n\t"
10707 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10708 "jmp 1f \n\t"
10709 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010710 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010711 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010712 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010713 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10714 "mov %%cr2, %%" _ASM_DX " \n\t"
10715 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010716 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010717 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010718 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010719 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010720 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010721 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010722 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10723 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10724 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10725 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10726 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10727 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010728#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010729 "mov %c[r8](%0), %%r8 \n\t"
10730 "mov %c[r9](%0), %%r9 \n\t"
10731 "mov %c[r10](%0), %%r10 \n\t"
10732 "mov %c[r11](%0), %%r11 \n\t"
10733 "mov %c[r12](%0), %%r12 \n\t"
10734 "mov %c[r13](%0), %%r13 \n\t"
10735 "mov %c[r14](%0), %%r14 \n\t"
10736 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010737#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010738 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010739
Avi Kivity6aa8b732006-12-10 02:21:36 -080010740 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010741 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010742 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010743 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010744 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010745 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010746 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010747 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010748 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010749 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010750 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10751 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10752 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10753 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10754 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10755 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10756 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010757#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010758 "mov %%r8, %c[r8](%0) \n\t"
10759 "mov %%r9, %c[r9](%0) \n\t"
10760 "mov %%r10, %c[r10](%0) \n\t"
10761 "mov %%r11, %c[r11](%0) \n\t"
10762 "mov %%r12, %c[r12](%0) \n\t"
10763 "mov %%r13, %c[r13](%0) \n\t"
10764 "mov %%r14, %c[r14](%0) \n\t"
10765 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010766 "xor %%r8d, %%r8d \n\t"
10767 "xor %%r9d, %%r9d \n\t"
10768 "xor %%r10d, %%r10d \n\t"
10769 "xor %%r11d, %%r11d \n\t"
10770 "xor %%r12d, %%r12d \n\t"
10771 "xor %%r13d, %%r13d \n\t"
10772 "xor %%r14d, %%r14d \n\t"
10773 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010774#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010775 "mov %%cr2, %%" _ASM_AX " \n\t"
10776 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010777
Jim Mattson0cb5b302018-01-03 14:31:38 -080010778 "xor %%eax, %%eax \n\t"
10779 "xor %%ebx, %%ebx \n\t"
10780 "xor %%esi, %%esi \n\t"
10781 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010782 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010783 ".pushsection .rodata \n\t"
10784 ".global vmx_return \n\t"
10785 "vmx_return: " _ASM_PTR " 2b \n\t"
10786 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010787 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010788 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010789 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010790 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010791 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10792 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10793 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10794 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10795 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10796 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10797 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010798#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010799 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10800 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10801 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10802 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10803 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10804 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10805 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10806 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010807#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010808 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10809 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010810 : "cc", "memory"
10811#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010812 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010813 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010814#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010815 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010816#endif
10817 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010818
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010819 /*
10820 * We do not use IBRS in the kernel. If this vCPU has used the
10821 * SPEC_CTRL MSR it may have left it on; save the value and
10822 * turn it off. This is much more efficient than blindly adding
10823 * it to the atomic save/restore list. Especially as the former
10824 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10825 *
10826 * For non-nested case:
10827 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10828 * save it.
10829 *
10830 * For nested case:
10831 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10832 * save it.
10833 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010834 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010835 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010836
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010837 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010838
David Woodhouse117cc7a2018-01-12 11:11:27 +000010839 /* Eliminate branch target predictions from guest mode */
10840 vmexit_fill_RSB();
10841
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010842 /* All fields are clean at this point */
10843 if (static_branch_unlikely(&enable_evmcs))
10844 current_evmcs->hv_clean_fields |=
10845 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10846
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010847 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010848 if (vmx->host_debugctlmsr)
10849 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010850
Avi Kivityaa67f602012-08-01 16:48:03 +030010851#ifndef CONFIG_X86_64
10852 /*
10853 * The sysexit path does not restore ds/es, so we must set them to
10854 * a reasonable value ourselves.
10855 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010856 * We can't defer this to vmx_prepare_switch_to_host() since that
10857 * function may be executed in interrupt context, which saves and
10858 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010859 */
10860 loadsegment(ds, __USER_DS);
10861 loadsegment(es, __USER_DS);
10862#endif
10863
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010864 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010865 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010866 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010867 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010868 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010869 vcpu->arch.regs_dirty = 0;
10870
Gleb Natapove0b890d2013-09-25 12:51:33 +030010871 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010872 * eager fpu is enabled if PKEY is supported and CR4 is switched
10873 * back on host, so it is safe to read guest PKRU from current
10874 * XSAVE.
10875 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010876 if (static_cpu_has(X86_FEATURE_PKU) &&
10877 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10878 vcpu->arch.pkru = __read_pkru();
10879 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010880 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010881 }
10882
Gleb Natapove0b890d2013-09-25 12:51:33 +030010883 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010884 vmx->idt_vectoring_info = 0;
10885
10886 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10887 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10888 return;
10889
10890 vmx->loaded_vmcs->launched = 1;
10891 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010892
Avi Kivity51aa01d2010-07-20 14:31:20 +030010893 vmx_complete_atomic_exit(vmx);
10894 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010895 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010896}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010897STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010898
Sean Christopherson434a1e92018-03-20 12:17:18 -070010899static struct kvm *vmx_vm_alloc(void)
10900{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010901 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010902 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010903}
10904
10905static void vmx_vm_free(struct kvm *kvm)
10906{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010907 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010908}
10909
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010910static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010911{
10912 struct vcpu_vmx *vmx = to_vmx(vcpu);
10913 int cpu;
10914
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010915 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010916 return;
10917
10918 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010919 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010920 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010921 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010922 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070010923
10924 vm_entry_controls_reset_shadow(vmx);
10925 vm_exit_controls_reset_shadow(vmx);
10926 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010927}
10928
Jim Mattson2f1fe812016-07-08 15:36:06 -070010929/*
10930 * Ensure that the current vmcs of the logical processor is the
10931 * vmcs01 of the vcpu before calling free_nested().
10932 */
10933static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10934{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020010935 vcpu_load(vcpu);
10936 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
10937 free_nested(vcpu);
10938 vcpu_put(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010939}
10940
Avi Kivity6aa8b732006-12-10 02:21:36 -080010941static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10942{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010943 struct vcpu_vmx *vmx = to_vmx(vcpu);
10944
Kai Huang843e4332015-01-28 10:54:28 +080010945 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010946 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010947 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010948 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010949 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010950 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010951 kfree(vmx->guest_msrs);
10952 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010953 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010954}
10955
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010956static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010957{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010958 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010959 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010960 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010961 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010962
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010963 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010964 return ERR_PTR(-ENOMEM);
10965
Wanpeng Li991e7a02015-09-16 17:30:05 +080010966 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010967
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010968 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10969 if (err)
10970 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010971
Peter Feiner4e595162016-07-07 14:49:58 -070010972 err = -ENOMEM;
10973
10974 /*
10975 * If PML is turned on, failure on enabling PML just results in failure
10976 * of creating the vcpu, therefore we can simplify PML logic (by
10977 * avoiding dealing with cases, such as enabling PML partially on vcpus
10978 * for the guest, etc.
10979 */
10980 if (enable_pml) {
10981 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10982 if (!vmx->pml_pg)
10983 goto uninit_vcpu;
10984 }
10985
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010986 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010987 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10988 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010989
Peter Feiner4e595162016-07-07 14:49:58 -070010990 if (!vmx->guest_msrs)
10991 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010992
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010993 err = alloc_loaded_vmcs(&vmx->vmcs01);
10994 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010995 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010996
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010997 msr_bitmap = vmx->vmcs01.msr_bitmap;
10998 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10999 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11000 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11001 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11002 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11003 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11004 vmx->msr_bitmap_mode = 0;
11005
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011006 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011007 cpu = get_cpu();
11008 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011009 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011010 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011011 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011012 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011013 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011014 err = alloc_apic_access_page(kvm);
11015 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011016 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011017 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011018
Sean Christophersone90008d2018-03-05 12:04:37 -080011019 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011020 err = init_rmode_identity_map(kvm);
11021 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011022 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011023 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011024
Roman Kagan63aff652018-07-19 21:59:07 +030011025 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011026 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11027 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011028
Wincy Van705699a2015-02-03 23:58:17 +080011029 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011030 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011031
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011032 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11033
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011034 /*
11035 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11036 * or POSTED_INTR_WAKEUP_VECTOR.
11037 */
11038 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11039 vmx->pi_desc.sn = 1;
11040
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011041 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011042
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011043free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011044 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011045free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011046 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011047free_pml:
11048 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011049uninit_vcpu:
11050 kvm_vcpu_uninit(&vmx->vcpu);
11051free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011052 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011053 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011054 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011055}
11056
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011057#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"
11058#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 -040011059
Wanpeng Lib31c1142018-03-12 04:53:04 -070011060static int vmx_vm_init(struct kvm *kvm)
11061{
Tianyu Lan877ad952018-07-19 08:40:23 +000011062 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11063
Wanpeng Lib31c1142018-03-12 04:53:04 -070011064 if (!ple_gap)
11065 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011066
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011067 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11068 switch (l1tf_mitigation) {
11069 case L1TF_MITIGATION_OFF:
11070 case L1TF_MITIGATION_FLUSH_NOWARN:
11071 /* 'I explicitly don't care' is set */
11072 break;
11073 case L1TF_MITIGATION_FLUSH:
11074 case L1TF_MITIGATION_FLUSH_NOSMT:
11075 case L1TF_MITIGATION_FULL:
11076 /*
11077 * Warn upon starting the first VM in a potentially
11078 * insecure environment.
11079 */
11080 if (cpu_smt_control == CPU_SMT_ENABLED)
11081 pr_warn_once(L1TF_MSG_SMT);
11082 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11083 pr_warn_once(L1TF_MSG_L1D);
11084 break;
11085 case L1TF_MITIGATION_FULL_FORCE:
11086 /* Flush is enforced */
11087 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011088 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011089 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011090 return 0;
11091}
11092
Yang, Sheng002c7f72007-07-31 14:23:01 +030011093static void __init vmx_check_processor_compat(void *rtn)
11094{
11095 struct vmcs_config vmcs_conf;
11096
11097 *(int *)rtn = 0;
11098 if (setup_vmcs_config(&vmcs_conf) < 0)
11099 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011100 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011101 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11102 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11103 smp_processor_id());
11104 *(int *)rtn = -EIO;
11105 }
11106}
11107
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011108static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011109{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011110 u8 cache;
11111 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011112
Sheng Yang522c68c2009-04-27 20:35:43 +080011113 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011114 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011115 * 2. EPT with VT-d:
11116 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011117 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011118 * b. VT-d with snooping control feature: snooping control feature of
11119 * VT-d engine can guarantee the cache correctness. Just set it
11120 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011121 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011122 * consistent with host MTRR
11123 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011124 if (is_mmio) {
11125 cache = MTRR_TYPE_UNCACHABLE;
11126 goto exit;
11127 }
11128
11129 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011130 ipat = VMX_EPT_IPAT_BIT;
11131 cache = MTRR_TYPE_WRBACK;
11132 goto exit;
11133 }
11134
11135 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11136 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011137 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011138 cache = MTRR_TYPE_WRBACK;
11139 else
11140 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011141 goto exit;
11142 }
11143
Xiao Guangrongff536042015-06-15 16:55:22 +080011144 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011145
11146exit:
11147 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011148}
11149
Sheng Yang17cc3932010-01-05 19:02:27 +080011150static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011151{
Sheng Yang878403b2010-01-05 19:02:29 +080011152 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11153 return PT_DIRECTORY_LEVEL;
11154 else
11155 /* For shadow and EPT supported 1GB page */
11156 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011157}
11158
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011159static void vmcs_set_secondary_exec_control(u32 new_ctl)
11160{
11161 /*
11162 * These bits in the secondary execution controls field
11163 * are dynamic, the others are mostly based on the hypervisor
11164 * architecture and the guest's CPUID. Do not touch the
11165 * dynamic bits.
11166 */
11167 u32 mask =
11168 SECONDARY_EXEC_SHADOW_VMCS |
11169 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011170 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11171 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011172
11173 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11174
11175 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11176 (new_ctl & ~mask) | (cur_ctl & mask));
11177}
11178
David Matlack8322ebb2016-11-29 18:14:09 -080011179/*
11180 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11181 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11182 */
11183static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11184{
11185 struct vcpu_vmx *vmx = to_vmx(vcpu);
11186 struct kvm_cpuid_entry2 *entry;
11187
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011188 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11189 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011190
11191#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11192 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011193 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011194} while (0)
11195
11196 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11197 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11198 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11199 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11200 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11201 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11202 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11203 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11204 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11205 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11206 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11207 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11208 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11209 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11210 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11211
11212 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11213 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11214 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11215 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11216 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011217 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011218
11219#undef cr4_fixed1_update
11220}
11221
Liran Alon5f76f6f2018-09-14 03:25:52 +030011222static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11223{
11224 struct vcpu_vmx *vmx = to_vmx(vcpu);
11225
11226 if (kvm_mpx_supported()) {
11227 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11228
11229 if (mpx_enabled) {
11230 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11231 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11232 } else {
11233 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11234 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11235 }
11236 }
11237}
11238
Sheng Yang0e851882009-12-18 16:48:46 +080011239static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11240{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011241 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011242
Paolo Bonzini80154d72017-08-24 13:55:35 +020011243 if (cpu_has_secondary_exec_ctrls()) {
11244 vmx_compute_secondary_exec_control(vmx);
11245 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011246 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011247
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011248 if (nested_vmx_allowed(vcpu))
11249 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11250 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11251 else
11252 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11253 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011254
Liran Alon5f76f6f2018-09-14 03:25:52 +030011255 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011256 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011257 nested_vmx_entry_exit_ctls_update(vcpu);
11258 }
Sheng Yang0e851882009-12-18 16:48:46 +080011259}
11260
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011261static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11262{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011263 if (func == 1 && nested)
11264 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011265}
11266
Yang Zhang25d92082013-08-06 12:00:32 +030011267static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11268 struct x86_exception *fault)
11269{
Jan Kiszka533558b2014-01-04 18:47:20 +010011270 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011271 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011272 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011273 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011274
Bandan Dasc5f983f2017-05-05 15:25:14 -040011275 if (vmx->nested.pml_full) {
11276 exit_reason = EXIT_REASON_PML_FULL;
11277 vmx->nested.pml_full = false;
11278 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11279 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011280 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011281 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011282 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011283
11284 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011285 vmcs12->guest_physical_address = fault->address;
11286}
11287
Peter Feiner995f00a2017-06-30 17:26:32 -070011288static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11289{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011290 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011291}
11292
Nadav Har'El155a97a2013-08-05 11:07:16 +030011293/* Callbacks for nested_ept_init_mmu_context: */
11294
11295static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11296{
11297 /* return the page table to be shadowed - in our case, EPT12 */
11298 return get_vmcs12(vcpu)->ept_pointer;
11299}
11300
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011301static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011302{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011303 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011304
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011305 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +020011306 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011307 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011308 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011309 nested_ept_ad_enabled(vcpu),
11310 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011311 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
11312 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
11313 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +020011314 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011315
11316 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011317}
11318
11319static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11320{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011321 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011322 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011323}
11324
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011325static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11326 u16 error_code)
11327{
11328 bool inequality, bit;
11329
11330 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11331 inequality =
11332 (error_code & vmcs12->page_fault_error_code_mask) !=
11333 vmcs12->page_fault_error_code_match;
11334 return inequality ^ bit;
11335}
11336
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011337static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11338 struct x86_exception *fault)
11339{
11340 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11341
11342 WARN_ON(!is_guest_mode(vcpu));
11343
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011344 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11345 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011346 vmcs12->vm_exit_intr_error_code = fault->error_code;
11347 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11348 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11349 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11350 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011351 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011352 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011353 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011354}
11355
Paolo Bonzinic9923842017-12-13 14:16:30 +010011356static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11357 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011358
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011359static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011360{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011361 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011362 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011363 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011364 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011365
11366 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011367 /*
11368 * Translate L1 physical address to host physical
11369 * address for vmcs02. Keep the page pinned, so this
11370 * physical address remains valid. We keep a reference
11371 * to it so we can release it later.
11372 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011373 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011374 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011375 vmx->nested.apic_access_page = NULL;
11376 }
11377 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011378 /*
11379 * If translation failed, no matter: This feature asks
11380 * to exit when accessing the given address, and if it
11381 * can never be accessed, this feature won't do
11382 * anything anyway.
11383 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011384 if (!is_error_page(page)) {
11385 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011386 hpa = page_to_phys(vmx->nested.apic_access_page);
11387 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11388 } else {
11389 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11390 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11391 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011392 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011393
11394 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011395 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011396 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011397 vmx->nested.virtual_apic_page = NULL;
11398 }
11399 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011400
11401 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011402 * If translation failed, VM entry will fail because
11403 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11404 * Failing the vm entry is _not_ what the processor
11405 * does but it's basically the only possibility we
11406 * have. We could still enter the guest if CR8 load
11407 * exits are enabled, CR8 store exits are enabled, and
11408 * virtualize APIC access is disabled; in this case
11409 * the processor would never use the TPR shadow and we
11410 * could simply clear the bit from the execution
11411 * control. But such a configuration is useless, so
11412 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011413 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011414 if (!is_error_page(page)) {
11415 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011416 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11417 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11418 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011419 }
11420
Wincy Van705699a2015-02-03 23:58:17 +080011421 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011422 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11423 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011424 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011425 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011426 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011427 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11428 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011429 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011430 vmx->nested.pi_desc_page = page;
11431 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011432 vmx->nested.pi_desc =
11433 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11434 (unsigned long)(vmcs12->posted_intr_desc_addr &
11435 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011436 vmcs_write64(POSTED_INTR_DESC_ADDR,
11437 page_to_phys(vmx->nested.pi_desc_page) +
11438 (unsigned long)(vmcs12->posted_intr_desc_addr &
11439 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011440 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011441 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011442 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11443 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011444 else
11445 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11446 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011447}
11448
Jan Kiszkaf4124502014-03-07 20:03:13 +010011449static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11450{
11451 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11452 struct vcpu_vmx *vmx = to_vmx(vcpu);
11453
Sean Christopherson4c008122018-08-27 15:21:10 -070011454 /*
11455 * A timer value of zero is architecturally guaranteed to cause
11456 * a VMExit prior to executing any instructions in the guest.
11457 */
11458 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011459 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11460 return;
11461 }
11462
Sean Christopherson4c008122018-08-27 15:21:10 -070011463 if (vcpu->arch.virtual_tsc_khz == 0)
11464 return;
11465
Jan Kiszkaf4124502014-03-07 20:03:13 +010011466 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11467 preemption_timeout *= 1000000;
11468 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11469 hrtimer_start(&vmx->nested.preemption_timer,
11470 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11471}
11472
Jim Mattson56a20512017-07-06 16:33:06 -070011473static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11474 struct vmcs12 *vmcs12)
11475{
11476 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11477 return 0;
11478
11479 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11480 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11481 return -EINVAL;
11482
11483 return 0;
11484}
11485
Wincy Van3af18d92015-02-03 23:49:31 +080011486static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11487 struct vmcs12 *vmcs12)
11488{
Wincy Van3af18d92015-02-03 23:49:31 +080011489 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11490 return 0;
11491
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011492 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011493 return -EINVAL;
11494
11495 return 0;
11496}
11497
Jim Mattson712b12d2017-08-24 13:24:47 -070011498static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11499 struct vmcs12 *vmcs12)
11500{
11501 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11502 return 0;
11503
11504 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11505 return -EINVAL;
11506
11507 return 0;
11508}
11509
Wincy Van3af18d92015-02-03 23:49:31 +080011510/*
11511 * Merge L0's and L1's MSR bitmap, return false to indicate that
11512 * we do not use the hardware.
11513 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011514static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11515 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011516{
Wincy Van82f0dd42015-02-03 23:57:18 +080011517 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011518 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011519 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011520 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011521 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011522 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011523 *
11524 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11525 * ensures that we do not accidentally generate an L02 MSR bitmap
11526 * from the L12 MSR bitmap that is too permissive.
11527 * 2. That L1 or L2s have actually used the MSR. This avoids
11528 * unnecessarily merging of the bitmap if the MSR is unused. This
11529 * works properly because we only update the L01 MSR bitmap lazily.
11530 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11531 * updated to reflect this when L1 (or its L2s) actually write to
11532 * the MSR.
11533 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011534 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11535 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011536
Paolo Bonzinic9923842017-12-13 14:16:30 +010011537 /* Nothing to do if the MSR bitmap is not in use. */
11538 if (!cpu_has_vmx_msr_bitmap() ||
11539 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11540 return false;
11541
Ashok Raj15d45072018-02-01 22:59:43 +010011542 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011543 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011544 return false;
11545
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011546 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11547 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011548 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011549
Radim Krčmářd048c092016-08-08 20:16:22 +020011550 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011551 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11552 /*
11553 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11554 * just lets the processor take the value from the virtual-APIC page;
11555 * take those 256 bits directly from the L1 bitmap.
11556 */
11557 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11558 unsigned word = msr / BITS_PER_LONG;
11559 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11560 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011561 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011562 } else {
11563 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11564 unsigned word = msr / BITS_PER_LONG;
11565 msr_bitmap_l0[word] = ~0;
11566 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11567 }
11568 }
11569
11570 nested_vmx_disable_intercept_for_msr(
11571 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011572 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011573 MSR_TYPE_W);
11574
11575 if (nested_cpu_has_vid(vmcs12)) {
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_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011579 MSR_TYPE_W);
11580 nested_vmx_disable_intercept_for_msr(
11581 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011582 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011583 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011584 }
Ashok Raj15d45072018-02-01 22:59:43 +010011585
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011586 if (spec_ctrl)
11587 nested_vmx_disable_intercept_for_msr(
11588 msr_bitmap_l1, msr_bitmap_l0,
11589 MSR_IA32_SPEC_CTRL,
11590 MSR_TYPE_R | MSR_TYPE_W);
11591
Ashok Raj15d45072018-02-01 22:59:43 +010011592 if (pred_cmd)
11593 nested_vmx_disable_intercept_for_msr(
11594 msr_bitmap_l1, msr_bitmap_l0,
11595 MSR_IA32_PRED_CMD,
11596 MSR_TYPE_W);
11597
Wincy Vanf2b93282015-02-03 23:56:03 +080011598 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011599 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011600
11601 return true;
11602}
11603
Liran Alon61ada742018-06-23 02:35:08 +030011604static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11605 struct vmcs12 *vmcs12)
11606{
11607 struct vmcs12 *shadow;
11608 struct page *page;
11609
11610 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11611 vmcs12->vmcs_link_pointer == -1ull)
11612 return;
11613
11614 shadow = get_shadow_vmcs12(vcpu);
11615 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11616
11617 memcpy(shadow, kmap(page), VMCS12_SIZE);
11618
11619 kunmap(page);
11620 kvm_release_page_clean(page);
11621}
11622
11623static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11624 struct vmcs12 *vmcs12)
11625{
11626 struct vcpu_vmx *vmx = to_vmx(vcpu);
11627
11628 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11629 vmcs12->vmcs_link_pointer == -1ull)
11630 return;
11631
11632 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11633 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11634}
11635
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011636static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11637 struct vmcs12 *vmcs12)
11638{
11639 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11640 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11641 return -EINVAL;
11642 else
11643 return 0;
11644}
11645
Wincy Vanf2b93282015-02-03 23:56:03 +080011646static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11647 struct vmcs12 *vmcs12)
11648{
Wincy Van82f0dd42015-02-03 23:57:18 +080011649 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011650 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011651 !nested_cpu_has_vid(vmcs12) &&
11652 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011653 return 0;
11654
11655 /*
11656 * If virtualize x2apic mode is enabled,
11657 * virtualize apic access must be disabled.
11658 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011659 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11660 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011661 return -EINVAL;
11662
Wincy Van608406e2015-02-03 23:57:51 +080011663 /*
11664 * If virtual interrupt delivery is enabled,
11665 * we must exit on external interrupts.
11666 */
11667 if (nested_cpu_has_vid(vmcs12) &&
11668 !nested_exit_on_intr(vcpu))
11669 return -EINVAL;
11670
Wincy Van705699a2015-02-03 23:58:17 +080011671 /*
11672 * bits 15:8 should be zero in posted_intr_nv,
11673 * the descriptor address has been already checked
11674 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011675 *
11676 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011677 */
11678 if (nested_cpu_has_posted_intr(vmcs12) &&
11679 (!nested_cpu_has_vid(vmcs12) ||
11680 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011681 (vmcs12->posted_intr_nv & 0xff00) ||
11682 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11683 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011684 return -EINVAL;
11685
Wincy Vanf2b93282015-02-03 23:56:03 +080011686 /* tpr shadow is needed by all apicv features. */
11687 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11688 return -EINVAL;
11689
11690 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011691}
11692
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011693static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11694 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011695 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011696{
Liran Alone2536742018-06-23 02:35:02 +030011697 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011698 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011699 u64 count, addr;
11700
Liran Alone2536742018-06-23 02:35:02 +030011701 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11702 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011703 WARN_ON(1);
11704 return -EINVAL;
11705 }
11706 if (count == 0)
11707 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011708 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011709 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11710 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011711 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011712 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11713 addr_field, maxphyaddr, count, addr);
11714 return -EINVAL;
11715 }
11716 return 0;
11717}
11718
11719static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11720 struct vmcs12 *vmcs12)
11721{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011722 if (vmcs12->vm_exit_msr_load_count == 0 &&
11723 vmcs12->vm_exit_msr_store_count == 0 &&
11724 vmcs12->vm_entry_msr_load_count == 0)
11725 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011726 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011727 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011728 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011729 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011730 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011731 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011732 return -EINVAL;
11733 return 0;
11734}
11735
Bandan Dasc5f983f2017-05-05 15:25:14 -040011736static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11737 struct vmcs12 *vmcs12)
11738{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011739 if (!nested_cpu_has_pml(vmcs12))
11740 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011741
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011742 if (!nested_cpu_has_ept(vmcs12) ||
11743 !page_address_valid(vcpu, vmcs12->pml_address))
11744 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011745
11746 return 0;
11747}
11748
Liran Alona8a7c022018-06-23 02:35:06 +030011749static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11750 struct vmcs12 *vmcs12)
11751{
11752 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11753 return 0;
11754
11755 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11756 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11757 return -EINVAL;
11758
11759 return 0;
11760}
11761
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011762static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11763 struct vmx_msr_entry *e)
11764{
11765 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011766 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011767 return -EINVAL;
11768 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11769 e->index == MSR_IA32_UCODE_REV)
11770 return -EINVAL;
11771 if (e->reserved != 0)
11772 return -EINVAL;
11773 return 0;
11774}
11775
11776static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11777 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011778{
11779 if (e->index == MSR_FS_BASE ||
11780 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011781 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11782 nested_vmx_msr_check_common(vcpu, e))
11783 return -EINVAL;
11784 return 0;
11785}
11786
11787static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11788 struct vmx_msr_entry *e)
11789{
11790 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11791 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011792 return -EINVAL;
11793 return 0;
11794}
11795
11796/*
11797 * Load guest's/host's msr at nested entry/exit.
11798 * return 0 for success, entry index for failure.
11799 */
11800static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11801{
11802 u32 i;
11803 struct vmx_msr_entry e;
11804 struct msr_data msr;
11805
11806 msr.host_initiated = false;
11807 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011808 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11809 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011810 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011811 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11812 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011813 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011814 }
11815 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011816 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011817 "%s check failed (%u, 0x%x, 0x%x)\n",
11818 __func__, i, e.index, e.reserved);
11819 goto fail;
11820 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011821 msr.index = e.index;
11822 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011823 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011824 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011825 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11826 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011827 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011828 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011829 }
11830 return 0;
11831fail:
11832 return i + 1;
11833}
11834
11835static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11836{
11837 u32 i;
11838 struct vmx_msr_entry e;
11839
11840 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011841 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011842 if (kvm_vcpu_read_guest(vcpu,
11843 gpa + i * sizeof(e),
11844 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011845 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011846 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11847 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011848 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011849 }
11850 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011851 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011852 "%s check failed (%u, 0x%x, 0x%x)\n",
11853 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011854 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011855 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011856 msr_info.host_initiated = false;
11857 msr_info.index = e.index;
11858 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011859 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011860 "%s cannot read MSR (%u, 0x%x)\n",
11861 __func__, i, e.index);
11862 return -EINVAL;
11863 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011864 if (kvm_vcpu_write_guest(vcpu,
11865 gpa + i * sizeof(e) +
11866 offsetof(struct vmx_msr_entry, value),
11867 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011868 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011869 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011870 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011871 return -EINVAL;
11872 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011873 }
11874 return 0;
11875}
11876
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011877static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11878{
11879 unsigned long invalid_mask;
11880
11881 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11882 return (val & invalid_mask) == 0;
11883}
11884
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011885/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011886 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11887 * emulating VM entry into a guest with EPT enabled.
11888 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11889 * is assigned to entry_failure_code on failure.
11890 */
11891static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011892 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011893{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011894 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011895 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011896 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11897 return 1;
11898 }
11899
11900 /*
11901 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11902 * must not be dereferenced.
11903 */
11904 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11905 !nested_ept) {
11906 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11907 *entry_failure_code = ENTRY_FAIL_PDPTE;
11908 return 1;
11909 }
11910 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011911 }
11912
Junaid Shahid50c28f22018-06-27 14:59:11 -070011913 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011914 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011915
11916 vcpu->arch.cr3 = cr3;
11917 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11918
11919 kvm_init_mmu(vcpu, false);
11920
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011921 return 0;
11922}
11923
Liran Alonefebf0a2018-10-08 23:42:20 +030011924/*
11925 * Returns if KVM is able to config CPU to tag TLB entries
11926 * populated by L2 differently than TLB entries populated
11927 * by L1.
11928 *
11929 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11930 *
11931 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11932 * with different VPID (L1 entries are tagged with vmx->vpid
11933 * while L2 entries are tagged with vmx->nested.vpid02).
11934 */
11935static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11936{
11937 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11938
11939 return nested_cpu_has_ept(vmcs12) ||
11940 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11941}
11942
Sean Christopherson3df5c372018-09-26 09:23:44 -070011943static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
11944{
11945 if (vmx->nested.nested_run_pending &&
11946 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11947 return vmcs12->guest_ia32_efer;
11948 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11949 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
11950 else
11951 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
11952}
11953
Sean Christopherson09abe322018-09-26 09:23:50 -070011954static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011955{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011956 /*
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011957 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070011958 * according to L0's settings (vmcs12 is irrelevant here). Host
11959 * fields that come from L0 and are not constant, e.g. HOST_CR3,
11960 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011961 */
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011962 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070011963 return;
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011964 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011965
Sean Christopherson52017602018-09-26 09:23:57 -070011966 /*
11967 * We don't care what the EPTP value is we just need to guarantee
11968 * it's valid so we don't get a false positive when doing early
11969 * consistency checks.
11970 */
11971 if (enable_ept && nested_early_check)
11972 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
11973
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011974 /* All VMFUNCs are currently emulated through L0 vmexits. */
11975 if (cpu_has_vmx_vmfunc())
11976 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11977
Sean Christopherson09abe322018-09-26 09:23:50 -070011978 if (cpu_has_vmx_posted_intr())
11979 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11980
11981 if (cpu_has_vmx_msr_bitmap())
11982 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
11983
11984 if (enable_pml)
11985 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011986
11987 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070011988 * Set the MSR load/store lists to match L0's settings. Only the
11989 * addresses are constant (for vmcs02), the counts can change based
11990 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011991 */
11992 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011993 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011994 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011995
Sean Christopherson09abe322018-09-26 09:23:50 -070011996 vmx_set_constant_host_state(vmx);
11997}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011998
Sean Christopherson09abe322018-09-26 09:23:50 -070011999static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
12000 struct vmcs12 *vmcs12)
12001{
12002 prepare_vmcs02_constant_state(vmx);
12003
12004 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012005
12006 if (enable_vpid) {
12007 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12008 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12009 else
12010 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12011 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012012}
12013
Sean Christopherson09abe322018-09-26 09:23:50 -070012014static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012015{
Bandan Das03efce62017-05-05 15:25:15 -040012016 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012017 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012018
Sean Christopherson09abe322018-09-26 09:23:50 -070012019 if (vmx->nested.dirty_vmcs12)
12020 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012021
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012022 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012023 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12024 * entry, but only if the current (host) sp changed from the value
12025 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12026 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070012027 * here we just force the write to happen on entry. host_rsp will
12028 * also be written unconditionally by nested_vmx_check_vmentry_hw()
12029 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012030 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012031 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012032
Sean Christopherson09abe322018-09-26 09:23:50 -070012033 /*
12034 * PIN CONTROLS
12035 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012036 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012037
Sean Christophersonf459a702018-08-27 15:21:11 -070012038 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012039 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012040 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12041 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012042
12043 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012044 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012045 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12046 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012047 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012048 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012049 }
Jan Kiszkaf4124502014-03-07 20:03:13 +010012050 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012051
Sean Christopherson09abe322018-09-26 09:23:50 -070012052 /*
12053 * EXEC CONTROLS
12054 */
12055 exec_control = vmx_exec_control(vmx); /* L0's desires */
12056 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12057 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12058 exec_control &= ~CPU_BASED_TPR_SHADOW;
12059 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012060
Sean Christopherson09abe322018-09-26 09:23:50 -070012061 /*
12062 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12063 * nested_get_vmcs12_pages can't fix it up, the illegal value
12064 * will result in a VM entry failure.
12065 */
12066 if (exec_control & CPU_BASED_TPR_SHADOW) {
12067 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12068 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12069 } else {
12070#ifdef CONFIG_X86_64
12071 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12072 CPU_BASED_CR8_STORE_EXITING;
12073#endif
12074 }
12075
12076 /*
12077 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12078 * for I/O port accesses.
12079 */
12080 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12081 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12082 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12083
12084 /*
12085 * SECONDARY EXEC CONTROLS
12086 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012087 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012088 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012089
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012090 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012091 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012092 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012093 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012094 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012095 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012096 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12097 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012098 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012099 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12100 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12101 ~SECONDARY_EXEC_ENABLE_PML;
12102 exec_control |= vmcs12_exec_ctrl;
12103 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012104
Liran Alon32c7acf2018-06-23 02:35:11 +030012105 /* VMCS shadowing for L2 is emulated for now */
12106 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12107
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012108 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012109 vmcs_write16(GUEST_INTR_STATUS,
12110 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012111
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012112 /*
12113 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12114 * nested_get_vmcs12_pages will either fix it up or
12115 * remove the VM execution control.
12116 */
12117 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12118 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12119
Sean Christopherson0b665d32018-08-14 09:33:34 -070012120 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12121 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12122
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012123 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12124 }
12125
Jim Mattson83bafef2016-10-04 10:48:38 -070012126 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012127 * ENTRY CONTROLS
12128 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012129 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012130 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12131 * on the related bits (if supported by the CPU) in the hope that
12132 * we can avoid VMWrites during vmx_set_efer().
Sean Christopherson3df5c372018-09-26 09:23:44 -070012133 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012134 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12135 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12136 if (cpu_has_load_ia32_efer) {
12137 if (guest_efer & EFER_LMA)
12138 exec_control |= VM_ENTRY_IA32E_MODE;
12139 if (guest_efer != host_efer)
12140 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12141 }
12142 vm_entry_controls_init(vmx, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012143
Sean Christopherson09abe322018-09-26 09:23:50 -070012144 /*
12145 * EXIT CONTROLS
12146 *
12147 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12148 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12149 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
12150 */
12151 exec_control = vmcs_config.vmexit_ctrl;
12152 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12153 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12154 vm_exit_controls_init(vmx, exec_control);
12155
12156 /*
12157 * Conceptually we want to copy the PML address and index from
12158 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12159 * since we always flush the log on each vmexit and never change
12160 * the PML address (once set), this happens to be equivalent to
12161 * simply resetting the index in vmcs02.
12162 */
12163 if (enable_pml)
12164 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12165
12166 /*
12167 * Interrupt/Exception Fields
12168 */
12169 if (vmx->nested.nested_run_pending) {
12170 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12171 vmcs12->vm_entry_intr_info_field);
12172 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12173 vmcs12->vm_entry_exception_error_code);
12174 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12175 vmcs12->vm_entry_instruction_len);
12176 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12177 vmcs12->guest_interruptibility_info);
12178 vmx->loaded_vmcs->nmi_known_unmasked =
12179 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12180 } else {
12181 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12182 }
12183}
12184
12185static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12186{
12187 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12188 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12189 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12190 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12191 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12192 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12193 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12194 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12195 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12196 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12197 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12198 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12199 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12200 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12201 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12202 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12203 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12204 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12205 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12206 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12207 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12208 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12209 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12210 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12211 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12212 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12213 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12214 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12215 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12216 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12217 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12218
12219 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12220 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12221 vmcs12->guest_pending_dbg_exceptions);
12222 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12223 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12224
12225 if (nested_cpu_has_xsaves(vmcs12))
12226 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12227
12228 /*
12229 * Whether page-faults are trapped is determined by a combination of
12230 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12231 * If enable_ept, L0 doesn't care about page faults and we should
12232 * set all of these to L1's desires. However, if !enable_ept, L0 does
12233 * care about (at least some) page faults, and because it is not easy
12234 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12235 * to exit on each and every L2 page fault. This is done by setting
12236 * MASK=MATCH=0 and (see below) EB.PF=1.
12237 * Note that below we don't need special code to set EB.PF beyond the
12238 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12239 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12240 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12241 */
12242 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12243 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12244 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12245 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12246
12247 if (cpu_has_vmx_apicv()) {
12248 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12249 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12250 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12251 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12252 }
12253
12254 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12255 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12256
12257 set_cr4_guest_host_mask(vmx);
12258
12259 if (kvm_mpx_supported()) {
12260 if (vmx->nested.nested_run_pending &&
12261 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12262 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12263 else
12264 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12265 }
12266
12267 /*
12268 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12269 */
12270 if (enable_ept) {
12271 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12272 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12273 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12274 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12275 }
12276}
12277
12278/*
12279 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12280 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12281 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12282 * guest in a way that will both be appropriate to L1's requests, and our
12283 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12284 * function also has additional necessary side-effects, like setting various
12285 * vcpu->arch fields.
12286 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12287 * is assigned to entry_failure_code on failure.
12288 */
12289static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12290 u32 *entry_failure_code)
12291{
12292 struct vcpu_vmx *vmx = to_vmx(vcpu);
12293
12294 if (vmx->nested.dirty_vmcs12) {
12295 prepare_vmcs02_full(vmx, vmcs12);
12296 vmx->nested.dirty_vmcs12 = false;
12297 }
12298
12299 /*
12300 * First, the fields that are shadowed. This must be kept in sync
12301 * with vmx_shadow_fields.h.
12302 */
12303
12304 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12305 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12306 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12307 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12308 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12309
12310 if (vmx->nested.nested_run_pending &&
12311 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12312 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12313 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12314 } else {
12315 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12316 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12317 }
12318 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12319
12320 vmx->nested.preemption_timer_expired = false;
12321 if (nested_cpu_has_preemption_timer(vmcs12))
12322 vmx_start_preemption_timer(vcpu);
12323
12324 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12325 * bitwise-or of what L1 wants to trap for L2, and what we want to
12326 * trap. Note that CR0.TS also needs updating - we do this later.
12327 */
12328 update_exception_bitmap(vcpu);
12329 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12330 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12331
Jim Mattson6514dc32018-04-26 16:09:12 -070012332 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012333 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012334 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012335 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012336 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012337 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012338 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012339
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012340 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12341
Peter Feinerc95ba922016-08-17 09:36:47 -070012342 if (kvm_has_tsc_control)
12343 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012344
12345 if (enable_vpid) {
12346 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012347 * There is no direct mapping between vpid02 and vpid12, the
12348 * vpid02 is per-vCPU for L0 and reused while the value of
12349 * vpid12 is changed w/ one invvpid during nested vmentry.
12350 * The vpid12 is allocated by L1 for L2, so it will not
12351 * influence global bitmap(for vpid01 and vpid02 allocation)
12352 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012353 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012354 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012355 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12356 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012357 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012358 }
12359 } else {
Liran Alon14389212018-10-08 23:42:17 +030012360 /*
12361 * If L1 use EPT, then L0 needs to execute INVEPT on
12362 * EPTP02 instead of EPTP01. Therefore, delay TLB
12363 * flush until vmcs02->eptp is fully updated by
12364 * KVM_REQ_LOAD_CR3. Note that this assumes
12365 * KVM_REQ_TLB_FLUSH is evaluated after
12366 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12367 */
12368 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012369 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012370 }
12371
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012372 if (nested_cpu_has_ept(vmcs12))
12373 nested_ept_init_mmu_context(vcpu);
12374 else if (nested_cpu_has2(vmcs12,
12375 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012376 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012377
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012378 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012379 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12380 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012381 * The CR0_READ_SHADOW is what L2 should have expected to read given
12382 * the specifications by L1; It's not enough to take
12383 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12384 * have more bits than L1 expected.
12385 */
12386 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12387 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12388
12389 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12390 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12391
Sean Christopherson09abe322018-09-26 09:23:50 -070012392 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012393 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012394 vmx_set_efer(vcpu, vcpu->arch.efer);
12395
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012396 /*
12397 * Guest state is invalid and unrestricted guest is disabled,
12398 * which means L1 attempted VMEntry to L2 with invalid state.
12399 * Fail the VMEntry.
12400 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012401 if (vmx->emulation_required) {
12402 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012403 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012404 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012405
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012406 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012407 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012408 entry_failure_code))
12409 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012410
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012411 if (!enable_ept)
12412 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12413
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012414 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12415 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012416 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012417}
12418
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012419static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12420{
12421 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12422 nested_cpu_has_virtual_nmis(vmcs12))
12423 return -EINVAL;
12424
12425 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12426 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12427 return -EINVAL;
12428
12429 return 0;
12430}
12431
Jim Mattsonca0bde22016-11-30 12:03:46 -080012432static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12433{
12434 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012435 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012436
12437 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12438 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12439 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12440
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012441 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12442 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12443
Jim Mattson56a20512017-07-06 16:33:06 -070012444 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12445 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12446
Jim Mattsonca0bde22016-11-30 12:03:46 -080012447 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12448 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12449
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012450 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12451 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12452
Jim Mattson712b12d2017-08-24 13:24:47 -070012453 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12454 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12455
Jim Mattsonca0bde22016-11-30 12:03:46 -080012456 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12457 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12458
12459 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12460 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12461
Bandan Dasc5f983f2017-05-05 15:25:14 -040012462 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12463 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12464
Liran Alona8a7c022018-06-23 02:35:06 +030012465 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12466 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12467
Jim Mattsonca0bde22016-11-30 12:03:46 -080012468 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012469 vmx->nested.msrs.procbased_ctls_low,
12470 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012471 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12472 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012473 vmx->nested.msrs.secondary_ctls_low,
12474 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012475 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012476 vmx->nested.msrs.pinbased_ctls_low,
12477 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012478 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012479 vmx->nested.msrs.exit_ctls_low,
12480 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012481 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012482 vmx->nested.msrs.entry_ctls_low,
12483 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012484 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12485
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012486 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012487 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12488
Bandan Das41ab9372017-08-03 15:54:43 -040012489 if (nested_cpu_has_vmfunc(vmcs12)) {
12490 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012491 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012492 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12493
12494 if (nested_cpu_has_eptp_switching(vmcs12)) {
12495 if (!nested_cpu_has_ept(vmcs12) ||
12496 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12497 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12498 }
12499 }
Bandan Das27c42a12017-08-03 15:54:42 -040012500
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012501 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12502 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12503
Jim Mattsonca0bde22016-11-30 12:03:46 -080012504 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12505 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12506 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12507 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12508
Marc Orr04473782018-06-20 17:21:29 -070012509 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012510 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12511 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12512 * the values of the LMA and LME bits in the field must each be that of
12513 * the host address-space size VM-exit control.
12514 */
12515 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12516 ia32e = (vmcs12->vm_exit_controls &
12517 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12518 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12519 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12520 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12521 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12522 }
12523
12524 /*
Marc Orr04473782018-06-20 17:21:29 -070012525 * From the Intel SDM, volume 3:
12526 * Fields relevant to VM-entry event injection must be set properly.
12527 * These fields are the VM-entry interruption-information field, the
12528 * VM-entry exception error code, and the VM-entry instruction length.
12529 */
12530 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12531 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12532 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12533 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12534 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12535 bool should_have_error_code;
12536 bool urg = nested_cpu_has2(vmcs12,
12537 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12538 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12539
12540 /* VM-entry interruption-info field: interruption type */
12541 if (intr_type == INTR_TYPE_RESERVED ||
12542 (intr_type == INTR_TYPE_OTHER_EVENT &&
12543 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12544 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12545
12546 /* VM-entry interruption-info field: vector */
12547 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12548 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12549 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12550 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12551
12552 /* VM-entry interruption-info field: deliver error code */
12553 should_have_error_code =
12554 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12555 x86_exception_has_error_code(vector);
12556 if (has_error_code != should_have_error_code)
12557 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12558
12559 /* VM-entry exception error code */
12560 if (has_error_code &&
12561 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12562 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12563
12564 /* VM-entry interruption-info field: reserved bits */
12565 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12566 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12567
12568 /* VM-entry instruction length */
12569 switch (intr_type) {
12570 case INTR_TYPE_SOFT_EXCEPTION:
12571 case INTR_TYPE_SOFT_INTR:
12572 case INTR_TYPE_PRIV_SW_EXCEPTION:
12573 if ((vmcs12->vm_entry_instruction_len > 15) ||
12574 (vmcs12->vm_entry_instruction_len == 0 &&
12575 !nested_cpu_has_zero_length_injection(vcpu)))
12576 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12577 }
12578 }
12579
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012580 if (nested_cpu_has_ept(vmcs12) &&
12581 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12582 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12583
Jim Mattsonca0bde22016-11-30 12:03:46 -080012584 return 0;
12585}
12586
Liran Alonf145d902018-06-23 02:35:07 +030012587static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12588 struct vmcs12 *vmcs12)
12589{
12590 int r;
12591 struct page *page;
12592 struct vmcs12 *shadow;
12593
12594 if (vmcs12->vmcs_link_pointer == -1ull)
12595 return 0;
12596
12597 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12598 return -EINVAL;
12599
12600 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12601 if (is_error_page(page))
12602 return -EINVAL;
12603
12604 r = 0;
12605 shadow = kmap(page);
12606 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12607 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12608 r = -EINVAL;
12609 kunmap(page);
12610 kvm_release_page_clean(page);
12611 return r;
12612}
12613
Jim Mattsonca0bde22016-11-30 12:03:46 -080012614static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12615 u32 *exit_qual)
12616{
12617 bool ia32e;
12618
12619 *exit_qual = ENTRY_FAIL_DEFAULT;
12620
12621 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12622 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12623 return 1;
12624
Liran Alonf145d902018-06-23 02:35:07 +030012625 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012626 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12627 return 1;
12628 }
12629
12630 /*
12631 * If the load IA32_EFER VM-entry control is 1, the following checks
12632 * are performed on the field for the IA32_EFER MSR:
12633 * - Bits reserved in the IA32_EFER MSR must be 0.
12634 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12635 * the IA-32e mode guest VM-exit control. It must also be identical
12636 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12637 * CR0.PG) is 1.
12638 */
12639 if (to_vmx(vcpu)->nested.nested_run_pending &&
12640 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12641 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12642 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12643 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12644 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12645 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12646 return 1;
12647 }
12648
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012649 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12650 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12651 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12652 return 1;
12653
Jim Mattsonca0bde22016-11-30 12:03:46 -080012654 return 0;
12655}
12656
Sean Christopherson52017602018-09-26 09:23:57 -070012657static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
12658{
12659 struct vcpu_vmx *vmx = to_vmx(vcpu);
12660 unsigned long cr3, cr4;
12661
12662 if (!nested_early_check)
12663 return 0;
12664
12665 if (vmx->msr_autoload.host.nr)
12666 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
12667 if (vmx->msr_autoload.guest.nr)
12668 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
12669
12670 preempt_disable();
12671
12672 vmx_prepare_switch_to_guest(vcpu);
12673
12674 /*
12675 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
12676 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
12677 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
12678 * there is no need to preserve other bits or save/restore the field.
12679 */
12680 vmcs_writel(GUEST_RFLAGS, 0);
12681
12682 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
12683
12684 cr3 = __get_current_cr3_fast();
12685 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
12686 vmcs_writel(HOST_CR3, cr3);
12687 vmx->loaded_vmcs->host_state.cr3 = cr3;
12688 }
12689
12690 cr4 = cr4_read_shadow();
12691 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
12692 vmcs_writel(HOST_CR4, cr4);
12693 vmx->loaded_vmcs->host_state.cr4 = cr4;
12694 }
12695
12696 vmx->__launched = vmx->loaded_vmcs->launched;
12697
12698 asm(
12699 /* Set HOST_RSP */
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012700 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012701 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
12702
12703 /* Check if vmlaunch of vmresume is needed */
12704 "cmpl $0, %c[launched](%0)\n\t"
12705 "je 1f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012706 __ex("vmresume") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012707 "jmp 2f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012708 "1: " __ex("vmlaunch") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012709 "jmp 2f\n\t"
12710 "2: "
12711
12712 /* Set vmx->fail accordingly */
12713 "setbe %c[fail](%0)\n\t"
12714
12715 ".pushsection .rodata\n\t"
12716 ".global vmx_early_consistency_check_return\n\t"
12717 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
12718 ".popsection"
12719 :
12720 : "c"(vmx), "d"((unsigned long)HOST_RSP),
12721 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
12722 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
12723 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
12724 : "rax", "cc", "memory"
12725 );
12726
12727 vmcs_writel(HOST_RIP, vmx_return);
12728
12729 preempt_enable();
12730
12731 if (vmx->msr_autoload.host.nr)
12732 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12733 if (vmx->msr_autoload.guest.nr)
12734 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12735
12736 if (vmx->fail) {
12737 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
12738 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12739 vmx->fail = 0;
12740 return 1;
12741 }
12742
12743 /*
12744 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
12745 */
12746 local_irq_enable();
12747 if (hw_breakpoint_active())
12748 set_debugreg(__this_cpu_read(cpu_dr7), 7);
12749
12750 /*
12751 * A non-failing VMEntry means we somehow entered guest mode with
12752 * an illegal RIP, and that's just the tip of the iceberg. There
12753 * is no telling what memory has been modified or what state has
12754 * been exposed to unknown code. Hitting this all but guarantees
12755 * a (very critical) hardware issue.
12756 */
12757 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
12758 VMX_EXIT_REASONS_FAILED_VMENTRY));
12759
12760 return 0;
12761}
12762STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
12763
Sean Christophersona633e412018-09-26 09:23:47 -070012764static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12765 struct vmcs12 *vmcs12);
12766
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012767/*
Sean Christophersona633e412018-09-26 09:23:47 -070012768 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012769 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070012770+ *
12771+ * Returns:
12772+ * 0 - success, i.e. proceed with actual VMEnter
12773+ * 1 - consistency check VMExit
12774+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012775 */
Sean Christophersona633e412018-09-26 09:23:47 -070012776static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
12777 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080012778{
12779 struct vcpu_vmx *vmx = to_vmx(vcpu);
12780 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020012781 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070012782 u32 exit_reason = EXIT_REASON_INVALID_STATE;
12783 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080012784
Paolo Bonzini7e712682018-10-03 13:44:26 +020012785 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12786 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12787 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12788 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012789
Jim Mattson858e25c2016-11-30 12:03:47 -080012790 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12791 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012792 if (kvm_mpx_supported() &&
12793 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12794 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012795
Jim Mattsonde3a0022017-11-27 17:22:25 -060012796 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012797
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012798 prepare_vmcs02_early(vmx, vmcs12);
12799
12800 if (from_vmentry) {
12801 nested_get_vmcs12_pages(vcpu);
12802
Sean Christopherson52017602018-09-26 09:23:57 -070012803 if (nested_vmx_check_vmentry_hw(vcpu)) {
12804 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12805 return -1;
12806 }
12807
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012808 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
12809 goto vmentry_fail_vmexit;
12810 }
12811
12812 enter_guest_mode(vcpu);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012813 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12814 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12815
Sean Christophersona633e412018-09-26 09:23:47 -070012816 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070012817 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080012818
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012819 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070012820 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
12821 exit_qual = nested_vmx_load_msr(vcpu,
12822 vmcs12->vm_entry_msr_load_addr,
12823 vmcs12->vm_entry_msr_load_count);
12824 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070012825 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012826 } else {
12827 /*
12828 * The MMU is not initialized to point at the right entities yet and
12829 * "get pages" would need to read data from the guest (i.e. we will
12830 * need to perform gpa to hpa translation). Request a call
12831 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12832 * have already been set at vmentry time and should not be reset.
12833 */
12834 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12835 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012836
Jim Mattson858e25c2016-11-30 12:03:47 -080012837 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012838 * If L1 had a pending IRQ/NMI until it executed
12839 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12840 * disallowed (e.g. interrupts disabled), L0 needs to
12841 * evaluate if this pending event should cause an exit from L2
12842 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12843 * intercept EXTERNAL_INTERRUPT).
12844 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012845 * Usually this would be handled by the processor noticing an
12846 * IRQ/NMI window request, or checking RVI during evaluation of
12847 * pending virtual interrupts. However, this setting was done
12848 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12849 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012850 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012851 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012852 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012853
12854 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012855 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12856 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12857 * returned as far as L1 is concerned. It will only return (and set
12858 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12859 */
12860 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012861
Sean Christophersona633e412018-09-26 09:23:47 -070012862 /*
12863 * A failed consistency check that leads to a VMExit during L1's
12864 * VMEnter to L2 is a variation of a normal VMexit, as explained in
12865 * 26.7 "VM-entry failures during or after loading guest state".
12866 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070012867vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012868 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12869 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12870 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012871
12872vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012873 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070012874
12875 if (!from_vmentry)
12876 return 1;
12877
Sean Christophersona633e412018-09-26 09:23:47 -070012878 load_vmcs12_host_state(vcpu, vmcs12);
12879 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12880 vmcs12->exit_qualification = exit_qual;
Sean Christophersona633e412018-09-26 09:23:47 -070012881 if (enable_shadow_vmcs)
12882 vmx->nested.sync_shadow_vmcs = true;
12883 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012884}
12885
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012886/*
12887 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12888 * for running an L2 nested guest.
12889 */
12890static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12891{
12892 struct vmcs12 *vmcs12;
12893 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012894 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012895 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012896
Kyle Hueyeb277562016-11-29 12:40:39 -080012897 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012898 return 1;
12899
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012900 if (vmx->nested.current_vmptr == -1ull)
12901 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080012902
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012903 vmcs12 = get_vmcs12(vcpu);
12904
Liran Alona6192d42018-06-23 02:35:04 +030012905 /*
12906 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12907 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12908 * rather than RFLAGS.ZF, and no error number is stored to the
12909 * VM-instruction error field.
12910 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012911 if (vmcs12->hdr.shadow_vmcs)
12912 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030012913
Abel Gordon012f83c2013-04-18 14:39:25 +030012914 if (enable_shadow_vmcs)
12915 copy_shadow_to_vmcs12(vmx);
12916
Nadav Har'El7c177932011-05-25 23:12:04 +030012917 /*
12918 * The nested entry process starts with enforcing various prerequisites
12919 * on vmcs12 as required by the Intel SDM, and act appropriately when
12920 * they fail: As the SDM explains, some conditions should cause the
12921 * instruction to fail, while others will cause the instruction to seem
12922 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12923 * To speed up the normal (success) code path, we should avoid checking
12924 * for misconfigurations which will anyway be caught by the processor
12925 * when using the merged vmcs02.
12926 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012927 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
12928 return nested_vmx_failValid(vcpu,
12929 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012930
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012931 if (vmcs12->launch_state == launch)
12932 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030012933 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12934 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030012935
Jim Mattsonca0bde22016-11-30 12:03:46 -080012936 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012937 if (ret)
12938 return nested_vmx_failValid(vcpu, ret);
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012939
Nadav Har'El7c177932011-05-25 23:12:04 +030012940 /*
12941 * We're finally done with prerequisite checking, and can start with
12942 * the nested entry.
12943 */
Jim Mattson6514dc32018-04-26 16:09:12 -070012944 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070012945 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070012946 vmx->nested.nested_run_pending = !ret;
12947 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012948 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070012949 else if (ret)
12950 return nested_vmx_failValid(vcpu,
12951 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030012952
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012953 /* Hide L1D cache contents from the nested guest. */
12954 vmx->vcpu.arch.l1tf_flush_l1d = true;
12955
Chao Gao135a06c2018-02-11 10:06:30 +080012956 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070012957 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030012958 * also be used as part of restoring nVMX state for
12959 * snapshot restore (migration).
12960 *
12961 * In this flow, it is assumed that vmcs12 cache was
12962 * trasferred as part of captured nVMX state and should
12963 * therefore not be read from guest memory (which may not
12964 * exist on destination host yet).
12965 */
12966 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12967
12968 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012969 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12970 * by event injection, halt vcpu.
12971 */
12972 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012973 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12974 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012975 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012976 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012977 return 1;
12978}
12979
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012980/*
12981 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12982 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12983 * This function returns the new value we should put in vmcs12.guest_cr0.
12984 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12985 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12986 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12987 * didn't trap the bit, because if L1 did, so would L0).
12988 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12989 * been modified by L2, and L1 knows it. So just leave the old value of
12990 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12991 * isn't relevant, because if L0 traps this bit it can set it to anything.
12992 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12993 * changed these bits, and therefore they need to be updated, but L0
12994 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12995 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12996 */
12997static inline unsigned long
12998vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12999{
13000 return
13001 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
13002 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
13003 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
13004 vcpu->arch.cr0_guest_owned_bits));
13005}
13006
13007static inline unsigned long
13008vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13009{
13010 return
13011 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
13012 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
13013 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13014 vcpu->arch.cr4_guest_owned_bits));
13015}
13016
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013017static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13018 struct vmcs12 *vmcs12)
13019{
13020 u32 idt_vectoring;
13021 unsigned int nr;
13022
Wanpeng Li664f8e22017-08-24 03:35:09 -070013023 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013024 nr = vcpu->arch.exception.nr;
13025 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13026
13027 if (kvm_exception_is_soft(nr)) {
13028 vmcs12->vm_exit_instruction_len =
13029 vcpu->arch.event_exit_inst_len;
13030 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13031 } else
13032 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13033
13034 if (vcpu->arch.exception.has_error_code) {
13035 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13036 vmcs12->idt_vectoring_error_code =
13037 vcpu->arch.exception.error_code;
13038 }
13039
13040 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010013041 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013042 vmcs12->idt_vectoring_info_field =
13043 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030013044 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013045 nr = vcpu->arch.interrupt.nr;
13046 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13047
13048 if (vcpu->arch.interrupt.soft) {
13049 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13050 vmcs12->vm_entry_instruction_len =
13051 vcpu->arch.event_exit_inst_len;
13052 } else
13053 idt_vectoring |= INTR_TYPE_EXT_INTR;
13054
13055 vmcs12->idt_vectoring_info_field = idt_vectoring;
13056 }
13057}
13058
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013059static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
13060{
13061 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013062 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020013063 bool block_nested_events =
13064 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080013065
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013066 if (vcpu->arch.exception.pending &&
13067 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020013068 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013069 return -EBUSY;
13070 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013071 return 0;
13072 }
13073
Jan Kiszkaf4124502014-03-07 20:03:13 +010013074 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13075 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013076 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010013077 return -EBUSY;
13078 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13079 return 0;
13080 }
13081
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013082 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013083 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013084 return -EBUSY;
13085 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13086 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13087 INTR_INFO_VALID_MASK, 0);
13088 /*
13089 * The NMI-triggered VM exit counts as injection:
13090 * clear this one and block further NMIs.
13091 */
13092 vcpu->arch.nmi_pending = 0;
13093 vmx_set_nmi_mask(vcpu, true);
13094 return 0;
13095 }
13096
13097 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13098 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013099 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013100 return -EBUSY;
13101 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013102 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013103 }
13104
David Hildenbrand6342c502017-01-25 11:58:58 +010013105 vmx_complete_nested_posted_interrupt(vcpu);
13106 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013107}
13108
Sean Christophersond264ee02018-08-27 15:21:12 -070013109static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13110{
13111 to_vmx(vcpu)->req_immediate_exit = true;
13112}
13113
Jan Kiszkaf4124502014-03-07 20:03:13 +010013114static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13115{
13116 ktime_t remaining =
13117 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13118 u64 value;
13119
13120 if (ktime_to_ns(remaining) <= 0)
13121 return 0;
13122
13123 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13124 do_div(value, 1000000);
13125 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13126}
13127
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013128/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013129 * Update the guest state fields of vmcs12 to reflect changes that
13130 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13131 * VM-entry controls is also updated, since this is really a guest
13132 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013133 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013134static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013135{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013136 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13137 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13138
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013139 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13140 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13141 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13142
13143 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13144 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13145 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13146 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13147 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13148 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13149 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13150 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13151 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13152 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13153 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13154 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13155 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13156 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13157 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13158 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13159 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13160 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13161 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13162 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13163 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13164 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13165 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13166 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13167 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13168 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13169 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13170 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13171 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13172 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13173 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13174 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13175 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13176 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13177 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13178 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13179
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013180 vmcs12->guest_interruptibility_info =
13181 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13182 vmcs12->guest_pending_dbg_exceptions =
13183 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013184 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13185 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13186 else
13187 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013188
Jan Kiszkaf4124502014-03-07 20:03:13 +010013189 if (nested_cpu_has_preemption_timer(vmcs12)) {
13190 if (vmcs12->vm_exit_controls &
13191 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13192 vmcs12->vmx_preemption_timer_value =
13193 vmx_get_preemption_timer_value(vcpu);
13194 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13195 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013196
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013197 /*
13198 * In some cases (usually, nested EPT), L2 is allowed to change its
13199 * own CR3 without exiting. If it has changed it, we must keep it.
13200 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13201 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13202 *
13203 * Additionally, restore L2's PDPTR to vmcs12.
13204 */
13205 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013206 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013207 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13208 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13209 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13210 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13211 }
13212
Jim Mattsond281e132017-06-01 12:44:46 -070013213 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013214
Wincy Van608406e2015-02-03 23:57:51 +080013215 if (nested_cpu_has_vid(vmcs12))
13216 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13217
Jan Kiszkac18911a2013-03-13 16:06:41 +010013218 vmcs12->vm_entry_controls =
13219 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013220 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013221
Jan Kiszka2996fca2014-06-16 13:59:43 +020013222 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13223 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13224 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13225 }
13226
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013227 /* TODO: These cannot have changed unless we have MSR bitmaps and
13228 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013229 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013230 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013231 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13232 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013233 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13234 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13235 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013236 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013237 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013238}
13239
13240/*
13241 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13242 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13243 * and this function updates it to reflect the changes to the guest state while
13244 * L2 was running (and perhaps made some exits which were handled directly by L0
13245 * without going back to L1), and to reflect the exit reason.
13246 * Note that we do not have to copy here all VMCS fields, just those that
13247 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13248 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13249 * which already writes to vmcs12 directly.
13250 */
13251static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13252 u32 exit_reason, u32 exit_intr_info,
13253 unsigned long exit_qualification)
13254{
13255 /* update guest state fields: */
13256 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013257
13258 /* update exit information fields: */
13259
Jan Kiszka533558b2014-01-04 18:47:20 +010013260 vmcs12->vm_exit_reason = exit_reason;
13261 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013262 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013263
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013264 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013265 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13266 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13267
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013268 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013269 vmcs12->launch_state = 1;
13270
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013271 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13272 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013273 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013274
13275 /*
13276 * Transfer the event that L0 or L1 may wanted to inject into
13277 * L2 to IDT_VECTORING_INFO_FIELD.
13278 */
13279 vmcs12_save_pending_event(vcpu, vmcs12);
13280 }
13281
13282 /*
13283 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13284 * preserved above and would only end up incorrectly in L1.
13285 */
13286 vcpu->arch.nmi_injected = false;
13287 kvm_clear_exception_queue(vcpu);
13288 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013289}
13290
13291/*
13292 * A part of what we need to when the nested L2 guest exits and we want to
13293 * run its L1 parent, is to reset L1's guest state to the host state specified
13294 * in vmcs12.
13295 * This function is to be called not only on normal nested exit, but also on
13296 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13297 * Failures During or After Loading Guest State").
13298 * This function should be called when the active VMCS is L1's (vmcs01).
13299 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013300static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13301 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013302{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013303 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013304 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013305
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013306 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13307 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013308 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013309 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13310 else
13311 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13312 vmx_set_efer(vcpu, vcpu->arch.efer);
13313
13314 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13315 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013316 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013317 vmx_set_interrupt_shadow(vcpu, 0);
13318
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013319 /*
13320 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013321 * actually changed, because vmx_set_cr0 refers to efer set above.
13322 *
13323 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13324 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013325 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013326 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013327 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013328
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013329 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013330 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013331 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013332
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013333 nested_ept_uninit_mmu_context(vcpu);
13334
13335 /*
13336 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13337 * couldn't have changed.
13338 */
13339 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13340 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13341
13342 if (!enable_ept)
13343 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013344
Liran Alon6f1e03b2018-05-22 17:16:14 +030013345 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013346 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013347 * VMEntry/VMExit. Thus, no need to flush TLB.
13348 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013349 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13350 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013351 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013352 * Otherwise, we can preserve TLB entries as long as we are
13353 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013354 *
13355 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13356 * and therefore we request the TLB flush to happen only after VMCS EPTP
13357 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013358 */
13359 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013360 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013361 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013362 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013363
13364 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13365 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13366 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13367 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13368 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013369 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13370 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013371
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013372 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13373 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13374 vmcs_write64(GUEST_BNDCFGS, 0);
13375
Jan Kiszka44811c02013-08-04 17:17:27 +020013376 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013377 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013378 vcpu->arch.pat = vmcs12->host_ia32_pat;
13379 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013380 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13381 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13382 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013383
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013384 /* Set L1 segment info according to Intel SDM
13385 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13386 seg = (struct kvm_segment) {
13387 .base = 0,
13388 .limit = 0xFFFFFFFF,
13389 .selector = vmcs12->host_cs_selector,
13390 .type = 11,
13391 .present = 1,
13392 .s = 1,
13393 .g = 1
13394 };
13395 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13396 seg.l = 1;
13397 else
13398 seg.db = 1;
13399 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13400 seg = (struct kvm_segment) {
13401 .base = 0,
13402 .limit = 0xFFFFFFFF,
13403 .type = 3,
13404 .present = 1,
13405 .s = 1,
13406 .db = 1,
13407 .g = 1
13408 };
13409 seg.selector = vmcs12->host_ds_selector;
13410 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13411 seg.selector = vmcs12->host_es_selector;
13412 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13413 seg.selector = vmcs12->host_ss_selector;
13414 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13415 seg.selector = vmcs12->host_fs_selector;
13416 seg.base = vmcs12->host_fs_base;
13417 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13418 seg.selector = vmcs12->host_gs_selector;
13419 seg.base = vmcs12->host_gs_base;
13420 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13421 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013422 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013423 .limit = 0x67,
13424 .selector = vmcs12->host_tr_selector,
13425 .type = 11,
13426 .present = 1
13427 };
13428 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13429
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013430 kvm_set_dr(vcpu, 7, 0x400);
13431 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013432
Wincy Van3af18d92015-02-03 23:49:31 +080013433 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013434 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013435
Wincy Vanff651cb2014-12-11 08:52:58 +030013436 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13437 vmcs12->vm_exit_msr_load_count))
13438 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013439}
13440
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013441static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13442{
13443 struct shared_msr_entry *efer_msr;
13444 unsigned int i;
13445
13446 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13447 return vmcs_read64(GUEST_IA32_EFER);
13448
13449 if (cpu_has_load_ia32_efer)
13450 return host_efer;
13451
13452 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13453 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13454 return vmx->msr_autoload.guest.val[i].value;
13455 }
13456
13457 efer_msr = find_msr_entry(vmx, MSR_EFER);
13458 if (efer_msr)
13459 return efer_msr->data;
13460
13461 return host_efer;
13462}
13463
13464static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13465{
13466 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13467 struct vcpu_vmx *vmx = to_vmx(vcpu);
13468 struct vmx_msr_entry g, h;
13469 struct msr_data msr;
13470 gpa_t gpa;
13471 u32 i, j;
13472
13473 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13474
13475 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13476 /*
13477 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13478 * as vmcs01.GUEST_DR7 contains a userspace defined value
13479 * and vcpu->arch.dr7 is not squirreled away before the
13480 * nested VMENTER (not worth adding a variable in nested_vmx).
13481 */
13482 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13483 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13484 else
13485 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13486 }
13487
13488 /*
13489 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13490 * handle a variety of side effects to KVM's software model.
13491 */
13492 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13493
13494 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13495 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13496
13497 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13498 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13499
13500 nested_ept_uninit_mmu_context(vcpu);
13501 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13502 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13503
13504 /*
13505 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13506 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13507 * VMFail, like everything else we just need to ensure our
13508 * software model is up-to-date.
13509 */
13510 ept_save_pdptrs(vcpu);
13511
13512 kvm_mmu_reset_context(vcpu);
13513
13514 if (cpu_has_vmx_msr_bitmap())
13515 vmx_update_msr_bitmap(vcpu);
13516
13517 /*
13518 * This nasty bit of open coding is a compromise between blindly
13519 * loading L1's MSRs using the exit load lists (incorrect emulation
13520 * of VMFail), leaving the nested VM's MSRs in the software model
13521 * (incorrect behavior) and snapshotting the modified MSRs (too
13522 * expensive since the lists are unbound by hardware). For each
13523 * MSR that was (prematurely) loaded from the nested VMEntry load
13524 * list, reload it from the exit load list if it exists and differs
13525 * from the guest value. The intent is to stuff host state as
13526 * silently as possible, not to fully process the exit load list.
13527 */
13528 msr.host_initiated = false;
13529 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13530 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13531 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13532 pr_debug_ratelimited(
13533 "%s read MSR index failed (%u, 0x%08llx)\n",
13534 __func__, i, gpa);
13535 goto vmabort;
13536 }
13537
13538 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13539 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13540 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13541 pr_debug_ratelimited(
13542 "%s read MSR failed (%u, 0x%08llx)\n",
13543 __func__, j, gpa);
13544 goto vmabort;
13545 }
13546 if (h.index != g.index)
13547 continue;
13548 if (h.value == g.value)
13549 break;
13550
13551 if (nested_vmx_load_msr_check(vcpu, &h)) {
13552 pr_debug_ratelimited(
13553 "%s check failed (%u, 0x%x, 0x%x)\n",
13554 __func__, j, h.index, h.reserved);
13555 goto vmabort;
13556 }
13557
13558 msr.index = h.index;
13559 msr.data = h.value;
13560 if (kvm_set_msr(vcpu, &msr)) {
13561 pr_debug_ratelimited(
13562 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13563 __func__, j, h.index, h.value);
13564 goto vmabort;
13565 }
13566 }
13567 }
13568
13569 return;
13570
13571vmabort:
13572 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13573}
13574
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013575/*
13576 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13577 * and modify vmcs12 to make it see what it would expect to see there if
13578 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13579 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013580static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13581 u32 exit_intr_info,
13582 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013583{
13584 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013585 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13586
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013587 /* trying to cancel vmlaunch/vmresume is a bug */
13588 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13589
Jim Mattson4f350c62017-09-14 16:31:44 -070013590 leave_guest_mode(vcpu);
13591
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013592 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13593 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13594
Jim Mattson4f350c62017-09-14 16:31:44 -070013595 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013596 if (exit_reason == -1)
13597 sync_vmcs12(vcpu, vmcs12);
13598 else
13599 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13600 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013601
Liran Alon61ada742018-06-23 02:35:08 +030013602 /*
13603 * Must happen outside of sync_vmcs12() as it will
13604 * also be used to capture vmcs12 cache as part of
13605 * capturing nVMX state for snapshot (migration).
13606 *
13607 * Otherwise, this flush will dirty guest memory at a
13608 * point it is already assumed by user-space to be
13609 * immutable.
13610 */
13611 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13612
Jim Mattson4f350c62017-09-14 16:31:44 -070013613 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13614 vmcs12->vm_exit_msr_store_count))
13615 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070013616 } else {
13617 /*
13618 * The only expected VM-instruction error is "VM entry with
13619 * invalid control field(s)." Anything else indicates a
13620 * problem with L0. And we should never get here with a
13621 * VMFail of any type if early consistency checks are enabled.
13622 */
13623 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
13624 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13625 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013626 }
13627
Jim Mattson4f350c62017-09-14 16:31:44 -070013628 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013629
Paolo Bonzini93140062016-07-06 13:23:51 +020013630 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013631 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13632 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013633 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013634
Peter Feinerc95ba922016-08-17 09:36:47 -070013635 if (kvm_has_tsc_control)
13636 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013637
Jim Mattson8d860bb2018-05-09 16:56:05 -040013638 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13639 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13640 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013641 } else if (!nested_cpu_has_ept(vmcs12) &&
13642 nested_cpu_has2(vmcs12,
13643 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013644 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013645 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013646
13647 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13648 vmx->host_rsp = 0;
13649
13650 /* Unpin physical memory we referred to in vmcs02 */
13651 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013652 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013653 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013654 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013655 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013656 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013657 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013658 }
Wincy Van705699a2015-02-03 23:58:17 +080013659 if (vmx->nested.pi_desc_page) {
13660 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013661 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013662 vmx->nested.pi_desc_page = NULL;
13663 vmx->nested.pi_desc = NULL;
13664 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013665
13666 /*
Tang Chen38b99172014-09-24 15:57:54 +080013667 * We are now running in L2, mmu_notifier will force to reload the
13668 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13669 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013670 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013671
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013672 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013673 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013674
13675 /* in case we halted in L2 */
13676 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013677
13678 if (likely(!vmx->fail)) {
13679 /*
13680 * TODO: SDM says that with acknowledge interrupt on
13681 * exit, bit 31 of the VM-exit interrupt information
13682 * (valid interrupt) is always set to 1 on
13683 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13684 * need kvm_cpu_has_interrupt(). See the commit
13685 * message for details.
13686 */
13687 if (nested_exit_intr_ack_set(vcpu) &&
13688 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13689 kvm_cpu_has_interrupt(vcpu)) {
13690 int irq = kvm_cpu_get_interrupt(vcpu);
13691 WARN_ON(irq < 0);
13692 vmcs12->vm_exit_intr_info = irq |
13693 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13694 }
13695
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013696 if (exit_reason != -1)
13697 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13698 vmcs12->exit_qualification,
13699 vmcs12->idt_vectoring_info_field,
13700 vmcs12->vm_exit_intr_info,
13701 vmcs12->vm_exit_intr_error_code,
13702 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013703
13704 load_vmcs12_host_state(vcpu, vmcs12);
13705
13706 return;
13707 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013708
Jim Mattson4f350c62017-09-14 16:31:44 -070013709 /*
13710 * After an early L2 VM-entry failure, we're now back
13711 * in L1 which thinks it just finished a VMLAUNCH or
13712 * VMRESUME instruction, so we need to set the failure
13713 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070013714 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070013715 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013716 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013717
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013718 /*
13719 * Restore L1's host state to KVM's software model. We're here
13720 * because a consistency check was caught by hardware, which
13721 * means some amount of guest state has been propagated to KVM's
13722 * model and needs to be unwound to the host's state.
13723 */
13724 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013725
Jim Mattson4f350c62017-09-14 16:31:44 -070013726 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013727}
13728
Nadav Har'El7c177932011-05-25 23:12:04 +030013729/*
Jan Kiszka42124922014-01-04 18:47:19 +010013730 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13731 */
13732static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13733{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013734 if (is_guest_mode(vcpu)) {
13735 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013736 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013737 }
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020013738 free_nested(vcpu);
Jan Kiszka42124922014-01-04 18:47:19 +010013739}
13740
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013741static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13742 struct x86_instruction_info *info,
13743 enum x86_intercept_stage stage)
13744{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013745 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13746 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13747
13748 /*
13749 * RDPID causes #UD if disabled through secondary execution controls.
13750 * Because it is marked as EmulateOnUD, we need to intercept it here.
13751 */
13752 if (info->intercept == x86_intercept_rdtscp &&
13753 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13754 ctxt->exception.vector = UD_VECTOR;
13755 ctxt->exception.error_code_valid = false;
13756 return X86EMUL_PROPAGATE_FAULT;
13757 }
13758
13759 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013760 return X86EMUL_CONTINUE;
13761}
13762
Yunhong Jiang64672c92016-06-13 14:19:59 -070013763#ifdef CONFIG_X86_64
13764/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13765static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13766 u64 divisor, u64 *result)
13767{
13768 u64 low = a << shift, high = a >> (64 - shift);
13769
13770 /* To avoid the overflow on divq */
13771 if (high >= divisor)
13772 return 1;
13773
13774 /* Low hold the result, high hold rem which is discarded */
13775 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13776 "rm" (divisor), "0" (low), "1" (high));
13777 *result = low;
13778
13779 return 0;
13780}
13781
13782static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13783{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013784 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013785 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013786
13787 if (kvm_mwait_in_guest(vcpu->kvm))
13788 return -EOPNOTSUPP;
13789
13790 vmx = to_vmx(vcpu);
13791 tscl = rdtsc();
13792 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13793 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013794 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13795
13796 if (delta_tsc > lapic_timer_advance_cycles)
13797 delta_tsc -= lapic_timer_advance_cycles;
13798 else
13799 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013800
13801 /* Convert to host delta tsc if tsc scaling is enabled */
13802 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13803 u64_shl_div_u64(delta_tsc,
13804 kvm_tsc_scaling_ratio_frac_bits,
13805 vcpu->arch.tsc_scaling_ratio,
13806 &delta_tsc))
13807 return -ERANGE;
13808
13809 /*
13810 * If the delta tsc can't fit in the 32 bit after the multi shift,
13811 * we can't use the preemption timer.
13812 * It's possible that it fits on later vmentries, but checking
13813 * on every vmentry is costly so we just use an hrtimer.
13814 */
13815 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13816 return -ERANGE;
13817
13818 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013819 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013820}
13821
13822static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13823{
Sean Christophersonf459a702018-08-27 15:21:11 -070013824 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013825}
13826#endif
13827
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013828static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013829{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013830 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013831 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013832}
13833
Kai Huang843e4332015-01-28 10:54:28 +080013834static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13835 struct kvm_memory_slot *slot)
13836{
13837 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13838 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13839}
13840
13841static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13842 struct kvm_memory_slot *slot)
13843{
13844 kvm_mmu_slot_set_dirty(kvm, slot);
13845}
13846
13847static void vmx_flush_log_dirty(struct kvm *kvm)
13848{
13849 kvm_flush_pml_buffers(kvm);
13850}
13851
Bandan Dasc5f983f2017-05-05 15:25:14 -040013852static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13853{
13854 struct vmcs12 *vmcs12;
13855 struct vcpu_vmx *vmx = to_vmx(vcpu);
13856 gpa_t gpa;
13857 struct page *page = NULL;
13858 u64 *pml_address;
13859
13860 if (is_guest_mode(vcpu)) {
13861 WARN_ON_ONCE(vmx->nested.pml_full);
13862
13863 /*
13864 * Check if PML is enabled for the nested guest.
13865 * Whether eptp bit 6 is set is already checked
13866 * as part of A/D emulation.
13867 */
13868 vmcs12 = get_vmcs12(vcpu);
13869 if (!nested_cpu_has_pml(vmcs12))
13870 return 0;
13871
Dan Carpenter47698862017-05-10 22:43:17 +030013872 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013873 vmx->nested.pml_full = true;
13874 return 1;
13875 }
13876
13877 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13878
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013879 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13880 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013881 return 0;
13882
13883 pml_address = kmap(page);
13884 pml_address[vmcs12->guest_pml_index--] = gpa;
13885 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013886 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013887 }
13888
13889 return 0;
13890}
13891
Kai Huang843e4332015-01-28 10:54:28 +080013892static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13893 struct kvm_memory_slot *memslot,
13894 gfn_t offset, unsigned long mask)
13895{
13896 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13897}
13898
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013899static void __pi_post_block(struct kvm_vcpu *vcpu)
13900{
13901 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13902 struct pi_desc old, new;
13903 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013904
13905 do {
13906 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013907 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13908 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013909
13910 dest = cpu_physical_id(vcpu->cpu);
13911
13912 if (x2apic_enabled())
13913 new.ndst = dest;
13914 else
13915 new.ndst = (dest << 8) & 0xFF00;
13916
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013917 /* set 'NV' to 'notification vector' */
13918 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013919 } while (cmpxchg64(&pi_desc->control, old.control,
13920 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013921
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013922 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13923 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013924 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013925 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013926 vcpu->pre_pcpu = -1;
13927 }
13928}
13929
Feng Wuefc64402015-09-18 22:29:51 +080013930/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013931 * This routine does the following things for vCPU which is going
13932 * to be blocked if VT-d PI is enabled.
13933 * - Store the vCPU to the wakeup list, so when interrupts happen
13934 * we can find the right vCPU to wake up.
13935 * - Change the Posted-interrupt descriptor as below:
13936 * 'NDST' <-- vcpu->pre_pcpu
13937 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13938 * - If 'ON' is set during this process, which means at least one
13939 * interrupt is posted for this vCPU, we cannot block it, in
13940 * this case, return 1, otherwise, return 0.
13941 *
13942 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013943static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013944{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013945 unsigned int dest;
13946 struct pi_desc old, new;
13947 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13948
13949 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013950 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13951 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013952 return 0;
13953
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013954 WARN_ON(irqs_disabled());
13955 local_irq_disable();
13956 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13957 vcpu->pre_pcpu = vcpu->cpu;
13958 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13959 list_add_tail(&vcpu->blocked_vcpu_list,
13960 &per_cpu(blocked_vcpu_on_cpu,
13961 vcpu->pre_pcpu));
13962 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13963 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013964
13965 do {
13966 old.control = new.control = pi_desc->control;
13967
Feng Wubf9f6ac2015-09-18 22:29:55 +080013968 WARN((pi_desc->sn == 1),
13969 "Warning: SN field of posted-interrupts "
13970 "is set before blocking\n");
13971
13972 /*
13973 * Since vCPU can be preempted during this process,
13974 * vcpu->cpu could be different with pre_pcpu, we
13975 * need to set pre_pcpu as the destination of wakeup
13976 * notification event, then we can find the right vCPU
13977 * to wakeup in wakeup handler if interrupts happen
13978 * when the vCPU is in blocked state.
13979 */
13980 dest = cpu_physical_id(vcpu->pre_pcpu);
13981
13982 if (x2apic_enabled())
13983 new.ndst = dest;
13984 else
13985 new.ndst = (dest << 8) & 0xFF00;
13986
13987 /* set 'NV' to 'wakeup vector' */
13988 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013989 } while (cmpxchg64(&pi_desc->control, old.control,
13990 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013991
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013992 /* We should not block the vCPU if an interrupt is posted for it. */
13993 if (pi_test_on(pi_desc) == 1)
13994 __pi_post_block(vcpu);
13995
13996 local_irq_enable();
13997 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013998}
13999
Yunhong Jiangbc225122016-06-13 14:19:58 -070014000static int vmx_pre_block(struct kvm_vcpu *vcpu)
14001{
14002 if (pi_pre_block(vcpu))
14003 return 1;
14004
Yunhong Jiang64672c92016-06-13 14:19:59 -070014005 if (kvm_lapic_hv_timer_in_use(vcpu))
14006 kvm_lapic_switch_to_sw_timer(vcpu);
14007
Yunhong Jiangbc225122016-06-13 14:19:58 -070014008 return 0;
14009}
14010
14011static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014012{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014013 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014014 return;
14015
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014016 WARN_ON(irqs_disabled());
14017 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014018 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014019 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080014020}
14021
Yunhong Jiangbc225122016-06-13 14:19:58 -070014022static void vmx_post_block(struct kvm_vcpu *vcpu)
14023{
Yunhong Jiang64672c92016-06-13 14:19:59 -070014024 if (kvm_x86_ops->set_hv_timer)
14025 kvm_lapic_switch_to_hv_timer(vcpu);
14026
Yunhong Jiangbc225122016-06-13 14:19:58 -070014027 pi_post_block(vcpu);
14028}
14029
Feng Wubf9f6ac2015-09-18 22:29:55 +080014030/*
Feng Wuefc64402015-09-18 22:29:51 +080014031 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14032 *
14033 * @kvm: kvm
14034 * @host_irq: host irq of the interrupt
14035 * @guest_irq: gsi of the interrupt
14036 * @set: set or unset PI
14037 * returns 0 on success, < 0 on failure
14038 */
14039static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14040 uint32_t guest_irq, bool set)
14041{
14042 struct kvm_kernel_irq_routing_entry *e;
14043 struct kvm_irq_routing_table *irq_rt;
14044 struct kvm_lapic_irq irq;
14045 struct kvm_vcpu *vcpu;
14046 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014047 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080014048
14049 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014050 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14051 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080014052 return 0;
14053
14054 idx = srcu_read_lock(&kvm->irq_srcu);
14055 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014056 if (guest_irq >= irq_rt->nr_rt_entries ||
14057 hlist_empty(&irq_rt->map[guest_irq])) {
14058 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14059 guest_irq, irq_rt->nr_rt_entries);
14060 goto out;
14061 }
Feng Wuefc64402015-09-18 22:29:51 +080014062
14063 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14064 if (e->type != KVM_IRQ_ROUTING_MSI)
14065 continue;
14066 /*
14067 * VT-d PI cannot support posting multicast/broadcast
14068 * interrupts to a vCPU, we still use interrupt remapping
14069 * for these kind of interrupts.
14070 *
14071 * For lowest-priority interrupts, we only support
14072 * those with single CPU as the destination, e.g. user
14073 * configures the interrupts via /proc/irq or uses
14074 * irqbalance to make the interrupts single-CPU.
14075 *
14076 * We will support full lowest-priority interrupt later.
14077 */
14078
Radim Krčmář371313132016-07-12 22:09:27 +020014079 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014080 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14081 /*
14082 * Make sure the IRTE is in remapped mode if
14083 * we don't handle it in posted mode.
14084 */
14085 ret = irq_set_vcpu_affinity(host_irq, NULL);
14086 if (ret < 0) {
14087 printk(KERN_INFO
14088 "failed to back to remapped mode, irq: %u\n",
14089 host_irq);
14090 goto out;
14091 }
14092
Feng Wuefc64402015-09-18 22:29:51 +080014093 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014094 }
Feng Wuefc64402015-09-18 22:29:51 +080014095
14096 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14097 vcpu_info.vector = irq.vector;
14098
hu huajun2698d822018-04-11 15:16:40 +080014099 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014100 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14101
14102 if (set)
14103 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014104 else
Feng Wuefc64402015-09-18 22:29:51 +080014105 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014106
14107 if (ret < 0) {
14108 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14109 __func__);
14110 goto out;
14111 }
14112 }
14113
14114 ret = 0;
14115out:
14116 srcu_read_unlock(&kvm->irq_srcu, idx);
14117 return ret;
14118}
14119
Ashok Rajc45dcc72016-06-22 14:59:56 +080014120static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14121{
14122 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14123 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14124 FEATURE_CONTROL_LMCE;
14125 else
14126 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14127 ~FEATURE_CONTROL_LMCE;
14128}
14129
Ladi Prosek72d7b372017-10-11 16:54:41 +020014130static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14131{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014132 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14133 if (to_vmx(vcpu)->nested.nested_run_pending)
14134 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014135 return 1;
14136}
14137
Ladi Prosek0234bf82017-10-11 16:54:40 +020014138static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14139{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014140 struct vcpu_vmx *vmx = to_vmx(vcpu);
14141
14142 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14143 if (vmx->nested.smm.guest_mode)
14144 nested_vmx_vmexit(vcpu, -1, 0, 0);
14145
14146 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14147 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014148 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014149 return 0;
14150}
14151
14152static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14153{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014154 struct vcpu_vmx *vmx = to_vmx(vcpu);
14155 int ret;
14156
14157 if (vmx->nested.smm.vmxon) {
14158 vmx->nested.vmxon = true;
14159 vmx->nested.smm.vmxon = false;
14160 }
14161
14162 if (vmx->nested.smm.guest_mode) {
14163 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014164 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014165 vcpu->arch.hflags |= HF_SMM_MASK;
14166 if (ret)
14167 return ret;
14168
14169 vmx->nested.smm.guest_mode = false;
14170 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014171 return 0;
14172}
14173
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014174static int enable_smi_window(struct kvm_vcpu *vcpu)
14175{
14176 return 0;
14177}
14178
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014179static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14180 struct kvm_nested_state __user *user_kvm_nested_state,
14181 u32 user_data_size)
14182{
14183 struct vcpu_vmx *vmx;
14184 struct vmcs12 *vmcs12;
14185 struct kvm_nested_state kvm_state = {
14186 .flags = 0,
14187 .format = 0,
14188 .size = sizeof(kvm_state),
14189 .vmx.vmxon_pa = -1ull,
14190 .vmx.vmcs_pa = -1ull,
14191 };
14192
14193 if (!vcpu)
14194 return kvm_state.size + 2 * VMCS12_SIZE;
14195
14196 vmx = to_vmx(vcpu);
14197 vmcs12 = get_vmcs12(vcpu);
14198 if (nested_vmx_allowed(vcpu) &&
14199 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14200 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14201 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14202
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014203 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014204 kvm_state.size += VMCS12_SIZE;
14205
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014206 if (is_guest_mode(vcpu) &&
14207 nested_cpu_has_shadow_vmcs(vmcs12) &&
14208 vmcs12->vmcs_link_pointer != -1ull)
14209 kvm_state.size += VMCS12_SIZE;
14210 }
14211
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014212 if (vmx->nested.smm.vmxon)
14213 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14214
14215 if (vmx->nested.smm.guest_mode)
14216 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14217
14218 if (is_guest_mode(vcpu)) {
14219 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14220
14221 if (vmx->nested.nested_run_pending)
14222 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14223 }
14224 }
14225
14226 if (user_data_size < kvm_state.size)
14227 goto out;
14228
14229 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14230 return -EFAULT;
14231
14232 if (vmx->nested.current_vmptr == -1ull)
14233 goto out;
14234
14235 /*
14236 * When running L2, the authoritative vmcs12 state is in the
14237 * vmcs02. When running L1, the authoritative vmcs12 state is
14238 * in the shadow vmcs linked to vmcs01, unless
14239 * sync_shadow_vmcs is set, in which case, the authoritative
14240 * vmcs12 state is in the vmcs12 already.
14241 */
14242 if (is_guest_mode(vcpu))
14243 sync_vmcs12(vcpu, vmcs12);
14244 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14245 copy_shadow_to_vmcs12(vmx);
14246
14247 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14248 return -EFAULT;
14249
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014250 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14251 vmcs12->vmcs_link_pointer != -1ull) {
14252 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14253 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14254 return -EFAULT;
14255 }
14256
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014257out:
14258 return kvm_state.size;
14259}
14260
14261static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14262 struct kvm_nested_state __user *user_kvm_nested_state,
14263 struct kvm_nested_state *kvm_state)
14264{
14265 struct vcpu_vmx *vmx = to_vmx(vcpu);
14266 struct vmcs12 *vmcs12;
14267 u32 exit_qual;
14268 int ret;
14269
14270 if (kvm_state->format != 0)
14271 return -EINVAL;
14272
14273 if (!nested_vmx_allowed(vcpu))
14274 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14275
14276 if (kvm_state->vmx.vmxon_pa == -1ull) {
14277 if (kvm_state->vmx.smm.flags)
14278 return -EINVAL;
14279
14280 if (kvm_state->vmx.vmcs_pa != -1ull)
14281 return -EINVAL;
14282
14283 vmx_leave_nested(vcpu);
14284 return 0;
14285 }
14286
14287 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14288 return -EINVAL;
14289
14290 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14291 return -EINVAL;
14292
14293 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14294 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14295 return -EINVAL;
14296
14297 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14298 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14299 return -EINVAL;
14300
14301 if (kvm_state->vmx.smm.flags &
14302 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14303 return -EINVAL;
14304
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014305 /*
14306 * SMM temporarily disables VMX, so we cannot be in guest mode,
14307 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14308 * must be zero.
14309 */
14310 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14311 return -EINVAL;
14312
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014313 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14314 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14315 return -EINVAL;
14316
14317 vmx_leave_nested(vcpu);
14318 if (kvm_state->vmx.vmxon_pa == -1ull)
14319 return 0;
14320
14321 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14322 ret = enter_vmx_operation(vcpu);
14323 if (ret)
14324 return ret;
14325
14326 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14327
14328 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14329 vmx->nested.smm.vmxon = true;
14330 vmx->nested.vmxon = false;
14331
14332 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14333 vmx->nested.smm.guest_mode = true;
14334 }
14335
14336 vmcs12 = get_vmcs12(vcpu);
14337 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14338 return -EFAULT;
14339
Liran Alon392b2f22018-06-23 02:35:01 +030014340 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014341 return -EINVAL;
14342
14343 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14344 return 0;
14345
14346 vmx->nested.nested_run_pending =
14347 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14348
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014349 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14350 vmcs12->vmcs_link_pointer != -1ull) {
14351 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14352 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14353 return -EINVAL;
14354
14355 if (copy_from_user(shadow_vmcs12,
14356 user_kvm_nested_state->data + VMCS12_SIZE,
14357 sizeof(*vmcs12)))
14358 return -EFAULT;
14359
14360 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14361 !shadow_vmcs12->hdr.shadow_vmcs)
14362 return -EINVAL;
14363 }
14364
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014365 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14366 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14367 return -EINVAL;
14368
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014369 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014370 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014371 if (ret)
14372 return -EINVAL;
14373
14374 return 0;
14375}
14376
Kees Cook404f6aa2016-08-08 16:29:06 -070014377static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014378 .cpu_has_kvm_support = cpu_has_kvm_support,
14379 .disabled_by_bios = vmx_disabled_by_bios,
14380 .hardware_setup = hardware_setup,
14381 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014382 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014383 .hardware_enable = hardware_enable,
14384 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014385 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014386 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014387
Wanpeng Lib31c1142018-03-12 04:53:04 -070014388 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014389 .vm_alloc = vmx_vm_alloc,
14390 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014391
Avi Kivity6aa8b732006-12-10 02:21:36 -080014392 .vcpu_create = vmx_create_vcpu,
14393 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014394 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014395
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014396 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014397 .vcpu_load = vmx_vcpu_load,
14398 .vcpu_put = vmx_vcpu_put,
14399
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014400 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014401 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014402 .get_msr = vmx_get_msr,
14403 .set_msr = vmx_set_msr,
14404 .get_segment_base = vmx_get_segment_base,
14405 .get_segment = vmx_get_segment,
14406 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014407 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014408 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014409 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014410 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014411 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014412 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014413 .set_cr3 = vmx_set_cr3,
14414 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014415 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014416 .get_idt = vmx_get_idt,
14417 .set_idt = vmx_set_idt,
14418 .get_gdt = vmx_get_gdt,
14419 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014420 .get_dr6 = vmx_get_dr6,
14421 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014422 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014423 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014424 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014425 .get_rflags = vmx_get_rflags,
14426 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014427
Avi Kivity6aa8b732006-12-10 02:21:36 -080014428 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014429 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014430
Avi Kivity6aa8b732006-12-10 02:21:36 -080014431 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014432 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014433 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014434 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14435 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014436 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014437 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014438 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014439 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014440 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014441 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014442 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014443 .get_nmi_mask = vmx_get_nmi_mask,
14444 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014445 .enable_nmi_window = enable_nmi_window,
14446 .enable_irq_window = enable_irq_window,
14447 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014448 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014449 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014450 .get_enable_apicv = vmx_get_enable_apicv,
14451 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014452 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014453 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014454 .hwapic_irr_update = vmx_hwapic_irr_update,
14455 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014456 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014457 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14458 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014459
Izik Eiduscbc94022007-10-25 00:29:55 +020014460 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014461 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014462 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014463 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014464
Avi Kivity586f9602010-11-18 13:09:54 +020014465 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014466
Sheng Yang17cc3932010-01-05 19:02:27 +080014467 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014468
14469 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014470
14471 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014472 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014473
14474 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014475
14476 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014477
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014478 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014479 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014480
14481 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014482
14483 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014484 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014485 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014486 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014487 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014488
14489 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014490 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014491
14492 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014493
14494 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14495 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14496 .flush_log_dirty = vmx_flush_log_dirty,
14497 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014498 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014499
Feng Wubf9f6ac2015-09-18 22:29:55 +080014500 .pre_block = vmx_pre_block,
14501 .post_block = vmx_post_block,
14502
Wei Huang25462f72015-06-19 15:45:05 +020014503 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014504
14505 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014506
14507#ifdef CONFIG_X86_64
14508 .set_hv_timer = vmx_set_hv_timer,
14509 .cancel_hv_timer = vmx_cancel_hv_timer,
14510#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014511
14512 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014513
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014514 .get_nested_state = vmx_get_nested_state,
14515 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014516 .get_vmcs12_pages = nested_get_vmcs12_pages,
14517
Ladi Prosek72d7b372017-10-11 16:54:41 +020014518 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014519 .pre_enter_smm = vmx_pre_enter_smm,
14520 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014521 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014522};
14523
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014524static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014525{
14526 if (vmx_l1d_flush_pages) {
14527 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14528 vmx_l1d_flush_pages = NULL;
14529 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014530 /* Restore state so sysfs ignores VMX */
14531 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014532}
14533
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014534static void vmx_exit(void)
14535{
14536#ifdef CONFIG_KEXEC_CORE
14537 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14538 synchronize_rcu();
14539#endif
14540
14541 kvm_exit();
14542
14543#if IS_ENABLED(CONFIG_HYPERV)
14544 if (static_branch_unlikely(&enable_evmcs)) {
14545 int cpu;
14546 struct hv_vp_assist_page *vp_ap;
14547 /*
14548 * Reset everything to support using non-enlightened VMCS
14549 * access later (e.g. when we reload the module with
14550 * enlightened_vmcs=0)
14551 */
14552 for_each_online_cpu(cpu) {
14553 vp_ap = hv_get_vp_assist_page(cpu);
14554
14555 if (!vp_ap)
14556 continue;
14557
14558 vp_ap->current_nested_vmcs = 0;
14559 vp_ap->enlighten_vmentry = 0;
14560 }
14561
14562 static_branch_disable(&enable_evmcs);
14563 }
14564#endif
14565 vmx_cleanup_l1d_flush();
14566}
14567module_exit(vmx_exit);
14568
Avi Kivity6aa8b732006-12-10 02:21:36 -080014569static int __init vmx_init(void)
14570{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014571 int r;
14572
14573#if IS_ENABLED(CONFIG_HYPERV)
14574 /*
14575 * Enlightened VMCS usage should be recommended and the host needs
14576 * to support eVMCS v1 or above. We can also disable eVMCS support
14577 * with module parameter.
14578 */
14579 if (enlightened_vmcs &&
14580 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14581 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14582 KVM_EVMCS_VERSION) {
14583 int cpu;
14584
14585 /* Check that we have assist pages on all online CPUs */
14586 for_each_online_cpu(cpu) {
14587 if (!hv_get_vp_assist_page(cpu)) {
14588 enlightened_vmcs = false;
14589 break;
14590 }
14591 }
14592
14593 if (enlightened_vmcs) {
14594 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14595 static_branch_enable(&enable_evmcs);
14596 }
14597 } else {
14598 enlightened_vmcs = false;
14599 }
14600#endif
14601
14602 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014603 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014604 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014605 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014606
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014607 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014608 * Must be called after kvm_init() so enable_ept is properly set
14609 * up. Hand the parameter mitigation value in which was stored in
14610 * the pre module init parser. If no parameter was given, it will
14611 * contain 'auto' which will be turned into the default 'cond'
14612 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014613 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014614 if (boot_cpu_has(X86_BUG_L1TF)) {
14615 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14616 if (r) {
14617 vmx_exit();
14618 return r;
14619 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014620 }
14621
Dave Young2965faa2015-09-09 15:38:55 -070014622#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014623 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14624 crash_vmclear_local_loaded_vmcss);
14625#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014626 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014627
He, Qingfdef3ad2007-04-30 09:45:24 +030014628 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014629}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014630module_init(vmx_init);