blob: 4231ccb68fded472ea5cb81fa26c72a648ecc6cb [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 Poimboeuf935893a2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williamseb99bd62018-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
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Paolo Bonzinia175d512018-02-22 16:43:17 +010053#include <asm/microcode.h>
Thomas Gleixner7a2d2352018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020057#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058
Avi Kivity4ecac3f2008-05-13 13:23:38 +030059#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040060#define __ex_clear(x, reg) \
61 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Josh Triplette9bda3b2012-03-20 23:33:51 -070066static const struct x86_cpu_id vmx_cpu_id[] = {
67 X86_FEATURE_MATCH(X86_FEATURE_VMX),
68 {}
69};
70MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
71
Rusty Russell476bc002012-01-13 09:32:18 +103072static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020073module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080074
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020077
Rusty Russell476bc002012-01-13 09:32:18 +103078static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020079module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080080
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070082module_param_named(unrestricted_guest,
83 enable_unrestricted_guest, bool, S_IRUGO);
84
Xudong Hao83c3a332012-05-28 19:33:35 +080085static bool __read_mostly enable_ept_ad_bits = 1;
86module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
87
Avi Kivitya27685c2012-06-12 20:30:18 +030088static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020089module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030090
Rusty Russell476bc002012-01-13 09:32:18 +103091static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080092module_param(vmm_exclusive, bool, S_IRUGO);
93
Rusty Russell476bc002012-01-13 09:32:18 +103094static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030095module_param(fasteoi, bool, S_IRUGO);
96
Yang Zhang5a717852013-04-11 19:25:16 +080097static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080098module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080099
Abel Gordonabc4fc52013-04-18 14:35:25 +0300100static bool __read_mostly enable_shadow_vmcs = 1;
101module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300102/*
103 * If nested=1, nested virtualization is supported, i.e., guests may use
104 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
105 * use VMX instructions.
106 */
Rusty Russell476bc002012-01-13 09:32:18 +1030107static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300108module_param(nested, bool, S_IRUGO);
109
Wanpeng Li20300092014-12-02 19:14:59 +0800110static u64 __read_mostly host_xss;
111
Kai Huang843e4332015-01-28 10:54:28 +0800112static bool __read_mostly enable_pml = 1;
113module_param_named(pml, enable_pml, bool, S_IRUGO);
114
Paolo Bonzini6236b782018-01-16 16:51:18 +0100115#define MSR_TYPE_R 1
116#define MSR_TYPE_W 2
117#define MSR_TYPE_RW 3
118
119#define MSR_BITMAP_MODE_X2APIC 1
120#define MSR_BITMAP_MODE_X2APIC_APICV 2
121#define MSR_BITMAP_MODE_LM 4
122
Haozhong Zhang64903d62015-10-20 15:39:09 +0800123#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
124
Yunhong Jiang64672c92016-06-13 14:19:59 -0700125/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
126static int __read_mostly cpu_preemption_timer_multi;
127static bool __read_mostly enable_preemption_timer = 1;
128#ifdef CONFIG_X86_64
129module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
130#endif
131
Gleb Natapov50378782013-02-04 16:00:28 +0200132#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
133#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200134#define KVM_VM_CR0_ALWAYS_ON \
135 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200136#define KVM_CR4_GUEST_OWNED_BITS \
137 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700138 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200139
Avi Kivitycdc0e242009-12-06 17:21:14 +0200140#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
141#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
142
Avi Kivity78ac8b42010-04-08 18:19:35 +0300143#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
144
Jan Kiszkaf4124502014-03-07 20:03:13 +0100145#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
146
Jan Dakinevich04c776e2018-02-23 11:42:18 +0100147#define VMX_VPID_EXTENT_SUPPORTED_MASK \
148 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
149 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
150 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
151 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153/*
154 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
155 * ple_gap: upper bound on the amount of time between two successive
156 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500157 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800158 * ple_window: upper bound on the amount of time a guest is allowed to execute
159 * in a PAUSE loop. Tests indicate that most spinlocks are held for
160 * less than 2^12 cycles
161 * Time is measured based on a counter that runs at the same rate as the TSC,
162 * refer SDM volume 3b section 21.6.13 & 22.1.3.
163 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200164#define KVM_VMX_DEFAULT_PLE_GAP 128
165#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
166#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
167#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
168#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
169 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
170
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800171static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
172module_param(ple_gap, int, S_IRUGO);
173
174static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, int, S_IRUGO);
176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
178static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
179module_param(ple_window_grow, int, S_IRUGO);
180
181/* Default resets per-vcpu window every exit to ple_window. */
182static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
183module_param(ple_window_shrink, int, S_IRUGO);
184
185/* Default is to compute the maximum so we can never overflow. */
186static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, int, S_IRUGO);
189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange936f5662018-07-21 22:25:00 +0200193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200194static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200195
Thomas Gleixner641a2112018-07-13 16:23:19 +0200196/* Storage for pre module init parameter parsing */
197static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200198
199static const struct {
200 const char *option;
201 enum vmx_l1d_flush_state cmd;
202} vmentry_l1d_param[] = {
Thomas Gleixner80e55b52018-07-13 16:23:16 +0200203 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200204 {"never", VMENTER_L1D_FLUSH_NEVER},
205 {"cond", VMENTER_L1D_FLUSH_COND},
206 {"always", VMENTER_L1D_FLUSH_ALWAYS},
207};
208
Thomas Gleixner641a2112018-07-13 16:23:19 +0200209#define L1D_CACHE_ORDER 4
210static void *vmx_l1d_flush_pages;
211
212static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
213{
214 struct page *page;
Nicolai Stange587d4992018-07-18 19:07:38 +0200215 unsigned int i;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200216
Thomas Gleixner641a2112018-07-13 16:23:19 +0200217 if (!enable_ept) {
218 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
219 return 0;
220 }
221
Paolo Bonzini383f1602018-08-05 16:07:46 +0200222 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
223 u64 msr;
224
225 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
226 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
227 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
228 return 0;
229 }
230 }
231
Jiri Kosina2decbf52018-07-13 16:23:25 +0200232 /* If set to auto use the default l1tf mitigation method */
233 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
234 switch (l1tf_mitigation) {
235 case L1TF_MITIGATION_OFF:
236 l1tf = VMENTER_L1D_FLUSH_NEVER;
237 break;
238 case L1TF_MITIGATION_FLUSH_NOWARN:
239 case L1TF_MITIGATION_FLUSH:
240 case L1TF_MITIGATION_FLUSH_NOSMT:
241 l1tf = VMENTER_L1D_FLUSH_COND;
242 break;
243 case L1TF_MITIGATION_FULL:
244 case L1TF_MITIGATION_FULL_FORCE:
245 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
246 break;
247 }
248 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
249 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
250 }
251
Thomas Gleixner641a2112018-07-13 16:23:19 +0200252 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
253 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
254 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
255 if (!page)
256 return -ENOMEM;
257 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange587d4992018-07-18 19:07:38 +0200258
259 /*
260 * Initialize each page with a different pattern in
261 * order to protect against KSM in the nested
262 * virtualization case.
263 */
264 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
265 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
266 PAGE_SIZE);
267 }
Thomas Gleixner641a2112018-07-13 16:23:19 +0200268 }
269
270 l1tf_vmx_mitigation = l1tf;
271
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200272 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
273 static_branch_enable(&vmx_l1d_should_flush);
274 else
275 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixnerdff09822018-07-13 16:23:20 +0200276
Nicolai Stange936f5662018-07-21 22:25:00 +0200277 if (l1tf == VMENTER_L1D_FLUSH_COND)
278 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200279 else
Nicolai Stange936f5662018-07-21 22:25:00 +0200280 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner641a2112018-07-13 16:23:19 +0200281 return 0;
282}
283
284static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200285{
286 unsigned int i;
287
Thomas Gleixner641a2112018-07-13 16:23:19 +0200288 if (s) {
289 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200290 if (sysfs_streq(s, vmentry_l1d_param[i].option))
Thomas Gleixner641a2112018-07-13 16:23:19 +0200291 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200292 }
293 }
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200294 return -EINVAL;
295}
296
Thomas Gleixner641a2112018-07-13 16:23:19 +0200297static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
298{
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200299 int l1tf, ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200300
301 if (!boot_cpu_has(X86_BUG_L1TF))
302 return 0;
303
304 l1tf = vmentry_l1d_flush_parse(s);
305 if (l1tf < 0)
306 return l1tf;
307
308 /*
309 * Has vmx_init() run already? If not then this is the pre init
310 * parameter parsing. In that case just store the value and let
311 * vmx_init() do the proper setup after enable_ept has been
312 * established.
313 */
314 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
315 vmentry_l1d_flush_param = l1tf;
316 return 0;
317 }
318
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200319 mutex_lock(&vmx_l1d_flush_mutex);
320 ret = vmx_setup_l1d_flush(l1tf);
321 mutex_unlock(&vmx_l1d_flush_mutex);
322 return ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200323}
324
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200325static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
326{
Thomas Gleixner641a2112018-07-13 16:23:19 +0200327 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200328}
329
330static const struct kernel_param_ops vmentry_l1d_flush_ops = {
331 .set = vmentry_l1d_flush_set,
332 .get = vmentry_l1d_flush_get,
333};
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200334module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200335
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200336#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300337
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400338struct vmcs {
339 u32 revision_id;
340 u32 abort;
341 char data[0];
342};
343
Nadav Har'Eld462b812011-05-24 15:26:10 +0300344/*
345 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
346 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
347 * loaded on this CPU (so we can clear them if the CPU goes down).
348 */
349struct loaded_vmcs {
350 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700351 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300352 int cpu;
353 int launched;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100354 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300355 struct list_head loaded_vmcss_on_cpu_link;
356};
357
Avi Kivity26bb0982009-09-07 11:14:12 +0300358struct shared_msr_entry {
359 unsigned index;
360 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200361 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300362};
363
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300364/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300365 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
366 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
367 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
368 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
369 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
370 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson46e24df2017-11-27 17:22:25 -0600371 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300372 * underlying hardware which will be used to run L2.
373 * This structure is packed to ensure that its layout is identical across
374 * machines (necessary for live migration).
375 * If there are changes in this struct, VMCS12_REVISION must be changed.
376 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300377typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300378struct __packed vmcs12 {
379 /* According to the Intel spec, a VMCS region must start with the
380 * following two fields. Then follow implementation-specific data.
381 */
382 u32 revision_id;
383 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300384
Nadav Har'El27d6c862011-05-25 23:06:59 +0300385 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
386 u32 padding[7]; /* room for future expansion */
387
Nadav Har'El22bd0352011-05-25 23:05:57 +0300388 u64 io_bitmap_a;
389 u64 io_bitmap_b;
390 u64 msr_bitmap;
391 u64 vm_exit_msr_store_addr;
392 u64 vm_exit_msr_load_addr;
393 u64 vm_entry_msr_load_addr;
394 u64 tsc_offset;
395 u64 virtual_apic_page_addr;
396 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800397 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300398 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800399 u64 eoi_exit_bitmap0;
400 u64 eoi_exit_bitmap1;
401 u64 eoi_exit_bitmap2;
402 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800403 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300404 u64 guest_physical_address;
405 u64 vmcs_link_pointer;
406 u64 guest_ia32_debugctl;
407 u64 guest_ia32_pat;
408 u64 guest_ia32_efer;
409 u64 guest_ia32_perf_global_ctrl;
410 u64 guest_pdptr0;
411 u64 guest_pdptr1;
412 u64 guest_pdptr2;
413 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100414 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300415 u64 host_ia32_pat;
416 u64 host_ia32_efer;
417 u64 host_ia32_perf_global_ctrl;
418 u64 padding64[8]; /* room for future expansion */
419 /*
420 * To allow migration of L1 (complete with its L2 guests) between
421 * machines of different natural widths (32 or 64 bit), we cannot have
422 * unsigned long fields with no explict size. We use u64 (aliased
423 * natural_width) instead. Luckily, x86 is little-endian.
424 */
425 natural_width cr0_guest_host_mask;
426 natural_width cr4_guest_host_mask;
427 natural_width cr0_read_shadow;
428 natural_width cr4_read_shadow;
429 natural_width cr3_target_value0;
430 natural_width cr3_target_value1;
431 natural_width cr3_target_value2;
432 natural_width cr3_target_value3;
433 natural_width exit_qualification;
434 natural_width guest_linear_address;
435 natural_width guest_cr0;
436 natural_width guest_cr3;
437 natural_width guest_cr4;
438 natural_width guest_es_base;
439 natural_width guest_cs_base;
440 natural_width guest_ss_base;
441 natural_width guest_ds_base;
442 natural_width guest_fs_base;
443 natural_width guest_gs_base;
444 natural_width guest_ldtr_base;
445 natural_width guest_tr_base;
446 natural_width guest_gdtr_base;
447 natural_width guest_idtr_base;
448 natural_width guest_dr7;
449 natural_width guest_rsp;
450 natural_width guest_rip;
451 natural_width guest_rflags;
452 natural_width guest_pending_dbg_exceptions;
453 natural_width guest_sysenter_esp;
454 natural_width guest_sysenter_eip;
455 natural_width host_cr0;
456 natural_width host_cr3;
457 natural_width host_cr4;
458 natural_width host_fs_base;
459 natural_width host_gs_base;
460 natural_width host_tr_base;
461 natural_width host_gdtr_base;
462 natural_width host_idtr_base;
463 natural_width host_ia32_sysenter_esp;
464 natural_width host_ia32_sysenter_eip;
465 natural_width host_rsp;
466 natural_width host_rip;
467 natural_width paddingl[8]; /* room for future expansion */
468 u32 pin_based_vm_exec_control;
469 u32 cpu_based_vm_exec_control;
470 u32 exception_bitmap;
471 u32 page_fault_error_code_mask;
472 u32 page_fault_error_code_match;
473 u32 cr3_target_count;
474 u32 vm_exit_controls;
475 u32 vm_exit_msr_store_count;
476 u32 vm_exit_msr_load_count;
477 u32 vm_entry_controls;
478 u32 vm_entry_msr_load_count;
479 u32 vm_entry_intr_info_field;
480 u32 vm_entry_exception_error_code;
481 u32 vm_entry_instruction_len;
482 u32 tpr_threshold;
483 u32 secondary_vm_exec_control;
484 u32 vm_instruction_error;
485 u32 vm_exit_reason;
486 u32 vm_exit_intr_info;
487 u32 vm_exit_intr_error_code;
488 u32 idt_vectoring_info_field;
489 u32 idt_vectoring_error_code;
490 u32 vm_exit_instruction_len;
491 u32 vmx_instruction_info;
492 u32 guest_es_limit;
493 u32 guest_cs_limit;
494 u32 guest_ss_limit;
495 u32 guest_ds_limit;
496 u32 guest_fs_limit;
497 u32 guest_gs_limit;
498 u32 guest_ldtr_limit;
499 u32 guest_tr_limit;
500 u32 guest_gdtr_limit;
501 u32 guest_idtr_limit;
502 u32 guest_es_ar_bytes;
503 u32 guest_cs_ar_bytes;
504 u32 guest_ss_ar_bytes;
505 u32 guest_ds_ar_bytes;
506 u32 guest_fs_ar_bytes;
507 u32 guest_gs_ar_bytes;
508 u32 guest_ldtr_ar_bytes;
509 u32 guest_tr_ar_bytes;
510 u32 guest_interruptibility_info;
511 u32 guest_activity_state;
512 u32 guest_sysenter_cs;
513 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100514 u32 vmx_preemption_timer_value;
515 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300516 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800517 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300518 u16 guest_es_selector;
519 u16 guest_cs_selector;
520 u16 guest_ss_selector;
521 u16 guest_ds_selector;
522 u16 guest_fs_selector;
523 u16 guest_gs_selector;
524 u16 guest_ldtr_selector;
525 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800526 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300527 u16 host_es_selector;
528 u16 host_cs_selector;
529 u16 host_ss_selector;
530 u16 host_ds_selector;
531 u16 host_fs_selector;
532 u16 host_gs_selector;
533 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300534};
535
536/*
537 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
538 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
539 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
540 */
541#define VMCS12_REVISION 0x11e57ed0
542
543/*
544 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
545 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
546 * current implementation, 4K are reserved to avoid future complications.
547 */
548#define VMCS12_SIZE 0x1000
549
550/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300551 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
552 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
553 */
554struct nested_vmx {
555 /* Has the level1 guest done vmxon? */
556 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400557 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300558
559 /* The guest-physical address of the current VMCS L1 keeps for L2 */
560 gpa_t current_vmptr;
561 /* The host-usable pointer to the above */
562 struct page *current_vmcs12_page;
563 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700564 /*
565 * Cache of the guest's VMCS, existing outside of guest memory.
566 * Loaded from guest memory during VMPTRLD. Flushed to guest
567 * memory during VMXOFF, VMCLEAR, VMPTRLD.
568 */
569 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300570 /*
571 * Indicates if the shadow vmcs must be updated with the
572 * data hold by vmcs12
573 */
574 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300575
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200576 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300577 /* L2 must run next, and mustn't decide to exit to L1. */
578 bool nested_run_pending;
Jim Mattson46e24df2017-11-27 17:22:25 -0600579
580 struct loaded_vmcs vmcs02;
581
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300582 /*
Jim Mattson46e24df2017-11-27 17:22:25 -0600583 * Guest pages referred to in the vmcs02 with host-physical
584 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300585 */
586 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800587 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800588 struct page *pi_desc_page;
589 struct pi_desc *pi_desc;
590 bool pi_pending;
591 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100592
593 struct hrtimer preemption_timer;
594 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200595
596 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
597 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800598
Wanpeng Li5c614b32015-10-13 09:18:36 -0700599 u16 vpid02;
600 u16 last_vpid;
601
Wincy Vanb9c237b2015-02-03 23:56:30 +0800602 u32 nested_vmx_procbased_ctls_low;
603 u32 nested_vmx_procbased_ctls_high;
604 u32 nested_vmx_true_procbased_ctls_low;
605 u32 nested_vmx_secondary_ctls_low;
606 u32 nested_vmx_secondary_ctls_high;
607 u32 nested_vmx_pinbased_ctls_low;
608 u32 nested_vmx_pinbased_ctls_high;
609 u32 nested_vmx_exit_ctls_low;
610 u32 nested_vmx_exit_ctls_high;
611 u32 nested_vmx_true_exit_ctls_low;
612 u32 nested_vmx_entry_ctls_low;
613 u32 nested_vmx_entry_ctls_high;
614 u32 nested_vmx_true_entry_ctls_low;
615 u32 nested_vmx_misc_low;
616 u32 nested_vmx_misc_high;
617 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700618 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300619};
620
Yang Zhang01e439b2013-04-11 19:25:12 +0800621#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800622#define POSTED_INTR_SN 1
623
Yang Zhang01e439b2013-04-11 19:25:12 +0800624/* Posted-Interrupt Descriptor */
625struct pi_desc {
626 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800627 union {
628 struct {
629 /* bit 256 - Outstanding Notification */
630 u16 on : 1,
631 /* bit 257 - Suppress Notification */
632 sn : 1,
633 /* bit 271:258 - Reserved */
634 rsvd_1 : 14;
635 /* bit 279:272 - Notification Vector */
636 u8 nv;
637 /* bit 287:280 - Reserved */
638 u8 rsvd_2;
639 /* bit 319:288 - Notification Destination */
640 u32 ndst;
641 };
642 u64 control;
643 };
644 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800645} __aligned(64);
646
Yang Zhanga20ed542013-04-11 19:25:15 +0800647static bool pi_test_and_set_on(struct pi_desc *pi_desc)
648{
649 return test_and_set_bit(POSTED_INTR_ON,
650 (unsigned long *)&pi_desc->control);
651}
652
653static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
654{
655 return test_and_clear_bit(POSTED_INTR_ON,
656 (unsigned long *)&pi_desc->control);
657}
658
659static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
660{
661 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
662}
663
Feng Wuebbfc762015-09-18 22:29:46 +0800664static inline void pi_clear_sn(struct pi_desc *pi_desc)
665{
666 return clear_bit(POSTED_INTR_SN,
667 (unsigned long *)&pi_desc->control);
668}
669
670static inline void pi_set_sn(struct pi_desc *pi_desc)
671{
672 return set_bit(POSTED_INTR_SN,
673 (unsigned long *)&pi_desc->control);
674}
675
676static inline int pi_test_on(struct pi_desc *pi_desc)
677{
678 return test_bit(POSTED_INTR_ON,
679 (unsigned long *)&pi_desc->control);
680}
681
682static inline int pi_test_sn(struct pi_desc *pi_desc)
683{
684 return test_bit(POSTED_INTR_SN,
685 (unsigned long *)&pi_desc->control);
686}
687
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400688struct vmx_msrs {
689 unsigned int nr;
690 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
691};
692
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400693struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000694 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300695 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300696 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200697 bool nmi_known_unmasked;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100698 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300699 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200700 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200701 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300702 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400703 int nmsrs;
704 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800705 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400706#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300707 u64 msr_host_kernel_gs_base;
708 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400709#endif
Ashok Raj70131292018-02-01 22:59:43 +0100710
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100711 u64 arch_capabilities;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +0100712 u64 spec_ctrl;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100713
Gleb Natapov2961e8762013-11-25 15:37:13 +0200714 u32 vm_entry_controls_shadow;
715 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300716 /*
717 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
718 * non-nested (L1) guest, it always points to vmcs01. For a nested
719 * guest (L2), it points to a different VMCS.
720 */
721 struct loaded_vmcs vmcs01;
722 struct loaded_vmcs *loaded_vmcs;
723 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300724 struct msr_autoload {
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400725 struct vmx_msrs guest;
726 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300727 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400728 struct {
729 int loaded;
730 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300731#ifdef CONFIG_X86_64
732 u16 ds_sel, es_sel;
733#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200734 int gs_ldt_reload_needed;
735 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000736 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700737 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400738 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200739 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300740 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300741 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300742 struct kvm_segment segs[8];
743 } rmode;
744 struct {
745 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300746 struct kvm_save_segment {
747 u16 selector;
748 unsigned long base;
749 u32 limit;
750 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300751 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300752 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800753 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300754 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200755
756 /* Support for vnmi-less CPUs */
757 int soft_vnmi_blocked;
758 ktime_t entry_time;
759 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800760 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800761
Yang Zhang01e439b2013-04-11 19:25:12 +0800762 /* Posted interrupt descriptor */
763 struct pi_desc pi_desc;
764
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300765 /* Support for a guest hypervisor (nested VMX) */
766 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200767
768 /* Dynamic PLE window. */
769 int ple_window;
770 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800771
772 /* Support for PML */
773#define PML_ENTITY_NUM 512
774 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800775
Yunhong Jiang64672c92016-06-13 14:19:59 -0700776 /* apic deadline value in host tsc */
777 u64 hv_deadline_tsc;
778
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800779 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800780
781 bool guest_pkru_valid;
782 u32 guest_pkru;
783 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800784
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800785 /*
786 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
787 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
788 * in msr_ia32_feature_control_valid_bits.
789 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800790 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800791 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400792};
793
Avi Kivity2fb92db2011-04-27 19:42:18 +0300794enum segment_cache_field {
795 SEG_FIELD_SEL = 0,
796 SEG_FIELD_BASE = 1,
797 SEG_FIELD_LIMIT = 2,
798 SEG_FIELD_AR = 3,
799
800 SEG_FIELD_NR = 4
801};
802
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400803static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
804{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000805 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400806}
807
Feng Wuefc64402015-09-18 22:29:51 +0800808static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
809{
810 return &(to_vmx(vcpu)->pi_desc);
811}
812
Nadav Har'El22bd0352011-05-25 23:05:57 +0300813#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
814#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
815#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
816 [number##_HIGH] = VMCS12_OFFSET(name)+4
817
Abel Gordon4607c2d2013-04-18 14:35:55 +0300818
Bandan Dasfe2b2012014-04-21 15:20:14 -0400819static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300820 /*
821 * We do NOT shadow fields that are modified when L0
822 * traps and emulates any vmx instruction (e.g. VMPTRLD,
823 * VMXON...) executed by L1.
824 * For example, VM_INSTRUCTION_ERROR is read
825 * by L1 if a vmx instruction fails (part of the error path).
826 * Note the code assumes this logic. If for some reason
827 * we start shadowing these fields then we need to
828 * force a shadow sync when L0 emulates vmx instructions
829 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
830 * by nested_vmx_failValid)
831 */
832 VM_EXIT_REASON,
833 VM_EXIT_INTR_INFO,
834 VM_EXIT_INSTRUCTION_LEN,
835 IDT_VECTORING_INFO_FIELD,
836 IDT_VECTORING_ERROR_CODE,
837 VM_EXIT_INTR_ERROR_CODE,
838 EXIT_QUALIFICATION,
839 GUEST_LINEAR_ADDRESS,
840 GUEST_PHYSICAL_ADDRESS
841};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400842static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300843 ARRAY_SIZE(shadow_read_only_fields);
844
Bandan Dasfe2b2012014-04-21 15:20:14 -0400845static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800846 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300847 GUEST_RIP,
848 GUEST_RSP,
849 GUEST_CR0,
850 GUEST_CR3,
851 GUEST_CR4,
852 GUEST_INTERRUPTIBILITY_INFO,
853 GUEST_RFLAGS,
854 GUEST_CS_SELECTOR,
855 GUEST_CS_AR_BYTES,
856 GUEST_CS_LIMIT,
857 GUEST_CS_BASE,
858 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100859 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300860 CR0_GUEST_HOST_MASK,
861 CR0_READ_SHADOW,
862 CR4_READ_SHADOW,
863 TSC_OFFSET,
864 EXCEPTION_BITMAP,
865 CPU_BASED_VM_EXEC_CONTROL,
866 VM_ENTRY_EXCEPTION_ERROR_CODE,
867 VM_ENTRY_INTR_INFO_FIELD,
868 VM_ENTRY_INSTRUCTION_LEN,
869 VM_ENTRY_EXCEPTION_ERROR_CODE,
870 HOST_FS_BASE,
871 HOST_GS_BASE,
872 HOST_FS_SELECTOR,
873 HOST_GS_SELECTOR
874};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400875static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300876 ARRAY_SIZE(shadow_read_write_fields);
877
Mathias Krause772e0312012-08-30 01:30:19 +0200878static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300879 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800880 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300881 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
882 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
883 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
884 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
885 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
886 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
887 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
888 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800889 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300890 FIELD(HOST_ES_SELECTOR, host_es_selector),
891 FIELD(HOST_CS_SELECTOR, host_cs_selector),
892 FIELD(HOST_SS_SELECTOR, host_ss_selector),
893 FIELD(HOST_DS_SELECTOR, host_ds_selector),
894 FIELD(HOST_FS_SELECTOR, host_fs_selector),
895 FIELD(HOST_GS_SELECTOR, host_gs_selector),
896 FIELD(HOST_TR_SELECTOR, host_tr_selector),
897 FIELD64(IO_BITMAP_A, io_bitmap_a),
898 FIELD64(IO_BITMAP_B, io_bitmap_b),
899 FIELD64(MSR_BITMAP, msr_bitmap),
900 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
901 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
902 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
903 FIELD64(TSC_OFFSET, tsc_offset),
904 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
905 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800906 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300907 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800908 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
909 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
910 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
911 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800912 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300913 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
914 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
915 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
916 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
917 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
918 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
919 FIELD64(GUEST_PDPTR0, guest_pdptr0),
920 FIELD64(GUEST_PDPTR1, guest_pdptr1),
921 FIELD64(GUEST_PDPTR2, guest_pdptr2),
922 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100923 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300924 FIELD64(HOST_IA32_PAT, host_ia32_pat),
925 FIELD64(HOST_IA32_EFER, host_ia32_efer),
926 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
927 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
928 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
929 FIELD(EXCEPTION_BITMAP, exception_bitmap),
930 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
931 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
932 FIELD(CR3_TARGET_COUNT, cr3_target_count),
933 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
934 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
935 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
936 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
937 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
938 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
939 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
940 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
941 FIELD(TPR_THRESHOLD, tpr_threshold),
942 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
943 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
944 FIELD(VM_EXIT_REASON, vm_exit_reason),
945 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
946 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
947 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
948 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
949 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
950 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
951 FIELD(GUEST_ES_LIMIT, guest_es_limit),
952 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
953 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
954 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
955 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
956 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
957 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
958 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
959 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
960 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
961 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
962 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
963 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
964 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
965 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
966 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
967 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
968 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
969 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
970 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
971 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
972 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100973 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300974 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
975 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
976 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
977 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
978 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
979 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
980 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
981 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
982 FIELD(EXIT_QUALIFICATION, exit_qualification),
983 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
984 FIELD(GUEST_CR0, guest_cr0),
985 FIELD(GUEST_CR3, guest_cr3),
986 FIELD(GUEST_CR4, guest_cr4),
987 FIELD(GUEST_ES_BASE, guest_es_base),
988 FIELD(GUEST_CS_BASE, guest_cs_base),
989 FIELD(GUEST_SS_BASE, guest_ss_base),
990 FIELD(GUEST_DS_BASE, guest_ds_base),
991 FIELD(GUEST_FS_BASE, guest_fs_base),
992 FIELD(GUEST_GS_BASE, guest_gs_base),
993 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
994 FIELD(GUEST_TR_BASE, guest_tr_base),
995 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
996 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
997 FIELD(GUEST_DR7, guest_dr7),
998 FIELD(GUEST_RSP, guest_rsp),
999 FIELD(GUEST_RIP, guest_rip),
1000 FIELD(GUEST_RFLAGS, guest_rflags),
1001 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1002 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1003 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1004 FIELD(HOST_CR0, host_cr0),
1005 FIELD(HOST_CR3, host_cr3),
1006 FIELD(HOST_CR4, host_cr4),
1007 FIELD(HOST_FS_BASE, host_fs_base),
1008 FIELD(HOST_GS_BASE, host_gs_base),
1009 FIELD(HOST_TR_BASE, host_tr_base),
1010 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1011 FIELD(HOST_IDTR_BASE, host_idtr_base),
1012 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1013 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1014 FIELD(HOST_RSP, host_rsp),
1015 FIELD(HOST_RIP, host_rip),
1016};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001017
1018static inline short vmcs_field_to_offset(unsigned long field)
1019{
Dan Williamseb99bd62018-01-31 17:47:03 -08001020 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1021 unsigned short offset;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001022
Dan Williamseb99bd62018-01-31 17:47:03 -08001023 BUILD_BUG_ON(size > SHRT_MAX);
1024 if (field >= size)
Andrew Honig012df712018-01-10 10:12:03 -08001025 return -ENOENT;
1026
Dan Williamseb99bd62018-01-31 17:47:03 -08001027 field = array_index_nospec(field, size);
1028 offset = vmcs_field_to_offset_table[field];
1029 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001030 return -ENOENT;
Dan Williamseb99bd62018-01-31 17:47:03 -08001031 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001032}
1033
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001034static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1035{
David Matlack4f2777b2016-07-13 17:16:37 -07001036 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001037}
1038
1039static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
1040{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001041 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08001042 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001043 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +08001044
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001045 return page;
1046}
1047
1048static void nested_release_page(struct page *page)
1049{
1050 kvm_release_page_dirty(page);
1051}
1052
1053static void nested_release_page_clean(struct page *page)
1054{
1055 kvm_release_page_clean(page);
1056}
1057
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001058static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +08001059static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001060static void kvm_cpu_vmxon(u64 addr);
1061static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001062static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +02001063static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001064static void vmx_set_segment(struct kvm_vcpu *vcpu,
1065 struct kvm_segment *var, int seg);
1066static void vmx_get_segment(struct kvm_vcpu *vcpu,
1067 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001068static bool guest_state_valid(struct kvm_vcpu *vcpu);
1069static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +03001070static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +03001071static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +08001072static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzini6236b782018-01-16 16:51:18 +01001073static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj70131292018-02-01 22:59:43 +01001074static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1075 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001076
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1078static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001079/*
1080 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1081 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1082 */
1083static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +03001084static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085
Feng Wubf9f6ac2015-09-18 22:29:55 +08001086/*
1087 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1088 * can find which vCPU should be waken up.
1089 */
1090static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1091static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1092
Avi Kivity3e7c73e2009-02-24 21:46:19 +02001093static unsigned long *vmx_io_bitmap_a;
1094static unsigned long *vmx_io_bitmap_b;
Abel Gordon4607c2d2013-04-18 14:35:55 +03001095static unsigned long *vmx_vmread_bitmap;
1096static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +03001097
Avi Kivity110312c2010-12-21 12:54:20 +02001098static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001099static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001100
Sheng Yang2384d2b2008-01-17 15:14:33 +08001101static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1102static DEFINE_SPINLOCK(vmx_vpid_lock);
1103
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001104static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 int size;
1106 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001107 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001109 u32 pin_based_exec_ctrl;
1110 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001111 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001112 u32 vmexit_ctrl;
1113 u32 vmentry_ctrl;
1114} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115
Hannes Ederefff9e52008-11-28 17:02:06 +01001116static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001117 u32 ept;
1118 u32 vpid;
1119} vmx_capability;
1120
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121#define VMX_SEGMENT_FIELD(seg) \
1122 [VCPU_SREG_##seg] = { \
1123 .selector = GUEST_##seg##_SELECTOR, \
1124 .base = GUEST_##seg##_BASE, \
1125 .limit = GUEST_##seg##_LIMIT, \
1126 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1127 }
1128
Mathias Krause772e0312012-08-30 01:30:19 +02001129static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 unsigned selector;
1131 unsigned base;
1132 unsigned limit;
1133 unsigned ar_bytes;
1134} kvm_vmx_segment_fields[] = {
1135 VMX_SEGMENT_FIELD(CS),
1136 VMX_SEGMENT_FIELD(DS),
1137 VMX_SEGMENT_FIELD(ES),
1138 VMX_SEGMENT_FIELD(FS),
1139 VMX_SEGMENT_FIELD(GS),
1140 VMX_SEGMENT_FIELD(SS),
1141 VMX_SEGMENT_FIELD(TR),
1142 VMX_SEGMENT_FIELD(LDTR),
1143};
1144
Avi Kivity26bb0982009-09-07 11:14:12 +03001145static u64 host_efer;
1146
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001147static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1148
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001149/*
Brian Gerst8c065852010-07-17 09:03:26 -04001150 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001151 * away by decrementing the array size.
1152 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001154#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001155 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001157 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159
Jan Kiszka5bb16012016-02-09 20:14:21 +01001160static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161{
1162 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1163 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001164 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1165}
1166
Jan Kiszka6f054852016-02-09 20:15:18 +01001167static inline bool is_debug(u32 intr_info)
1168{
1169 return is_exception_n(intr_info, DB_VECTOR);
1170}
1171
1172static inline bool is_breakpoint(u32 intr_info)
1173{
1174 return is_exception_n(intr_info, BP_VECTOR);
1175}
1176
Jan Kiszka5bb16012016-02-09 20:14:21 +01001177static inline bool is_page_fault(u32 intr_info)
1178{
1179 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180}
1181
Gui Jianfeng31299942010-03-15 17:29:09 +08001182static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001183{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001184 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001185}
1186
Gui Jianfeng31299942010-03-15 17:29:09 +08001187static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001188{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001189 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001190}
1191
Gui Jianfeng31299942010-03-15 17:29:09 +08001192static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193{
1194 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1195 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1196}
1197
Gui Jianfeng31299942010-03-15 17:29:09 +08001198static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001199{
1200 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1201 INTR_INFO_VALID_MASK)) ==
1202 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1203}
1204
Linus Torvalds587da2b2018-03-20 12:16:59 -07001205/* Undocumented: icebp/int1 */
1206static inline bool is_icebp(u32 intr_info)
1207{
1208 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1209 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1210}
1211
Gui Jianfeng31299942010-03-15 17:29:09 +08001212static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001213{
Sheng Yang04547152009-04-01 15:52:31 +08001214 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001215}
1216
Gui Jianfeng31299942010-03-15 17:29:09 +08001217static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001218{
Sheng Yang04547152009-04-01 15:52:31 +08001219 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001220}
1221
Paolo Bonzini35754c92015-07-29 12:05:37 +02001222static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001223{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001224 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001225}
1226
Gui Jianfeng31299942010-03-15 17:29:09 +08001227static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001228{
Sheng Yang04547152009-04-01 15:52:31 +08001229 return vmcs_config.cpu_based_exec_ctrl &
1230 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001231}
1232
Avi Kivity774ead32007-12-26 13:57:04 +02001233static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001234{
Sheng Yang04547152009-04-01 15:52:31 +08001235 return vmcs_config.cpu_based_2nd_exec_ctrl &
1236 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1237}
1238
Yang Zhang8d146952013-01-25 10:18:50 +08001239static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1240{
1241 return vmcs_config.cpu_based_2nd_exec_ctrl &
1242 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1243}
1244
Yang Zhang83d4c282013-01-25 10:18:49 +08001245static inline bool cpu_has_vmx_apic_register_virt(void)
1246{
1247 return vmcs_config.cpu_based_2nd_exec_ctrl &
1248 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1249}
1250
Yang Zhangc7c9c562013-01-25 10:18:51 +08001251static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1252{
1253 return vmcs_config.cpu_based_2nd_exec_ctrl &
1254 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1255}
1256
Yunhong Jiang64672c92016-06-13 14:19:59 -07001257/*
1258 * Comment's format: document - errata name - stepping - processor name.
1259 * Refer from
1260 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1261 */
1262static u32 vmx_preemption_cpu_tfms[] = {
1263/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12640x000206E6,
1265/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1266/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1267/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12680x00020652,
1269/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12700x00020655,
1271/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1272/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1273/*
1274 * 320767.pdf - AAP86 - B1 -
1275 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1276 */
12770x000106E5,
1278/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
12790x000106A0,
1280/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
12810x000106A1,
1282/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
12830x000106A4,
1284 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1285 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1286 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
12870x000106A5,
1288};
1289
1290static inline bool cpu_has_broken_vmx_preemption_timer(void)
1291{
1292 u32 eax = cpuid_eax(0x00000001), i;
1293
1294 /* Clear the reserved bits */
1295 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001296 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001297 if (eax == vmx_preemption_cpu_tfms[i])
1298 return true;
1299
1300 return false;
1301}
1302
1303static inline bool cpu_has_vmx_preemption_timer(void)
1304{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001305 return vmcs_config.pin_based_exec_ctrl &
1306 PIN_BASED_VMX_PREEMPTION_TIMER;
1307}
1308
Yang Zhang01e439b2013-04-11 19:25:12 +08001309static inline bool cpu_has_vmx_posted_intr(void)
1310{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001311 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1312 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001313}
1314
1315static inline bool cpu_has_vmx_apicv(void)
1316{
1317 return cpu_has_vmx_apic_register_virt() &&
1318 cpu_has_vmx_virtual_intr_delivery() &&
1319 cpu_has_vmx_posted_intr();
1320}
1321
Sheng Yang04547152009-04-01 15:52:31 +08001322static inline bool cpu_has_vmx_flexpriority(void)
1323{
1324 return cpu_has_vmx_tpr_shadow() &&
1325 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001326}
1327
Marcelo Tosattie7997942009-06-11 12:07:40 -03001328static inline bool cpu_has_vmx_ept_execute_only(void)
1329{
Gui Jianfeng31299942010-03-15 17:29:09 +08001330 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001331}
1332
Marcelo Tosattie7997942009-06-11 12:07:40 -03001333static inline bool cpu_has_vmx_ept_2m_page(void)
1334{
Gui Jianfeng31299942010-03-15 17:29:09 +08001335 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001336}
1337
Sheng Yang878403b2010-01-05 19:02:29 +08001338static inline bool cpu_has_vmx_ept_1g_page(void)
1339{
Gui Jianfeng31299942010-03-15 17:29:09 +08001340 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001341}
1342
Sheng Yang4bc9b982010-06-02 14:05:24 +08001343static inline bool cpu_has_vmx_ept_4levels(void)
1344{
1345 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1346}
1347
Xudong Hao83c3a332012-05-28 19:33:35 +08001348static inline bool cpu_has_vmx_ept_ad_bits(void)
1349{
1350 return vmx_capability.ept & VMX_EPT_AD_BIT;
1351}
1352
Gui Jianfeng31299942010-03-15 17:29:09 +08001353static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001354{
Gui Jianfeng31299942010-03-15 17:29:09 +08001355 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001356}
1357
Gui Jianfeng31299942010-03-15 17:29:09 +08001358static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001359{
Gui Jianfeng31299942010-03-15 17:29:09 +08001360 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001361}
1362
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001363static inline bool cpu_has_vmx_invvpid_single(void)
1364{
1365 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1366}
1367
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001368static inline bool cpu_has_vmx_invvpid_global(void)
1369{
1370 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1371}
1372
Wanpeng Li2df19692017-03-23 05:30:08 -07001373static inline bool cpu_has_vmx_invvpid(void)
1374{
1375 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1376}
1377
Gui Jianfeng31299942010-03-15 17:29:09 +08001378static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001379{
Sheng Yang04547152009-04-01 15:52:31 +08001380 return vmcs_config.cpu_based_2nd_exec_ctrl &
1381 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001382}
1383
Gui Jianfeng31299942010-03-15 17:29:09 +08001384static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001385{
1386 return vmcs_config.cpu_based_2nd_exec_ctrl &
1387 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1388}
1389
Gui Jianfeng31299942010-03-15 17:29:09 +08001390static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001391{
1392 return vmcs_config.cpu_based_2nd_exec_ctrl &
1393 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1394}
1395
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001396static inline bool cpu_has_vmx_basic_inout(void)
1397{
1398 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1399}
1400
Paolo Bonzini35754c92015-07-29 12:05:37 +02001401static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001402{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001403 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001404}
1405
Gui Jianfeng31299942010-03-15 17:29:09 +08001406static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001407{
Sheng Yang04547152009-04-01 15:52:31 +08001408 return vmcs_config.cpu_based_2nd_exec_ctrl &
1409 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001410}
1411
Gui Jianfeng31299942010-03-15 17:29:09 +08001412static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001413{
1414 return vmcs_config.cpu_based_2nd_exec_ctrl &
1415 SECONDARY_EXEC_RDTSCP;
1416}
1417
Mao, Junjiead756a12012-07-02 01:18:48 +00001418static inline bool cpu_has_vmx_invpcid(void)
1419{
1420 return vmcs_config.cpu_based_2nd_exec_ctrl &
1421 SECONDARY_EXEC_ENABLE_INVPCID;
1422}
1423
Gui Jianfeng31299942010-03-15 17:29:09 +08001424static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001425{
1426 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1427}
1428
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001429static inline bool cpu_has_vmx_wbinvd_exit(void)
1430{
1431 return vmcs_config.cpu_based_2nd_exec_ctrl &
1432 SECONDARY_EXEC_WBINVD_EXITING;
1433}
1434
Abel Gordonabc4fc52013-04-18 14:35:25 +03001435static inline bool cpu_has_vmx_shadow_vmcs(void)
1436{
1437 u64 vmx_msr;
1438 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1439 /* check if the cpu supports writing r/o exit information fields */
1440 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1441 return false;
1442
1443 return vmcs_config.cpu_based_2nd_exec_ctrl &
1444 SECONDARY_EXEC_SHADOW_VMCS;
1445}
1446
Kai Huang843e4332015-01-28 10:54:28 +08001447static inline bool cpu_has_vmx_pml(void)
1448{
1449 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1450}
1451
Haozhong Zhang64903d62015-10-20 15:39:09 +08001452static inline bool cpu_has_vmx_tsc_scaling(void)
1453{
1454 return vmcs_config.cpu_based_2nd_exec_ctrl &
1455 SECONDARY_EXEC_TSC_SCALING;
1456}
1457
Sheng Yang04547152009-04-01 15:52:31 +08001458static inline bool report_flexpriority(void)
1459{
1460 return flexpriority_enabled;
1461}
1462
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001463static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1464{
1465 return vmcs12->cpu_based_vm_exec_control & bit;
1466}
1467
1468static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1469{
1470 return (vmcs12->cpu_based_vm_exec_control &
1471 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1472 (vmcs12->secondary_vm_exec_control & bit);
1473}
1474
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001475static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001476{
1477 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1478}
1479
Jan Kiszkaf4124502014-03-07 20:03:13 +01001480static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1481{
1482 return vmcs12->pin_based_vm_exec_control &
1483 PIN_BASED_VMX_PREEMPTION_TIMER;
1484}
1485
Nadav Har'El155a97a2013-08-05 11:07:16 +03001486static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1487{
1488 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1489}
1490
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001491static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1492{
1493 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1494 vmx_xsaves_supported();
1495}
1496
Wincy Vanf2b93282015-02-03 23:56:03 +08001497static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1498{
1499 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1500}
1501
Wanpeng Li5c614b32015-10-13 09:18:36 -07001502static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1503{
1504 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1505}
1506
Wincy Van82f0dd42015-02-03 23:57:18 +08001507static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1508{
1509 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1510}
1511
Wincy Van608406e2015-02-03 23:57:51 +08001512static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1513{
1514 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1515}
1516
Wincy Van705699a2015-02-03 23:58:17 +08001517static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1518{
1519 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1520}
1521
Jim Mattson3f618a02016-12-12 11:01:37 -08001522static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001523{
1524 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001525 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001526}
1527
Jan Kiszka533558b2014-01-04 18:47:20 +01001528static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1529 u32 exit_intr_info,
1530 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001531static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1532 struct vmcs12 *vmcs12,
1533 u32 reason, unsigned long qualification);
1534
Rusty Russell8b9cf982007-07-30 16:31:43 +10001535static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001536{
1537 int i;
1538
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001539 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001540 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001541 return i;
1542 return -1;
1543}
1544
Sheng Yang2384d2b2008-01-17 15:14:33 +08001545static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1546{
1547 struct {
1548 u64 vpid : 16;
1549 u64 rsvd : 48;
1550 u64 gva;
1551 } operand = { vpid, 0, gva };
1552
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001553 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001554 /* CF==1 or ZF==1 --> rc = -1 */
1555 "; ja 1f ; ud2 ; 1:"
1556 : : "a"(&operand), "c"(ext) : "cc", "memory");
1557}
1558
Sheng Yang14394422008-04-28 12:24:45 +08001559static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1560{
1561 struct {
1562 u64 eptp, gpa;
1563 } operand = {eptp, gpa};
1564
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001565 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001566 /* CF==1 or ZF==1 --> rc = -1 */
1567 "; ja 1f ; ud2 ; 1:\n"
1568 : : "a" (&operand), "c" (ext) : "cc", "memory");
1569}
1570
Avi Kivity26bb0982009-09-07 11:14:12 +03001571static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001572{
1573 int i;
1574
Rusty Russell8b9cf982007-07-30 16:31:43 +10001575 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001576 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001577 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001578 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001579}
1580
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581static void vmcs_clear(struct vmcs *vmcs)
1582{
1583 u64 phys_addr = __pa(vmcs);
1584 u8 error;
1585
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001586 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001587 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 : "cc", "memory");
1589 if (error)
1590 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1591 vmcs, phys_addr);
1592}
1593
Nadav Har'Eld462b812011-05-24 15:26:10 +03001594static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1595{
1596 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001597 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1598 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001599 loaded_vmcs->cpu = -1;
1600 loaded_vmcs->launched = 0;
1601}
1602
Dongxiao Xu7725b892010-05-11 18:29:38 +08001603static void vmcs_load(struct vmcs *vmcs)
1604{
1605 u64 phys_addr = __pa(vmcs);
1606 u8 error;
1607
1608 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001609 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001610 : "cc", "memory");
1611 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001612 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001613 vmcs, phys_addr);
1614}
1615
Dave Young2965faa2015-09-09 15:38:55 -07001616#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001617/*
1618 * This bitmap is used to indicate whether the vmclear
1619 * operation is enabled on all cpus. All disabled by
1620 * default.
1621 */
1622static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1623
1624static inline void crash_enable_local_vmclear(int cpu)
1625{
1626 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1627}
1628
1629static inline void crash_disable_local_vmclear(int cpu)
1630{
1631 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1632}
1633
1634static inline int crash_local_vmclear_enabled(int cpu)
1635{
1636 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1637}
1638
1639static void crash_vmclear_local_loaded_vmcss(void)
1640{
1641 int cpu = raw_smp_processor_id();
1642 struct loaded_vmcs *v;
1643
1644 if (!crash_local_vmclear_enabled(cpu))
1645 return;
1646
1647 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1648 loaded_vmcss_on_cpu_link)
1649 vmcs_clear(v->vmcs);
1650}
1651#else
1652static inline void crash_enable_local_vmclear(int cpu) { }
1653static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001654#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001655
Nadav Har'Eld462b812011-05-24 15:26:10 +03001656static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001658 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001659 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660
Nadav Har'Eld462b812011-05-24 15:26:10 +03001661 if (loaded_vmcs->cpu != cpu)
1662 return; /* vcpu migration can race with cpu offline */
1663 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001665 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001666 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001667
1668 /*
1669 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1670 * is before setting loaded_vmcs->vcpu to -1 which is done in
1671 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1672 * then adds the vmcs into percpu list before it is deleted.
1673 */
1674 smp_wmb();
1675
Nadav Har'Eld462b812011-05-24 15:26:10 +03001676 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001677 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678}
1679
Nadav Har'Eld462b812011-05-24 15:26:10 +03001680static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001681{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001682 int cpu = loaded_vmcs->cpu;
1683
1684 if (cpu != -1)
1685 smp_call_function_single(cpu,
1686 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001687}
1688
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001689static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001690{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001691 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001692 return;
1693
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001694 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001695 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001696}
1697
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001698static inline void vpid_sync_vcpu_global(void)
1699{
1700 if (cpu_has_vmx_invvpid_global())
1701 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1702}
1703
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001704static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001705{
1706 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001707 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001708 else
1709 vpid_sync_vcpu_global();
1710}
1711
Sheng Yang14394422008-04-28 12:24:45 +08001712static inline void ept_sync_global(void)
1713{
1714 if (cpu_has_vmx_invept_global())
1715 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1716}
1717
1718static inline void ept_sync_context(u64 eptp)
1719{
Avi Kivity089d0342009-03-23 18:26:32 +02001720 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001721 if (cpu_has_vmx_invept_context())
1722 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1723 else
1724 ept_sync_global();
1725 }
1726}
1727
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001728static __always_inline void vmcs_check16(unsigned long field)
1729{
1730 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1731 "16-bit accessor invalid for 64-bit field");
1732 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1733 "16-bit accessor invalid for 64-bit high field");
1734 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1735 "16-bit accessor invalid for 32-bit high field");
1736 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1737 "16-bit accessor invalid for natural width field");
1738}
1739
1740static __always_inline void vmcs_check32(unsigned long field)
1741{
1742 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1743 "32-bit accessor invalid for 16-bit field");
1744 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1745 "32-bit accessor invalid for natural width field");
1746}
1747
1748static __always_inline void vmcs_check64(unsigned long field)
1749{
1750 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1751 "64-bit accessor invalid for 16-bit field");
1752 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1753 "64-bit accessor invalid for 64-bit high field");
1754 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1755 "64-bit accessor invalid for 32-bit field");
1756 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1757 "64-bit accessor invalid for natural width field");
1758}
1759
1760static __always_inline void vmcs_checkl(unsigned long field)
1761{
1762 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1763 "Natural width accessor invalid for 16-bit field");
1764 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1765 "Natural width accessor invalid for 64-bit field");
1766 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1767 "Natural width accessor invalid for 64-bit high field");
1768 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1769 "Natural width accessor invalid for 32-bit field");
1770}
1771
1772static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773{
Avi Kivity5e520e62011-05-15 10:13:12 -04001774 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775
Avi Kivity5e520e62011-05-15 10:13:12 -04001776 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1777 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 return value;
1779}
1780
Avi Kivity96304212011-05-15 10:13:13 -04001781static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001783 vmcs_check16(field);
1784 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785}
1786
Avi Kivity96304212011-05-15 10:13:13 -04001787static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001789 vmcs_check32(field);
1790 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791}
1792
Avi Kivity96304212011-05-15 10:13:13 -04001793static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001795 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001796#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001797 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001799 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800#endif
1801}
1802
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001803static __always_inline unsigned long vmcs_readl(unsigned long field)
1804{
1805 vmcs_checkl(field);
1806 return __vmcs_readl(field);
1807}
1808
Avi Kivitye52de1b2007-01-05 16:36:56 -08001809static noinline void vmwrite_error(unsigned long field, unsigned long value)
1810{
1811 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1812 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1813 dump_stack();
1814}
1815
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001816static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817{
1818 u8 error;
1819
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001820 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001821 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001822 if (unlikely(error))
1823 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824}
1825
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001826static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001828 vmcs_check16(field);
1829 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830}
1831
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001832static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001834 vmcs_check32(field);
1835 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836}
1837
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001838static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001840 vmcs_check64(field);
1841 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001842#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001844 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845#endif
1846}
1847
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001848static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001849{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001850 vmcs_checkl(field);
1851 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001852}
1853
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001854static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001855{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001856 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1857 "vmcs_clear_bits does not support 64-bit fields");
1858 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1859}
1860
1861static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1862{
1863 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1864 "vmcs_set_bits does not support 64-bit fields");
1865 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001866}
1867
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001868static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1869{
1870 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1871}
1872
Gleb Natapov2961e8762013-11-25 15:37:13 +02001873static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1874{
1875 vmcs_write32(VM_ENTRY_CONTROLS, val);
1876 vmx->vm_entry_controls_shadow = val;
1877}
1878
1879static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1880{
1881 if (vmx->vm_entry_controls_shadow != val)
1882 vm_entry_controls_init(vmx, val);
1883}
1884
1885static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1886{
1887 return vmx->vm_entry_controls_shadow;
1888}
1889
1890
1891static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1892{
1893 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1894}
1895
1896static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1897{
1898 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1899}
1900
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001901static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1902{
1903 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1904}
1905
Gleb Natapov2961e8762013-11-25 15:37:13 +02001906static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1907{
1908 vmcs_write32(VM_EXIT_CONTROLS, val);
1909 vmx->vm_exit_controls_shadow = val;
1910}
1911
1912static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1913{
1914 if (vmx->vm_exit_controls_shadow != val)
1915 vm_exit_controls_init(vmx, val);
1916}
1917
1918static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1919{
1920 return vmx->vm_exit_controls_shadow;
1921}
1922
1923
1924static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1925{
1926 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1927}
1928
1929static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1930{
1931 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1932}
1933
Avi Kivity2fb92db2011-04-27 19:42:18 +03001934static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1935{
1936 vmx->segment_cache.bitmask = 0;
1937}
1938
1939static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1940 unsigned field)
1941{
1942 bool ret;
1943 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1944
1945 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1946 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1947 vmx->segment_cache.bitmask = 0;
1948 }
1949 ret = vmx->segment_cache.bitmask & mask;
1950 vmx->segment_cache.bitmask |= mask;
1951 return ret;
1952}
1953
1954static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1955{
1956 u16 *p = &vmx->segment_cache.seg[seg].selector;
1957
1958 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1959 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1960 return *p;
1961}
1962
1963static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1964{
1965 ulong *p = &vmx->segment_cache.seg[seg].base;
1966
1967 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1968 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1969 return *p;
1970}
1971
1972static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1973{
1974 u32 *p = &vmx->segment_cache.seg[seg].limit;
1975
1976 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1977 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1978 return *p;
1979}
1980
1981static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1982{
1983 u32 *p = &vmx->segment_cache.seg[seg].ar;
1984
1985 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1986 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1987 return *p;
1988}
1989
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001990static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1991{
1992 u32 eb;
1993
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001994 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001995 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001996 if ((vcpu->guest_debug &
1997 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1998 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1999 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002000 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002001 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002002 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002003 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02002004 if (vcpu->fpu_active)
2005 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002006
2007 /* When we are running a nested L2 guest and L1 specified for it a
2008 * certain exception bitmap, we must trap the same exceptions and pass
2009 * them to L1. When running L2, we will only handle the exceptions
2010 * specified above if L1 did not want them.
2011 */
2012 if (is_guest_mode(vcpu))
2013 eb |= get_vmcs12(vcpu)->exception_bitmap;
2014
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002015 vmcs_write32(EXCEPTION_BITMAP, eb);
2016}
2017
Ashok Raj70131292018-02-01 22:59:43 +01002018/*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01002019 * Check if MSR is intercepted for currently loaded MSR bitmap.
2020 */
2021static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2022{
2023 unsigned long *msr_bitmap;
2024 int f = sizeof(unsigned long);
2025
2026 if (!cpu_has_vmx_msr_bitmap())
2027 return true;
2028
2029 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2030
2031 if (msr <= 0x1fff) {
2032 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2033 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2034 msr &= 0x1fff;
2035 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2036 }
2037
2038 return true;
2039}
2040
2041/*
Ashok Raj70131292018-02-01 22:59:43 +01002042 * Check if MSR is intercepted for L01 MSR bitmap.
2043 */
2044static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2045{
2046 unsigned long *msr_bitmap;
2047 int f = sizeof(unsigned long);
2048
2049 if (!cpu_has_vmx_msr_bitmap())
2050 return true;
2051
2052 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2053
2054 if (msr <= 0x1fff) {
2055 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2056 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2057 msr &= 0x1fff;
2058 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2059 }
2060
2061 return true;
2062}
2063
Gleb Natapov2961e8762013-11-25 15:37:13 +02002064static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2065 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002066{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002067 vm_entry_controls_clearbit(vmx, entry);
2068 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002069}
2070
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002071static int find_msr(struct vmx_msrs *m, unsigned int msr)
2072{
2073 unsigned int i;
2074
2075 for (i = 0; i < m->nr; ++i) {
2076 if (m->val[i].index == msr)
2077 return i;
2078 }
2079 return -ENOENT;
2080}
2081
Avi Kivity61d2ef22010-04-28 16:40:38 +03002082static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2083{
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002084 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002085 struct msr_autoload *m = &vmx->msr_autoload;
2086
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002087 switch (msr) {
2088 case MSR_EFER:
2089 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002090 clear_atomic_switch_msr_special(vmx,
2091 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002092 VM_EXIT_LOAD_IA32_EFER);
2093 return;
2094 }
2095 break;
2096 case MSR_CORE_PERF_GLOBAL_CTRL:
2097 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002098 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002099 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2100 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2101 return;
2102 }
2103 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002104 }
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002105 i = find_msr(&m->guest, msr);
2106 if (i < 0)
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002107 goto skip_guest;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002108 --m->guest.nr;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002109 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002110 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002111
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002112skip_guest:
2113 i = find_msr(&m->host, msr);
2114 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002115 return;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002116
2117 --m->host.nr;
2118 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002119 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002120}
2121
Gleb Natapov2961e8762013-11-25 15:37:13 +02002122static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2123 unsigned long entry, unsigned long exit,
2124 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2125 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002126{
2127 vmcs_write64(guest_val_vmcs, guest_val);
2128 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002129 vm_entry_controls_setbit(vmx, entry);
2130 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002131}
2132
Avi Kivity61d2ef22010-04-28 16:40:38 +03002133static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002134 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002135{
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002136 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002137 struct msr_autoload *m = &vmx->msr_autoload;
2138
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002139 switch (msr) {
2140 case MSR_EFER:
2141 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002142 add_atomic_switch_msr_special(vmx,
2143 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002144 VM_EXIT_LOAD_IA32_EFER,
2145 GUEST_IA32_EFER,
2146 HOST_IA32_EFER,
2147 guest_val, host_val);
2148 return;
2149 }
2150 break;
2151 case MSR_CORE_PERF_GLOBAL_CTRL:
2152 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002153 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002154 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2155 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2156 GUEST_IA32_PERF_GLOBAL_CTRL,
2157 HOST_IA32_PERF_GLOBAL_CTRL,
2158 guest_val, host_val);
2159 return;
2160 }
2161 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002162 case MSR_IA32_PEBS_ENABLE:
2163 /* PEBS needs a quiescent period after being disabled (to write
2164 * a record). Disabling PEBS through VMX MSR swapping doesn't
2165 * provide that period, so a CPU could write host's record into
2166 * guest's memory.
2167 */
2168 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002169 }
2170
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002171 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002172 if (!entry_only)
2173 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002174
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002175 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002176 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002177 "Can't add msr %x\n", msr);
2178 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002179 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002180 if (i < 0) {
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002181 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002182 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002183 }
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002184 m->guest.val[i].index = msr;
2185 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002186
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002187 if (entry_only)
2188 return;
2189
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002190 if (j < 0) {
2191 j = m->host.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002192 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002193 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002194 m->host.val[j].index = msr;
2195 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002196}
2197
Avi Kivity33ed6322007-05-02 16:54:03 +03002198static void reload_tss(void)
2199{
Avi Kivity33ed6322007-05-02 16:54:03 +03002200 /*
2201 * VT restores TR but not its size. Useless.
2202 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002203 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002204 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002205
Avi Kivityd3591922010-07-26 18:32:39 +03002206 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002207 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2208 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002209}
2210
Avi Kivity92c0d902009-10-29 11:00:16 +02002211static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002212{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002213 u64 guest_efer = vmx->vcpu.arch.efer;
2214 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002215
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002216 if (!enable_ept) {
2217 /*
2218 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2219 * host CPUID is more efficient than testing guest CPUID
2220 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2221 */
2222 if (boot_cpu_has(X86_FEATURE_SMEP))
2223 guest_efer |= EFER_NX;
2224 else if (!(guest_efer & EFER_NX))
2225 ignore_bits |= EFER_NX;
2226 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002227
Avi Kivity51c6cf62007-08-29 03:48:05 +03002228 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002229 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002230 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002231 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002232#ifdef CONFIG_X86_64
2233 ignore_bits |= EFER_LMA | EFER_LME;
2234 /* SCE is meaningful only in long mode on Intel */
2235 if (guest_efer & EFER_LMA)
2236 ignore_bits &= ~(u64)EFER_SCE;
2237#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002238
2239 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002240
2241 /*
2242 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2243 * On CPUs that support "load IA32_EFER", always switch EFER
2244 * atomically, since it's faster than switching it manually.
2245 */
2246 if (cpu_has_load_ia32_efer ||
2247 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002248 if (!(guest_efer & EFER_LMA))
2249 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002250 if (guest_efer != host_efer)
2251 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002252 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002253 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002254 } else {
2255 guest_efer &= ~ignore_bits;
2256 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002257
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002258 vmx->guest_msrs[efer_offset].data = guest_efer;
2259 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2260
2261 return true;
2262 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002263}
2264
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002265static unsigned long segment_base(u16 selector)
2266{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002267 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002268 struct desc_struct *d;
2269 unsigned long table_base;
2270 unsigned long v;
2271
2272 if (!(selector & ~3))
2273 return 0;
2274
Avi Kivityd3591922010-07-26 18:32:39 +03002275 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002276
2277 if (selector & 4) { /* from ldt */
2278 u16 ldt_selector = kvm_read_ldt();
2279
2280 if (!(ldt_selector & ~3))
2281 return 0;
2282
2283 table_base = segment_base(ldt_selector);
2284 }
2285 d = (struct desc_struct *)(table_base + (selector & ~7));
2286 v = get_desc_base(d);
2287#ifdef CONFIG_X86_64
2288 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2289 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2290#endif
2291 return v;
2292}
2293
2294static inline unsigned long kvm_read_tr_base(void)
2295{
2296 u16 tr;
2297 asm("str %0" : "=g"(tr));
2298 return segment_base(tr);
2299}
2300
Avi Kivity04d2cc72007-09-10 18:10:54 +03002301static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002302{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002303 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002304 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002305
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002306 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002307 return;
2308
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002309 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002310 /*
2311 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2312 * allow segment selectors with cpl > 0 or ti == 1.
2313 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002314 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002315 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002316 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002317 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002318 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002319 vmx->host_state.fs_reload_needed = 0;
2320 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002321 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002322 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002323 }
Avi Kivity9581d442010-10-19 16:46:55 +02002324 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002325 if (!(vmx->host_state.gs_sel & 7))
2326 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002327 else {
2328 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002329 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002330 }
2331
2332#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002333 savesegment(ds, vmx->host_state.ds_sel);
2334 savesegment(es, vmx->host_state.es_sel);
2335#endif
2336
2337#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002338 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2339 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2340#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002341 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2342 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002343#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002344
2345#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002346 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2347 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002348 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002349#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002350 if (boot_cpu_has(X86_FEATURE_MPX))
2351 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002352 for (i = 0; i < vmx->save_nmsrs; ++i)
2353 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002354 vmx->guest_msrs[i].data,
2355 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002356}
2357
Avi Kivitya9b21b62008-06-24 11:48:49 +03002358static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002359{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002360 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002361 return;
2362
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002363 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002364 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002365#ifdef CONFIG_X86_64
2366 if (is_long_mode(&vmx->vcpu))
2367 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2368#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002369 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002370 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002371#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002372 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002373#else
2374 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002375#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002376 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002377 if (vmx->host_state.fs_reload_needed)
2378 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002379#ifdef CONFIG_X86_64
2380 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2381 loadsegment(ds, vmx->host_state.ds_sel);
2382 loadsegment(es, vmx->host_state.es_sel);
2383 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002384#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002385 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002386#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002387 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002388#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002389 if (vmx->host_state.msr_host_bndcfgs)
2390 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002391 /*
2392 * If the FPU is not active (through the host task or
2393 * the guest vcpu), then restore the cr0.TS bit.
2394 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002395 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002396 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002397 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002398}
2399
Avi Kivitya9b21b62008-06-24 11:48:49 +03002400static void vmx_load_host_state(struct vcpu_vmx *vmx)
2401{
2402 preempt_disable();
2403 __vmx_load_host_state(vmx);
2404 preempt_enable();
2405}
2406
Feng Wu28b835d2015-09-18 22:29:54 +08002407static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2408{
2409 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2410 struct pi_desc old, new;
2411 unsigned int dest;
2412
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002413 /*
2414 * In case of hot-plug or hot-unplug, we may have to undo
2415 * vmx_vcpu_pi_put even if there is no assigned device. And we
2416 * always keep PI.NDST up to date for simplicity: it makes the
2417 * code easier, and CPU migration is not a fast path.
2418 */
2419 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002420 return;
2421
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002422 /*
2423 * First handle the simple case where no cmpxchg is necessary; just
2424 * allow posting non-urgent interrupts.
2425 *
2426 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2427 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2428 * expects the VCPU to be on the blocked_vcpu_list that matches
2429 * PI.NDST.
2430 */
2431 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2432 vcpu->cpu == cpu) {
2433 pi_clear_sn(pi_desc);
2434 return;
2435 }
2436
2437 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002438 do {
2439 old.control = new.control = pi_desc->control;
2440
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002441 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002442
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002443 if (x2apic_enabled())
2444 new.ndst = dest;
2445 else
2446 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002447
Feng Wu28b835d2015-09-18 22:29:54 +08002448 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002449 } while (cmpxchg64(&pi_desc->control, old.control,
2450 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002451}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002452
Peter Feinerc95ba922016-08-17 09:36:47 -07002453static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2454{
2455 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2456 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2457}
2458
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459/*
2460 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2461 * vcpu mutex is already taken.
2462 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002463static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002465 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002466 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002467 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002469 if (!vmm_exclusive)
2470 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002471 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002472 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002474 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002475 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002476 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002477
2478 /*
2479 * Read loaded_vmcs->cpu should be before fetching
2480 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2481 * See the comments in __loaded_vmcs_clear().
2482 */
2483 smp_rmb();
2484
Nadav Har'Eld462b812011-05-24 15:26:10 +03002485 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2486 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002487 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002488 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002489 }
2490
2491 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2492 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2493 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002494 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002495 }
2496
2497 if (!already_loaded) {
2498 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2499 unsigned long sysenter_esp;
2500
2501 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002502
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503 /*
2504 * Linux uses per-cpu TSS and GDT, so set these when switching
2505 * processors.
2506 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002507 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002508 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509
2510 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2511 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002512
Nadav Har'Eld462b812011-05-24 15:26:10 +03002513 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514 }
Feng Wu28b835d2015-09-18 22:29:54 +08002515
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002516 /* Setup TSC multiplier */
2517 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002518 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2519 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002520
Feng Wu28b835d2015-09-18 22:29:54 +08002521 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002522 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002523}
2524
2525static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2526{
2527 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2528
2529 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002530 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2531 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002532 return;
2533
2534 /* Set SN when the vCPU is preempted */
2535 if (vcpu->preempted)
2536 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537}
2538
2539static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2540{
Feng Wu28b835d2015-09-18 22:29:54 +08002541 vmx_vcpu_pi_put(vcpu);
2542
Avi Kivitya9b21b62008-06-24 11:48:49 +03002543 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002544 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002545 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2546 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002547 kvm_cpu_vmxoff();
2548 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549}
2550
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002551static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2552{
Avi Kivity81231c62010-01-24 16:26:40 +02002553 ulong cr0;
2554
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002555 if (vcpu->fpu_active)
2556 return;
2557 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002558 cr0 = vmcs_readl(GUEST_CR0);
2559 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2560 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2561 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002562 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002563 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002564 if (is_guest_mode(vcpu))
2565 vcpu->arch.cr0_guest_owned_bits &=
2566 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002567 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002568}
2569
Avi Kivityedcafe32009-12-30 18:07:40 +02002570static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2571
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002572/*
2573 * Return the cr0 value that a nested guest would read. This is a combination
2574 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2575 * its hypervisor (cr0_read_shadow).
2576 */
2577static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2578{
2579 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2580 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2581}
2582static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2583{
2584 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2585 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2586}
2587
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002588static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2589{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002590 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2591 * set this *before* calling this function.
2592 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002593 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002594 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002595 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002596 vcpu->arch.cr0_guest_owned_bits = 0;
2597 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002598 if (is_guest_mode(vcpu)) {
2599 /*
2600 * L1's specified read shadow might not contain the TS bit,
2601 * so now that we turned on shadowing of this bit, we need to
2602 * set this bit of the shadow. Like in nested_vmx_run we need
2603 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2604 * up-to-date here because we just decached cr0.TS (and we'll
2605 * only update vmcs12->guest_cr0 on nested exit).
2606 */
2607 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2608 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2609 (vcpu->arch.cr0 & X86_CR0_TS);
2610 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2611 } else
2612 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002613}
2614
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2616{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002617 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002618
Avi Kivity6de12732011-03-07 12:51:22 +02002619 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2620 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2621 rflags = vmcs_readl(GUEST_RFLAGS);
2622 if (to_vmx(vcpu)->rmode.vm86_active) {
2623 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2624 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2625 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2626 }
2627 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002628 }
Avi Kivity6de12732011-03-07 12:51:22 +02002629 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630}
2631
2632static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2633{
Avi Kivity6de12732011-03-07 12:51:22 +02002634 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2635 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002636 if (to_vmx(vcpu)->rmode.vm86_active) {
2637 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002638 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002639 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 vmcs_writel(GUEST_RFLAGS, rflags);
2641}
2642
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002643static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2644{
2645 return to_vmx(vcpu)->guest_pkru;
2646}
2647
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002648static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002649{
2650 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2651 int ret = 0;
2652
2653 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002654 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002655 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002656 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002657
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002658 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002659}
2660
2661static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2662{
2663 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2664 u32 interruptibility = interruptibility_old;
2665
2666 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2667
Jan Kiszka48005f62010-02-19 19:38:07 +01002668 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002669 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002670 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002671 interruptibility |= GUEST_INTR_STATE_STI;
2672
2673 if ((interruptibility != interruptibility_old))
2674 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2675}
2676
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2678{
2679 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002681 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002683 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684
Glauber Costa2809f5d2009-05-12 16:21:05 -04002685 /* skipping an emulated instruction also counts */
2686 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687}
2688
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002689/*
2690 * KVM wants to inject page-faults which it got to the guest. This function
2691 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002692 */
Gleb Natapove011c662013-09-25 12:51:35 +03002693static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002694{
2695 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2696
Gleb Natapove011c662013-09-25 12:51:35 +03002697 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002698 return 0;
2699
Wanpeng Lia29fd272017-06-05 05:19:09 -07002700 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002701 vmcs_read32(VM_EXIT_INTR_INFO),
2702 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002703 return 1;
2704}
2705
Avi Kivity298101d2007-11-25 13:41:11 +02002706static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002707 bool has_error_code, u32 error_code,
2708 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002709{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002710 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002711 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002712
Gleb Natapove011c662013-09-25 12:51:35 +03002713 if (!reinject && is_guest_mode(vcpu) &&
2714 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002715 return;
2716
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002717 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002718 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002719 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2720 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002721
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002722 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002723 int inc_eip = 0;
2724 if (kvm_exception_is_soft(nr))
2725 inc_eip = vcpu->arch.event_exit_inst_len;
2726 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002727 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002728 return;
2729 }
2730
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002731 WARN_ON_ONCE(vmx->emulation_required);
2732
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002733 if (kvm_exception_is_soft(nr)) {
2734 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2735 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002736 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2737 } else
2738 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2739
2740 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002741}
2742
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002743static bool vmx_rdtscp_supported(void)
2744{
2745 return cpu_has_vmx_rdtscp();
2746}
2747
Mao, Junjiead756a12012-07-02 01:18:48 +00002748static bool vmx_invpcid_supported(void)
2749{
2750 return cpu_has_vmx_invpcid() && enable_ept;
2751}
2752
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753/*
Eddie Donga75beee2007-05-17 18:55:15 +03002754 * Swap MSR entry in host/guest MSR entry array.
2755 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002756static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002757{
Avi Kivity26bb0982009-09-07 11:14:12 +03002758 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002759
2760 tmp = vmx->guest_msrs[to];
2761 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2762 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002763}
2764
2765/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002766 * Set up the vmcs to automatically save and restore system
2767 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2768 * mode, as fiddling with msrs is very expensive.
2769 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002770static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002771{
Avi Kivity26bb0982009-09-07 11:14:12 +03002772 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002773
Eddie Donga75beee2007-05-17 18:55:15 +03002774 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002775#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002776 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002777 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002778 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002779 move_msr_up(vmx, index, save_nmsrs++);
2780 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002781 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002782 move_msr_up(vmx, index, save_nmsrs++);
2783 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002784 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002785 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002786 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002787 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002788 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002789 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002790 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002791 * if efer.sce is enabled.
2792 */
Brian Gerst8c065852010-07-17 09:03:26 -04002793 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002794 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002795 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002796 }
Eddie Donga75beee2007-05-17 18:55:15 +03002797#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002798 index = __find_msr_index(vmx, MSR_EFER);
2799 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002800 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002801
Avi Kivity26bb0982009-09-07 11:14:12 +03002802 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002803
Yang Zhang8d146952013-01-25 10:18:50 +08002804 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002805 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002806}
2807
2808/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002810 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2811 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002813static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814{
2815 u64 host_tsc, tsc_offset;
2816
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002817 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002819 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820}
2821
2822/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002823 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002825static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002827 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002828 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002829 * We're here if L1 chose not to trap WRMSR to TSC. According
2830 * to the spec, this should set L1's TSC; The offset that L1
2831 * set for L2 remains unchanged, and still needs to be added
2832 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002833 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002834 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002835 /* recalculate vmcs02.TSC_OFFSET: */
2836 vmcs12 = get_vmcs12(vcpu);
2837 vmcs_write64(TSC_OFFSET, offset +
2838 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2839 vmcs12->tsc_offset : 0));
2840 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002841 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2842 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002843 vmcs_write64(TSC_OFFSET, offset);
2844 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845}
2846
Nadav Har'El801d3422011-05-25 23:02:23 +03002847static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2848{
2849 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2850 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2851}
2852
2853/*
2854 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2855 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2856 * all guests if the "nested" module option is off, and can also be disabled
2857 * for a single guest by disabling its VMX cpuid bit.
2858 */
2859static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2860{
2861 return nested && guest_cpuid_has_vmx(vcpu);
2862}
2863
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002865 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2866 * returned for the various VMX controls MSRs when nested VMX is enabled.
2867 * The same values should also be used to verify that vmcs12 control fields are
2868 * valid during nested entry from L1 to L2.
2869 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2870 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2871 * bit in the high half is on if the corresponding bit in the control field
2872 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002873 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002874static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002875{
2876 /*
2877 * Note that as a general rule, the high half of the MSRs (bits in
2878 * the control fields which may be 1) should be initialized by the
2879 * intersection of the underlying hardware's MSR (i.e., features which
2880 * can be supported) and the list of features we want to expose -
2881 * because they are known to be properly supported in our code.
2882 * Also, usually, the low half of the MSRs (bits which must be 1) can
2883 * be set to 0, meaning that L1 may turn off any of these bits. The
2884 * reason is that if one of these bits is necessary, it will appear
2885 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2886 * fields of vmcs01 and vmcs02, will turn these bits off - and
2887 * nested_vmx_exit_handled() will not pass related exits to L1.
2888 * These rules have exceptions below.
2889 */
2890
2891 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002892 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002893 vmx->nested.nested_vmx_pinbased_ctls_low,
2894 vmx->nested.nested_vmx_pinbased_ctls_high);
2895 vmx->nested.nested_vmx_pinbased_ctls_low |=
2896 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2897 vmx->nested.nested_vmx_pinbased_ctls_high &=
2898 PIN_BASED_EXT_INTR_MASK |
2899 PIN_BASED_NMI_EXITING |
2900 PIN_BASED_VIRTUAL_NMIS;
2901 vmx->nested.nested_vmx_pinbased_ctls_high |=
2902 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002903 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002904 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002905 vmx->nested.nested_vmx_pinbased_ctls_high |=
2906 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002907
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002908 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002909 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002910 vmx->nested.nested_vmx_exit_ctls_low,
2911 vmx->nested.nested_vmx_exit_ctls_high);
2912 vmx->nested.nested_vmx_exit_ctls_low =
2913 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002914
Wincy Vanb9c237b2015-02-03 23:56:30 +08002915 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002916#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002917 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002918#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002919 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002920 vmx->nested.nested_vmx_exit_ctls_high |=
2921 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002922 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002923 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2924
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002925 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002926 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002927
Jan Kiszka2996fca2014-06-16 13:59:43 +02002928 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002929 vmx->nested.nested_vmx_true_exit_ctls_low =
2930 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002931 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2932
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002933 /* entry controls */
2934 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002935 vmx->nested.nested_vmx_entry_ctls_low,
2936 vmx->nested.nested_vmx_entry_ctls_high);
2937 vmx->nested.nested_vmx_entry_ctls_low =
2938 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2939 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002940#ifdef CONFIG_X86_64
2941 VM_ENTRY_IA32E_MODE |
2942#endif
2943 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002944 vmx->nested.nested_vmx_entry_ctls_high |=
2945 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002946 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002947 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002948
Jan Kiszka2996fca2014-06-16 13:59:43 +02002949 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002950 vmx->nested.nested_vmx_true_entry_ctls_low =
2951 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002952 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2953
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002954 /* cpu-based controls */
2955 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002956 vmx->nested.nested_vmx_procbased_ctls_low,
2957 vmx->nested.nested_vmx_procbased_ctls_high);
2958 vmx->nested.nested_vmx_procbased_ctls_low =
2959 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2960 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002961 CPU_BASED_VIRTUAL_INTR_PENDING |
2962 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002963 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2964 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2965 CPU_BASED_CR3_STORE_EXITING |
2966#ifdef CONFIG_X86_64
2967 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2968#endif
2969 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002970 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2971 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2972 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2973 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002974 /*
2975 * We can allow some features even when not supported by the
2976 * hardware. For example, L1 can specify an MSR bitmap - and we
2977 * can use it to avoid exits to L1 - even when L0 runs L2
2978 * without MSR bitmaps.
2979 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002980 vmx->nested.nested_vmx_procbased_ctls_high |=
2981 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002982 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002983
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002984 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002985 vmx->nested.nested_vmx_true_procbased_ctls_low =
2986 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002987 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2988
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002989 /* secondary cpu-based controls */
2990 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002991 vmx->nested.nested_vmx_secondary_ctls_low,
2992 vmx->nested.nested_vmx_secondary_ctls_high);
2993 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2994 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002995 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002996 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002997 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002998 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002999 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003000 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08003001 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07003002 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003003
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003004 if (enable_ept) {
3005 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003006 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003007 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003008 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01003009 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
3010 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003011 if (cpu_has_vmx_ept_execute_only())
3012 vmx->nested.nested_vmx_ept_caps |=
3013 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003014 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04003015 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3016 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003017 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08003018 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003019
Paolo Bonzinief697a72016-03-18 16:58:38 +01003020 /*
3021 * Old versions of KVM use the single-context version without
3022 * checking for support, so declare that it is supported even
3023 * though it is treated as global context. The alternative is
3024 * not failing the single-context invvpid, and it is worse.
3025 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07003026 if (enable_vpid)
3027 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01003028 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07003029 else
3030 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003031
Radim Krčmář0790ec12015-03-17 14:02:32 +01003032 if (enable_unrestricted_guest)
3033 vmx->nested.nested_vmx_secondary_ctls_high |=
3034 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3035
Jan Kiszkac18911a2013-03-13 16:06:41 +01003036 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003037 rdmsr(MSR_IA32_VMX_MISC,
3038 vmx->nested.nested_vmx_misc_low,
3039 vmx->nested.nested_vmx_misc_high);
3040 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
3041 vmx->nested.nested_vmx_misc_low |=
3042 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003043 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003044 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003045}
3046
3047static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3048{
3049 /*
3050 * Bits 0 in high must be 0, and bits 1 in low must be 1.
3051 */
3052 return ((control & high) | low) == control;
3053}
3054
3055static inline u64 vmx_control_msr(u32 low, u32 high)
3056{
3057 return low | ((u64)high << 32);
3058}
3059
Jan Kiszkacae50132014-01-04 18:47:22 +01003060/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003061static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3062{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003063 struct vcpu_vmx *vmx = to_vmx(vcpu);
3064
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003065 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003066 case MSR_IA32_VMX_BASIC:
3067 /*
3068 * This MSR reports some information about VMX support. We
3069 * should return information about the VMX we emulate for the
3070 * guest, and the VMCS structure we give it - not about the
3071 * VMX support of the underlying hardware.
3072 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003073 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003074 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3075 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003076 if (cpu_has_vmx_basic_inout())
3077 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003078 break;
3079 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3080 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003081 *pdata = vmx_control_msr(
3082 vmx->nested.nested_vmx_pinbased_ctls_low,
3083 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003084 break;
3085 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003086 *pdata = vmx_control_msr(
3087 vmx->nested.nested_vmx_true_procbased_ctls_low,
3088 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02003089 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003090 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003091 *pdata = vmx_control_msr(
3092 vmx->nested.nested_vmx_procbased_ctls_low,
3093 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003094 break;
3095 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003096 *pdata = vmx_control_msr(
3097 vmx->nested.nested_vmx_true_exit_ctls_low,
3098 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003099 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003100 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003101 *pdata = vmx_control_msr(
3102 vmx->nested.nested_vmx_exit_ctls_low,
3103 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003104 break;
3105 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003106 *pdata = vmx_control_msr(
3107 vmx->nested.nested_vmx_true_entry_ctls_low,
3108 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003109 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003110 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003111 *pdata = vmx_control_msr(
3112 vmx->nested.nested_vmx_entry_ctls_low,
3113 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003114 break;
3115 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003116 *pdata = vmx_control_msr(
3117 vmx->nested.nested_vmx_misc_low,
3118 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003119 break;
3120 /*
3121 * These MSRs specify bits which the guest must keep fixed (on or off)
3122 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3123 * We picked the standard core2 setting.
3124 */
3125#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3126#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3127 case MSR_IA32_VMX_CR0_FIXED0:
3128 *pdata = VMXON_CR0_ALWAYSON;
3129 break;
3130 case MSR_IA32_VMX_CR0_FIXED1:
3131 *pdata = -1ULL;
3132 break;
3133 case MSR_IA32_VMX_CR4_FIXED0:
3134 *pdata = VMXON_CR4_ALWAYSON;
3135 break;
3136 case MSR_IA32_VMX_CR4_FIXED1:
3137 *pdata = -1ULL;
3138 break;
3139 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003140 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003141 break;
3142 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003143 *pdata = vmx_control_msr(
3144 vmx->nested.nested_vmx_secondary_ctls_low,
3145 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003146 break;
3147 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003148 *pdata = vmx->nested.nested_vmx_ept_caps |
3149 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003150 break;
3151 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003152 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003153 }
3154
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003155 return 0;
3156}
3157
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003158static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3159 uint64_t val)
3160{
3161 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3162
3163 return !(val & ~valid_bits);
3164}
3165
Tom Lendacky62d88fc2018-02-21 13:39:51 -06003166static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3167{
3168 return 1;
3169}
3170
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003171/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172 * Reads an msr value (of 'msr_index') into 'pdata'.
3173 * Returns 0 on success, non-0 otherwise.
3174 * Assumes vcpu_load() was already called.
3175 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003176static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177{
Avi Kivity26bb0982009-09-07 11:14:12 +03003178 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003180 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003181#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003183 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184 break;
3185 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003186 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003188 case MSR_KERNEL_GS_BASE:
3189 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003190 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003191 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003192#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003194 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303195 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003196 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003198 case MSR_IA32_SPEC_CTRL:
3199 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003200 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003201 return 1;
3202
3203 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3204 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003205 case MSR_IA32_ARCH_CAPABILITIES:
3206 if (!msr_info->host_initiated &&
3207 !guest_cpuid_has_arch_capabilities(vcpu))
3208 return 1;
3209 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3210 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003212 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 break;
3214 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003215 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 break;
3217 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003218 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003220 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003221 if (!kvm_mpx_supported() ||
3222 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003223 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003224 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003225 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003226 case MSR_IA32_MCG_EXT_CTL:
3227 if (!msr_info->host_initiated &&
3228 !(to_vmx(vcpu)->msr_ia32_feature_control &
3229 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003230 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003231 msr_info->data = vcpu->arch.mcg_ext_ctl;
3232 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003233 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003234 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003235 break;
3236 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3237 if (!nested_vmx_allowed(vcpu))
3238 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003239 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003240 case MSR_IA32_XSS:
3241 if (!vmx_xsaves_supported())
3242 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003243 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003244 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003245 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003246 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003247 return 1;
3248 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003250 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003251 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003252 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003253 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003255 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 }
3257
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 return 0;
3259}
3260
Jan Kiszkacae50132014-01-04 18:47:22 +01003261static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3262
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263/*
3264 * Writes msr value into into the appropriate "register".
3265 * Returns 0 on success, non-0 otherwise.
3266 * Assumes vcpu_load() was already called.
3267 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003268static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003270 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003271 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003272 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003273 u32 msr_index = msr_info->index;
3274 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003275
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003277 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003278 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003279 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003280#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003282 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 vmcs_writel(GUEST_FS_BASE, data);
3284 break;
3285 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003286 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287 vmcs_writel(GUEST_GS_BASE, data);
3288 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003289 case MSR_KERNEL_GS_BASE:
3290 vmx_load_host_state(vmx);
3291 vmx->msr_guest_kernel_gs_base = data;
3292 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293#endif
3294 case MSR_IA32_SYSENTER_CS:
3295 vmcs_write32(GUEST_SYSENTER_CS, data);
3296 break;
3297 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003298 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 break;
3300 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003301 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003303 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003304 if (!kvm_mpx_supported() ||
3305 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003306 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003307 if (is_noncanonical_address(data & PAGE_MASK) ||
3308 (data & MSR_IA32_BNDCFGS_RSVD))
3309 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003310 vmcs_write64(GUEST_BNDCFGS, data);
3311 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303312 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003313 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003315 case MSR_IA32_SPEC_CTRL:
3316 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003317 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003318 return 1;
3319
3320 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003321 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003322 return 1;
3323
3324 vmx->spec_ctrl = data;
3325
3326 if (!data)
3327 break;
3328
3329 /*
3330 * For non-nested:
3331 * When it's written (to non-zero) for the first time, pass
3332 * it through.
3333 *
3334 * For nested:
3335 * The handling of the MSR bitmap for L2 guests is done in
3336 * nested_vmx_merge_msr_bitmap. We should not touch the
3337 * vmcs02.msr_bitmap here since it gets completely overwritten
3338 * in the merging. We update the vmcs01 here for L1 as well
3339 * since it will end up touching the MSR anyway now.
3340 */
3341 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3342 MSR_IA32_SPEC_CTRL,
3343 MSR_TYPE_RW);
3344 break;
Ashok Raj70131292018-02-01 22:59:43 +01003345 case MSR_IA32_PRED_CMD:
3346 if (!msr_info->host_initiated &&
3347 !guest_cpuid_has_ibpb(vcpu))
3348 return 1;
3349
3350 if (data & ~PRED_CMD_IBPB)
3351 return 1;
3352
3353 if (!data)
3354 break;
3355
3356 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3357
3358 /*
3359 * For non-nested:
3360 * When it's written (to non-zero) for the first time, pass
3361 * it through.
3362 *
3363 * For nested:
3364 * The handling of the MSR bitmap for L2 guests is done in
3365 * nested_vmx_merge_msr_bitmap. We should not touch the
3366 * vmcs02.msr_bitmap here since it gets completely overwritten
3367 * in the merging.
3368 */
3369 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3370 MSR_TYPE_W);
3371 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003372 case MSR_IA32_ARCH_CAPABILITIES:
3373 if (!msr_info->host_initiated)
3374 return 1;
3375 vmx->arch_capabilities = data;
3376 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003377 case MSR_IA32_CR_PAT:
3378 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003379 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3380 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003381 vmcs_write64(GUEST_IA32_PAT, data);
3382 vcpu->arch.pat = data;
3383 break;
3384 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003385 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003386 break;
Will Auldba904632012-11-29 12:42:50 -08003387 case MSR_IA32_TSC_ADJUST:
3388 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003389 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003390 case MSR_IA32_MCG_EXT_CTL:
3391 if ((!msr_info->host_initiated &&
3392 !(to_vmx(vcpu)->msr_ia32_feature_control &
3393 FEATURE_CONTROL_LMCE)) ||
3394 (data & ~MCG_EXT_CTL_LMCE_EN))
3395 return 1;
3396 vcpu->arch.mcg_ext_ctl = data;
3397 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003398 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003399 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003400 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003401 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3402 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003403 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003404 if (msr_info->host_initiated && data == 0)
3405 vmx_leave_nested(vcpu);
3406 break;
3407 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3408 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003409 case MSR_IA32_XSS:
3410 if (!vmx_xsaves_supported())
3411 return 1;
3412 /*
3413 * The only supported bit as of Skylake is bit 8, but
3414 * it is not supported on KVM.
3415 */
3416 if (data != 0)
3417 return 1;
3418 vcpu->arch.ia32_xss = data;
3419 if (vcpu->arch.ia32_xss != host_xss)
3420 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04003421 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08003422 else
3423 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3424 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003425 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003426 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003427 return 1;
3428 /* Check reserved bit, higher 32 bits should be zero */
3429 if ((data >> 32) != 0)
3430 return 1;
3431 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003433 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003434 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003435 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003436 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003437 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3438 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003439 ret = kvm_set_shared_msr(msr->index, msr->data,
3440 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003441 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003442 if (ret)
3443 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003444 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003445 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003447 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448 }
3449
Eddie Dong2cc51562007-05-21 07:28:09 +03003450 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451}
3452
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003453static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003455 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3456 switch (reg) {
3457 case VCPU_REGS_RSP:
3458 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3459 break;
3460 case VCPU_REGS_RIP:
3461 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3462 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003463 case VCPU_EXREG_PDPTR:
3464 if (enable_ept)
3465 ept_save_pdptrs(vcpu);
3466 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003467 default:
3468 break;
3469 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470}
3471
Avi Kivity6aa8b732006-12-10 02:21:36 -08003472static __init int cpu_has_kvm_support(void)
3473{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003474 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475}
3476
3477static __init int vmx_disabled_by_bios(void)
3478{
3479 u64 msr;
3480
3481 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003482 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003483 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003484 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3485 && tboot_enabled())
3486 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003487 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003488 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003489 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003490 && !tboot_enabled()) {
3491 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003492 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003493 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003494 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003495 /* launched w/o TXT and VMX disabled */
3496 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3497 && !tboot_enabled())
3498 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003499 }
3500
3501 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502}
3503
Dongxiao Xu7725b892010-05-11 18:29:38 +08003504static void kvm_cpu_vmxon(u64 addr)
3505{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003506 intel_pt_handle_vmx(1);
3507
Dongxiao Xu7725b892010-05-11 18:29:38 +08003508 asm volatile (ASM_VMX_VMXON_RAX
3509 : : "a"(&addr), "m"(addr)
3510 : "memory", "cc");
3511}
3512
Radim Krčmář13a34e02014-08-28 15:13:03 +02003513static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514{
3515 int cpu = raw_smp_processor_id();
3516 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003517 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003519 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003520 return -EBUSY;
3521
Nadav Har'Eld462b812011-05-24 15:26:10 +03003522 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003523 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3524 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003525
3526 /*
3527 * Now we can enable the vmclear operation in kdump
3528 * since the loaded_vmcss_on_cpu list on this cpu
3529 * has been initialized.
3530 *
3531 * Though the cpu is not in VMX operation now, there
3532 * is no problem to enable the vmclear operation
3533 * for the loaded_vmcss_on_cpu list is empty!
3534 */
3535 crash_enable_local_vmclear(cpu);
3536
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003538
3539 test_bits = FEATURE_CONTROL_LOCKED;
3540 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3541 if (tboot_enabled())
3542 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3543
3544 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003546 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3547 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003548 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003549
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003550 if (vmm_exclusive) {
3551 kvm_cpu_vmxon(phys_addr);
3552 ept_sync_global();
3553 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003554
Christoph Lameter89cbc762014-08-17 12:30:40 -05003555 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003556
Alexander Graf10474ae2009-09-15 11:37:46 +02003557 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558}
3559
Nadav Har'Eld462b812011-05-24 15:26:10 +03003560static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003561{
3562 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003563 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003564
Nadav Har'Eld462b812011-05-24 15:26:10 +03003565 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3566 loaded_vmcss_on_cpu_link)
3567 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003568}
3569
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003570
3571/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3572 * tricks.
3573 */
3574static void kvm_cpu_vmxoff(void)
3575{
3576 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003577
3578 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003579}
3580
Radim Krčmář13a34e02014-08-28 15:13:03 +02003581static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003582{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003583 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003584 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003585 kvm_cpu_vmxoff();
3586 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003587 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588}
3589
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003590static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003591 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592{
3593 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003594 u32 ctl = ctl_min | ctl_opt;
3595
3596 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3597
3598 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3599 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3600
3601 /* Ensure minimum (required) set of control bits are supported. */
3602 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003603 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003604
3605 *result = ctl;
3606 return 0;
3607}
3608
Avi Kivity110312c2010-12-21 12:54:20 +02003609static __init bool allow_1_setting(u32 msr, u32 ctl)
3610{
3611 u32 vmx_msr_low, vmx_msr_high;
3612
3613 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3614 return vmx_msr_high & ctl;
3615}
3616
Yang, Sheng002c7f72007-07-31 14:23:01 +03003617static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003618{
3619 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003620 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003621 u32 _pin_based_exec_control = 0;
3622 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003623 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003624 u32 _vmexit_control = 0;
3625 u32 _vmentry_control = 0;
3626
Raghavendra K T10166742012-02-07 23:19:20 +05303627 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003628#ifdef CONFIG_X86_64
3629 CPU_BASED_CR8_LOAD_EXITING |
3630 CPU_BASED_CR8_STORE_EXITING |
3631#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003632 CPU_BASED_CR3_LOAD_EXITING |
3633 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003634 CPU_BASED_USE_IO_BITMAPS |
3635 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003636 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003637 CPU_BASED_MWAIT_EXITING |
3638 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003639 CPU_BASED_INVLPG_EXITING |
3640 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003641
Sheng Yangf78e0e22007-10-29 09:40:42 +08003642 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003643 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003644 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003645 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3646 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003647 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003648#ifdef CONFIG_X86_64
3649 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3650 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3651 ~CPU_BASED_CR8_STORE_EXITING;
3652#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003653 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003654 min2 = 0;
3655 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003656 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003657 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003658 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003659 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003660 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003661 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003662 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003663 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003664 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003665 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003666 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003667 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003668 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003669 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003670 if (adjust_vmx_controls(min2, opt2,
3671 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003672 &_cpu_based_2nd_exec_control) < 0)
3673 return -EIO;
3674 }
3675#ifndef CONFIG_X86_64
3676 if (!(_cpu_based_2nd_exec_control &
3677 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3678 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3679#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003680
3681 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3682 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003683 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003684 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3685 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003686
Sheng Yangd56f5462008-04-25 10:13:16 +08003687 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003688 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3689 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003690 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3691 CPU_BASED_CR3_STORE_EXITING |
3692 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003693 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3694 vmx_capability.ept, vmx_capability.vpid);
3695 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003696
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003697 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003698#ifdef CONFIG_X86_64
3699 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3700#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003701 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003702 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003703 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3704 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003705 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003706
Yang Zhang01e439b2013-04-11 19:25:12 +08003707 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003708 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3709 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003710 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3711 &_pin_based_exec_control) < 0)
3712 return -EIO;
3713
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003714 if (cpu_has_broken_vmx_preemption_timer())
3715 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003716 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003717 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003718 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3719
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003720 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003721 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003722 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3723 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003724 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003726 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003727
3728 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3729 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003730 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003731
3732#ifdef CONFIG_X86_64
3733 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3734 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003735 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003736#endif
3737
3738 /* Require Write-Back (WB) memory type for VMCS accesses. */
3739 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003740 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003741
Yang, Sheng002c7f72007-07-31 14:23:01 +03003742 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003743 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003744 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003745 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003746
Yang, Sheng002c7f72007-07-31 14:23:01 +03003747 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3748 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003749 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003750 vmcs_conf->vmexit_ctrl = _vmexit_control;
3751 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003752
Avi Kivity110312c2010-12-21 12:54:20 +02003753 cpu_has_load_ia32_efer =
3754 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3755 VM_ENTRY_LOAD_IA32_EFER)
3756 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3757 VM_EXIT_LOAD_IA32_EFER);
3758
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003759 cpu_has_load_perf_global_ctrl =
3760 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3761 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3762 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3763 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3764
3765 /*
3766 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003767 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003768 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3769 *
3770 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3771 *
3772 * AAK155 (model 26)
3773 * AAP115 (model 30)
3774 * AAT100 (model 37)
3775 * BC86,AAY89,BD102 (model 44)
3776 * BA97 (model 46)
3777 *
3778 */
3779 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3780 switch (boot_cpu_data.x86_model) {
3781 case 26:
3782 case 30:
3783 case 37:
3784 case 44:
3785 case 46:
3786 cpu_has_load_perf_global_ctrl = false;
3787 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3788 "does not work properly. Using workaround\n");
3789 break;
3790 default:
3791 break;
3792 }
3793 }
3794
Borislav Petkov782511b2016-04-04 22:25:03 +02003795 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003796 rdmsrl(MSR_IA32_XSS, host_xss);
3797
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003798 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003799}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800
3801static struct vmcs *alloc_vmcs_cpu(int cpu)
3802{
3803 int node = cpu_to_node(cpu);
3804 struct page *pages;
3805 struct vmcs *vmcs;
3806
Vlastimil Babka96db8002015-09-08 15:03:50 -07003807 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808 if (!pages)
3809 return NULL;
3810 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003811 memset(vmcs, 0, vmcs_config.size);
3812 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003813 return vmcs;
3814}
3815
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816static void free_vmcs(struct vmcs *vmcs)
3817{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003818 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819}
3820
Nadav Har'Eld462b812011-05-24 15:26:10 +03003821/*
3822 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3823 */
3824static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3825{
3826 if (!loaded_vmcs->vmcs)
3827 return;
3828 loaded_vmcs_clear(loaded_vmcs);
3829 free_vmcs(loaded_vmcs->vmcs);
3830 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003831 if (loaded_vmcs->msr_bitmap)
3832 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003833 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003834}
3835
Paolo Bonziniff546f92018-01-11 12:16:15 +01003836static struct vmcs *alloc_vmcs(void)
3837{
3838 return alloc_vmcs_cpu(raw_smp_processor_id());
3839}
3840
3841static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3842{
3843 loaded_vmcs->vmcs = alloc_vmcs();
3844 if (!loaded_vmcs->vmcs)
3845 return -ENOMEM;
3846
3847 loaded_vmcs->shadow_vmcs = NULL;
3848 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003849
3850 if (cpu_has_vmx_msr_bitmap()) {
3851 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3852 if (!loaded_vmcs->msr_bitmap)
3853 goto out_vmcs;
3854 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3855 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003856 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003857
3858out_vmcs:
3859 free_loaded_vmcs(loaded_vmcs);
3860 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003861}
3862
Sam Ravnborg39959582007-06-01 00:47:13 -07003863static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864{
3865 int cpu;
3866
Zachary Amsden3230bb42009-09-29 11:38:37 -10003867 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003869 per_cpu(vmxarea, cpu) = NULL;
3870 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871}
3872
Bandan Dasfe2b2012014-04-21 15:20:14 -04003873static void init_vmcs_shadow_fields(void)
3874{
3875 int i, j;
3876
3877 /* No checks for read only fields yet */
3878
3879 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3880 switch (shadow_read_write_fields[i]) {
3881 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003882 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003883 continue;
3884 break;
3885 default:
3886 break;
3887 }
3888
3889 if (j < i)
3890 shadow_read_write_fields[j] =
3891 shadow_read_write_fields[i];
3892 j++;
3893 }
3894 max_shadow_read_write_fields = j;
3895
3896 /* shadowed fields guest access without vmexit */
3897 for (i = 0; i < max_shadow_read_write_fields; i++) {
3898 clear_bit(shadow_read_write_fields[i],
3899 vmx_vmwrite_bitmap);
3900 clear_bit(shadow_read_write_fields[i],
3901 vmx_vmread_bitmap);
3902 }
3903 for (i = 0; i < max_shadow_read_only_fields; i++)
3904 clear_bit(shadow_read_only_fields[i],
3905 vmx_vmread_bitmap);
3906}
3907
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908static __init int alloc_kvm_area(void)
3909{
3910 int cpu;
3911
Zachary Amsden3230bb42009-09-29 11:38:37 -10003912 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003913 struct vmcs *vmcs;
3914
3915 vmcs = alloc_vmcs_cpu(cpu);
3916 if (!vmcs) {
3917 free_kvm_area();
3918 return -ENOMEM;
3919 }
3920
3921 per_cpu(vmxarea, cpu) = vmcs;
3922 }
3923 return 0;
3924}
3925
Gleb Natapov14168782013-01-21 15:36:49 +02003926static bool emulation_required(struct kvm_vcpu *vcpu)
3927{
3928 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3929}
3930
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003931static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003932 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003934 if (!emulate_invalid_guest_state) {
3935 /*
3936 * CS and SS RPL should be equal during guest entry according
3937 * to VMX spec, but in reality it is not always so. Since vcpu
3938 * is in the middle of the transition from real mode to
3939 * protected mode it is safe to assume that RPL 0 is a good
3940 * default value.
3941 */
3942 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003943 save->selector &= ~SEGMENT_RPL_MASK;
3944 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003945 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003947 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948}
3949
3950static void enter_pmode(struct kvm_vcpu *vcpu)
3951{
3952 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003953 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954
Gleb Natapovd99e4152012-12-20 16:57:45 +02003955 /*
3956 * Update real mode segment cache. It may be not up-to-date if sement
3957 * register was written while vcpu was in a guest mode.
3958 */
3959 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3960 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3961 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3962 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3963 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3964 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3965
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003966 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003967
Avi Kivity2fb92db2011-04-27 19:42:18 +03003968 vmx_segment_cache_clear(vmx);
3969
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003970 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003971
3972 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003973 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3974 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975 vmcs_writel(GUEST_RFLAGS, flags);
3976
Rusty Russell66aee912007-07-17 23:34:16 +10003977 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3978 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003979
3980 update_exception_bitmap(vcpu);
3981
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003982 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3983 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3984 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3985 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3986 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3987 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003988}
3989
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003990static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003991{
Mathias Krause772e0312012-08-30 01:30:19 +02003992 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003993 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994
Gleb Natapovd99e4152012-12-20 16:57:45 +02003995 var.dpl = 0x3;
3996 if (seg == VCPU_SREG_CS)
3997 var.type = 0x3;
3998
3999 if (!emulate_invalid_guest_state) {
4000 var.selector = var.base >> 4;
4001 var.base = var.base & 0xffff0;
4002 var.limit = 0xffff;
4003 var.g = 0;
4004 var.db = 0;
4005 var.present = 1;
4006 var.s = 1;
4007 var.l = 0;
4008 var.unusable = 0;
4009 var.type = 0x3;
4010 var.avl = 0;
4011 if (save->base & 0xf)
4012 printk_once(KERN_WARNING "kvm: segment base is not "
4013 "paragraph aligned when entering "
4014 "protected mode (seg=%d)", seg);
4015 }
4016
4017 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05004018 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004019 vmcs_write32(sf->limit, var.limit);
4020 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021}
4022
4023static void enter_rmode(struct kvm_vcpu *vcpu)
4024{
4025 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004026 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004027
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004028 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4029 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4030 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4031 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4032 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004033 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4034 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004035
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004036 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004037
Gleb Natapov776e58e2011-03-13 12:34:27 +02004038 /*
4039 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004040 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004041 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004042 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004043 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4044 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004045
Avi Kivity2fb92db2011-04-27 19:42:18 +03004046 vmx_segment_cache_clear(vmx);
4047
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004048 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004050 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4051
4052 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004053 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004055 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056
4057 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004058 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 update_exception_bitmap(vcpu);
4060
Gleb Natapovd99e4152012-12-20 16:57:45 +02004061 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4062 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4063 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4064 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4065 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4066 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004067
Eddie Dong8668a3c2007-10-10 14:26:45 +08004068 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004069}
4070
Amit Shah401d10d2009-02-20 22:53:37 +05304071static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4072{
4073 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004074 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4075
4076 if (!msr)
4077 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304078
Avi Kivity44ea2b12009-09-06 15:55:37 +03004079 /*
4080 * Force kernel_gs_base reloading before EFER changes, as control
4081 * of this msr depends on is_long_mode().
4082 */
4083 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004084 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304085 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004086 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304087 msr->data = efer;
4088 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004089 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304090
4091 msr->data = efer & ~EFER_LME;
4092 }
4093 setup_msrs(vmx);
4094}
4095
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004096#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097
4098static void enter_lmode(struct kvm_vcpu *vcpu)
4099{
4100 u32 guest_tr_ar;
4101
Avi Kivity2fb92db2011-04-27 19:42:18 +03004102 vmx_segment_cache_clear(to_vmx(vcpu));
4103
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004105 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004106 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4107 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004109 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4110 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 }
Avi Kivityda38f432010-07-06 11:30:49 +03004112 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113}
4114
4115static void exit_lmode(struct kvm_vcpu *vcpu)
4116{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004117 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004118 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119}
4120
4121#endif
4122
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004123static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004124{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004125 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004126 if (enable_ept) {
4127 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4128 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004129 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004130 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004131}
4132
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004133static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4134{
4135 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4136}
4137
Jim Mattson8386ff52017-03-16 13:53:59 -07004138static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4139{
4140 if (enable_ept)
4141 vmx_flush_tlb(vcpu);
4142}
4143
Avi Kivitye8467fd2009-12-29 18:43:06 +02004144static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4145{
4146 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4147
4148 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4149 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4150}
4151
Avi Kivityaff48ba2010-12-05 18:56:11 +02004152static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4153{
4154 if (enable_ept && is_paging(vcpu))
4155 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4156 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4157}
4158
Anthony Liguori25c4c272007-04-27 09:29:21 +03004159static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004160{
Avi Kivityfc78f512009-12-07 12:16:48 +02004161 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4162
4163 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4164 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004165}
4166
Sheng Yang14394422008-04-28 12:24:45 +08004167static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4168{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004169 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4170
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004171 if (!test_bit(VCPU_EXREG_PDPTR,
4172 (unsigned long *)&vcpu->arch.regs_dirty))
4173 return;
4174
Sheng Yang14394422008-04-28 12:24:45 +08004175 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004176 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4177 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4178 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4179 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004180 }
4181}
4182
Avi Kivity8f5d5492009-05-31 18:41:29 +03004183static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4184{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004185 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4186
Avi Kivity8f5d5492009-05-31 18:41:29 +03004187 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004188 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4189 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4190 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4191 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004192 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004193
4194 __set_bit(VCPU_EXREG_PDPTR,
4195 (unsigned long *)&vcpu->arch.regs_avail);
4196 __set_bit(VCPU_EXREG_PDPTR,
4197 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004198}
4199
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004200static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004201
4202static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4203 unsigned long cr0,
4204 struct kvm_vcpu *vcpu)
4205{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004206 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4207 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004208 if (!(cr0 & X86_CR0_PG)) {
4209 /* From paging/starting to nonpaging */
4210 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004211 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004212 (CPU_BASED_CR3_LOAD_EXITING |
4213 CPU_BASED_CR3_STORE_EXITING));
4214 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004215 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004216 } else if (!is_paging(vcpu)) {
4217 /* From nonpaging to paging */
4218 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004219 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004220 ~(CPU_BASED_CR3_LOAD_EXITING |
4221 CPU_BASED_CR3_STORE_EXITING));
4222 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004223 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004224 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004225
4226 if (!(cr0 & X86_CR0_WP))
4227 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004228}
4229
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4231{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004232 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004233 unsigned long hw_cr0;
4234
Gleb Natapov50378782013-02-04 16:00:28 +02004235 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004236 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004237 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004238 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004239 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004240
Gleb Natapov218e7632013-01-21 15:36:45 +02004241 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4242 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243
Gleb Natapov218e7632013-01-21 15:36:45 +02004244 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4245 enter_rmode(vcpu);
4246 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004248#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004249 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004250 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004252 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253 exit_lmode(vcpu);
4254 }
4255#endif
4256
Avi Kivity089d0342009-03-23 18:26:32 +02004257 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004258 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4259
Avi Kivity02daab22009-12-30 12:40:26 +02004260 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004261 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004262
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004264 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004265 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004266
4267 /* depends on vcpu->arch.cr0 to be set to a new value */
4268 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004269}
4270
Sheng Yang14394422008-04-28 12:24:45 +08004271static u64 construct_eptp(unsigned long root_hpa)
4272{
4273 u64 eptp;
4274
4275 /* TODO write the value reading from MSR */
4276 eptp = VMX_EPT_DEFAULT_MT |
4277 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004278 if (enable_ept_ad_bits)
4279 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004280 eptp |= (root_hpa & PAGE_MASK);
4281
4282 return eptp;
4283}
4284
Avi Kivity6aa8b732006-12-10 02:21:36 -08004285static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4286{
Sheng Yang14394422008-04-28 12:24:45 +08004287 unsigned long guest_cr3;
4288 u64 eptp;
4289
4290 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004291 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004292 eptp = construct_eptp(cr3);
4293 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004294 if (is_paging(vcpu) || is_guest_mode(vcpu))
4295 guest_cr3 = kvm_read_cr3(vcpu);
4296 else
4297 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004298 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004299 }
4300
Sheng Yang2384d2b2008-01-17 15:14:33 +08004301 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004302 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303}
4304
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004305static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004306{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004307 /*
4308 * Pass through host's Machine Check Enable value to hw_cr4, which
4309 * is in force while we are in guest mode. Do not let guests control
4310 * this bit, even if host CR4.MCE == 0.
4311 */
4312 unsigned long hw_cr4 =
4313 (cr4_read_shadow() & X86_CR4_MCE) |
4314 (cr4 & ~X86_CR4_MCE) |
4315 (to_vmx(vcpu)->rmode.vm86_active ?
4316 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004317
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004318 if (cr4 & X86_CR4_VMXE) {
4319 /*
4320 * To use VMXON (and later other VMX instructions), a guest
4321 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4322 * So basically the check on whether to allow nested VMX
4323 * is here.
4324 */
4325 if (!nested_vmx_allowed(vcpu))
4326 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004327 }
4328 if (to_vmx(vcpu)->nested.vmxon &&
4329 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004330 return 1;
4331
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004332 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004333 if (enable_ept) {
4334 if (!is_paging(vcpu)) {
4335 hw_cr4 &= ~X86_CR4_PAE;
4336 hw_cr4 |= X86_CR4_PSE;
4337 } else if (!(cr4 & X86_CR4_PAE)) {
4338 hw_cr4 &= ~X86_CR4_PAE;
4339 }
4340 }
Sheng Yang14394422008-04-28 12:24:45 +08004341
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004342 if (!enable_unrestricted_guest && !is_paging(vcpu))
4343 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004344 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4345 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4346 * to be manually disabled when guest switches to non-paging
4347 * mode.
4348 *
4349 * If !enable_unrestricted_guest, the CPU is always running
4350 * with CR0.PG=1 and CR4 needs to be modified.
4351 * If enable_unrestricted_guest, the CPU automatically
4352 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004353 */
Huaitong Handdba2622016-03-22 16:51:15 +08004354 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004355
Sheng Yang14394422008-04-28 12:24:45 +08004356 vmcs_writel(CR4_READ_SHADOW, cr4);
4357 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004358 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359}
4360
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361static void vmx_get_segment(struct kvm_vcpu *vcpu,
4362 struct kvm_segment *var, int seg)
4363{
Avi Kivitya9179492011-01-03 14:28:52 +02004364 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365 u32 ar;
4366
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004367 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004368 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004369 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004370 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004371 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004372 var->base = vmx_read_guest_seg_base(vmx, seg);
4373 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4374 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004375 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004376 var->base = vmx_read_guest_seg_base(vmx, seg);
4377 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4378 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4379 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004380 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381 var->type = ar & 15;
4382 var->s = (ar >> 4) & 1;
4383 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004384 /*
4385 * Some userspaces do not preserve unusable property. Since usable
4386 * segment has to be present according to VMX spec we can use present
4387 * property to amend userspace bug by making unusable segment always
4388 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4389 * segment as unusable.
4390 */
4391 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392 var->avl = (ar >> 12) & 1;
4393 var->l = (ar >> 13) & 1;
4394 var->db = (ar >> 14) & 1;
4395 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396}
4397
Avi Kivitya9179492011-01-03 14:28:52 +02004398static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4399{
Avi Kivitya9179492011-01-03 14:28:52 +02004400 struct kvm_segment s;
4401
4402 if (to_vmx(vcpu)->rmode.vm86_active) {
4403 vmx_get_segment(vcpu, &s, seg);
4404 return s.base;
4405 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004406 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004407}
4408
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004409static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004410{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004411 struct vcpu_vmx *vmx = to_vmx(vcpu);
4412
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004413 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004414 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004415 else {
4416 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004417 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004418 }
Avi Kivity69c73022011-03-07 15:26:44 +02004419}
4420
Avi Kivity653e3102007-05-07 10:55:37 +03004421static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004423 u32 ar;
4424
Avi Kivityf0495f92012-06-07 17:06:10 +03004425 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004426 ar = 1 << 16;
4427 else {
4428 ar = var->type & 15;
4429 ar |= (var->s & 1) << 4;
4430 ar |= (var->dpl & 3) << 5;
4431 ar |= (var->present & 1) << 7;
4432 ar |= (var->avl & 1) << 12;
4433 ar |= (var->l & 1) << 13;
4434 ar |= (var->db & 1) << 14;
4435 ar |= (var->g & 1) << 15;
4436 }
Avi Kivity653e3102007-05-07 10:55:37 +03004437
4438 return ar;
4439}
4440
4441static void vmx_set_segment(struct kvm_vcpu *vcpu,
4442 struct kvm_segment *var, int seg)
4443{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004444 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004445 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004446
Avi Kivity2fb92db2011-04-27 19:42:18 +03004447 vmx_segment_cache_clear(vmx);
4448
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004449 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4450 vmx->rmode.segs[seg] = *var;
4451 if (seg == VCPU_SREG_TR)
4452 vmcs_write16(sf->selector, var->selector);
4453 else if (var->s)
4454 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004455 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004456 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004457
Avi Kivity653e3102007-05-07 10:55:37 +03004458 vmcs_writel(sf->base, var->base);
4459 vmcs_write32(sf->limit, var->limit);
4460 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004461
4462 /*
4463 * Fix the "Accessed" bit in AR field of segment registers for older
4464 * qemu binaries.
4465 * IA32 arch specifies that at the time of processor reset the
4466 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004467 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004468 * state vmexit when "unrestricted guest" mode is turned on.
4469 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4470 * tree. Newer qemu binaries with that qemu fix would not need this
4471 * kvm hack.
4472 */
4473 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004474 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004475
Gleb Natapovf924d662012-12-12 19:10:55 +02004476 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004477
4478out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004479 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480}
4481
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4483{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004484 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004485
4486 *db = (ar >> 14) & 1;
4487 *l = (ar >> 13) & 1;
4488}
4489
Gleb Natapov89a27f42010-02-16 10:51:48 +02004490static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004491{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004492 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4493 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004494}
4495
Gleb Natapov89a27f42010-02-16 10:51:48 +02004496static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004498 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4499 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500}
4501
Gleb Natapov89a27f42010-02-16 10:51:48 +02004502static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004503{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004504 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4505 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004506}
4507
Gleb Natapov89a27f42010-02-16 10:51:48 +02004508static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004509{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004510 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4511 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512}
4513
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004514static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4515{
4516 struct kvm_segment var;
4517 u32 ar;
4518
4519 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004520 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004521 if (seg == VCPU_SREG_CS)
4522 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004523 ar = vmx_segment_access_rights(&var);
4524
4525 if (var.base != (var.selector << 4))
4526 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004527 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004528 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004529 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004530 return false;
4531
4532 return true;
4533}
4534
4535static bool code_segment_valid(struct kvm_vcpu *vcpu)
4536{
4537 struct kvm_segment cs;
4538 unsigned int cs_rpl;
4539
4540 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004541 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004542
Avi Kivity1872a3f2009-01-04 23:26:52 +02004543 if (cs.unusable)
4544 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004545 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004546 return false;
4547 if (!cs.s)
4548 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004549 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004550 if (cs.dpl > cs_rpl)
4551 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004552 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004553 if (cs.dpl != cs_rpl)
4554 return false;
4555 }
4556 if (!cs.present)
4557 return false;
4558
4559 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4560 return true;
4561}
4562
4563static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4564{
4565 struct kvm_segment ss;
4566 unsigned int ss_rpl;
4567
4568 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004569 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004570
Avi Kivity1872a3f2009-01-04 23:26:52 +02004571 if (ss.unusable)
4572 return true;
4573 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004574 return false;
4575 if (!ss.s)
4576 return false;
4577 if (ss.dpl != ss_rpl) /* DPL != RPL */
4578 return false;
4579 if (!ss.present)
4580 return false;
4581
4582 return true;
4583}
4584
4585static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4586{
4587 struct kvm_segment var;
4588 unsigned int rpl;
4589
4590 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004591 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004592
Avi Kivity1872a3f2009-01-04 23:26:52 +02004593 if (var.unusable)
4594 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004595 if (!var.s)
4596 return false;
4597 if (!var.present)
4598 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004599 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004600 if (var.dpl < rpl) /* DPL < RPL */
4601 return false;
4602 }
4603
4604 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4605 * rights flags
4606 */
4607 return true;
4608}
4609
4610static bool tr_valid(struct kvm_vcpu *vcpu)
4611{
4612 struct kvm_segment tr;
4613
4614 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4615
Avi Kivity1872a3f2009-01-04 23:26:52 +02004616 if (tr.unusable)
4617 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004618 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004619 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004620 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004621 return false;
4622 if (!tr.present)
4623 return false;
4624
4625 return true;
4626}
4627
4628static bool ldtr_valid(struct kvm_vcpu *vcpu)
4629{
4630 struct kvm_segment ldtr;
4631
4632 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4633
Avi Kivity1872a3f2009-01-04 23:26:52 +02004634 if (ldtr.unusable)
4635 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004636 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004637 return false;
4638 if (ldtr.type != 2)
4639 return false;
4640 if (!ldtr.present)
4641 return false;
4642
4643 return true;
4644}
4645
4646static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4647{
4648 struct kvm_segment cs, ss;
4649
4650 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4651 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4652
Nadav Amitb32a9912015-03-29 16:33:04 +03004653 return ((cs.selector & SEGMENT_RPL_MASK) ==
4654 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004655}
4656
4657/*
4658 * Check if guest state is valid. Returns true if valid, false if
4659 * not.
4660 * We assume that registers are always usable
4661 */
4662static bool guest_state_valid(struct kvm_vcpu *vcpu)
4663{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004664 if (enable_unrestricted_guest)
4665 return true;
4666
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004667 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004668 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004669 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4670 return false;
4671 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4672 return false;
4673 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4674 return false;
4675 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4676 return false;
4677 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4678 return false;
4679 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4680 return false;
4681 } else {
4682 /* protected mode guest state checks */
4683 if (!cs_ss_rpl_check(vcpu))
4684 return false;
4685 if (!code_segment_valid(vcpu))
4686 return false;
4687 if (!stack_segment_valid(vcpu))
4688 return false;
4689 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4690 return false;
4691 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4692 return false;
4693 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4694 return false;
4695 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4696 return false;
4697 if (!tr_valid(vcpu))
4698 return false;
4699 if (!ldtr_valid(vcpu))
4700 return false;
4701 }
4702 /* TODO:
4703 * - Add checks on RIP
4704 * - Add checks on RFLAGS
4705 */
4706
4707 return true;
4708}
4709
Mike Dayd77c26f2007-10-08 09:02:08 -04004710static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004712 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004713 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004714 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004716 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004717 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004718 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4719 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004720 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004721 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004722 r = kvm_write_guest_page(kvm, fn++, &data,
4723 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004724 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004725 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004726 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4727 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004728 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004729 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4730 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004731 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004732 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004733 r = kvm_write_guest_page(kvm, fn, &data,
4734 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4735 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004736out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004737 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004738 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739}
4740
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004741static int init_rmode_identity_map(struct kvm *kvm)
4742{
Tang Chenf51770e2014-09-16 18:41:59 +08004743 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004744 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004745 u32 tmp;
4746
Avi Kivity089d0342009-03-23 18:26:32 +02004747 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004748 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004749
4750 /* Protect kvm->arch.ept_identity_pagetable_done. */
4751 mutex_lock(&kvm->slots_lock);
4752
Tang Chenf51770e2014-09-16 18:41:59 +08004753 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004754 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004755
Sheng Yangb927a3c2009-07-21 10:42:48 +08004756 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004757
4758 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004759 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004760 goto out2;
4761
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004762 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004763 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4764 if (r < 0)
4765 goto out;
4766 /* Set up identity-mapping pagetable for EPT in real mode */
4767 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4768 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4769 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4770 r = kvm_write_guest_page(kvm, identity_map_pfn,
4771 &tmp, i * sizeof(tmp), sizeof(tmp));
4772 if (r < 0)
4773 goto out;
4774 }
4775 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004776
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004777out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004778 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004779
4780out2:
4781 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004782 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004783}
4784
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785static void seg_setup(int seg)
4786{
Mathias Krause772e0312012-08-30 01:30:19 +02004787 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004788 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789
4790 vmcs_write16(sf->selector, 0);
4791 vmcs_writel(sf->base, 0);
4792 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004793 ar = 0x93;
4794 if (seg == VCPU_SREG_CS)
4795 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004796
4797 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798}
4799
Sheng Yangf78e0e22007-10-29 09:40:42 +08004800static int alloc_apic_access_page(struct kvm *kvm)
4801{
Xiao Guangrong44841412012-09-07 14:14:20 +08004802 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004803 int r = 0;
4804
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004805 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004806 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004807 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004808 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4809 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004810 if (r)
4811 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004812
Tang Chen73a6d942014-09-11 13:38:00 +08004813 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004814 if (is_error_page(page)) {
4815 r = -EFAULT;
4816 goto out;
4817 }
4818
Tang Chenc24ae0d2014-09-24 15:57:58 +08004819 /*
4820 * Do not pin the page in memory, so that memory hot-unplug
4821 * is able to migrate it.
4822 */
4823 put_page(page);
4824 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004825out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004826 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004827 return r;
4828}
4829
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004830static int alloc_identity_pagetable(struct kvm *kvm)
4831{
Tang Chena255d472014-09-16 18:41:58 +08004832 /* Called with kvm->slots_lock held. */
4833
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004834 int r = 0;
4835
Tang Chena255d472014-09-16 18:41:58 +08004836 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4837
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004838 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4839 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004840
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004841 return r;
4842}
4843
Wanpeng Li991e7a02015-09-16 17:30:05 +08004844static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004845{
4846 int vpid;
4847
Avi Kivity919818a2009-03-23 18:01:29 +02004848 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004849 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004850 spin_lock(&vmx_vpid_lock);
4851 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004852 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004853 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004854 else
4855 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004856 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004857 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004858}
4859
Wanpeng Li991e7a02015-09-16 17:30:05 +08004860static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004861{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004862 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004863 return;
4864 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004865 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004866 spin_unlock(&vmx_vpid_lock);
4867}
4868
Paolo Bonzini6236b782018-01-16 16:51:18 +01004869static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4870 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004871{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004872 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004873
4874 if (!cpu_has_vmx_msr_bitmap())
4875 return;
4876
4877 /*
4878 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4879 * have the write-low and read-high bitmap offsets the wrong way round.
4880 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4881 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004882 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004883 if (type & MSR_TYPE_R)
4884 /* read-low */
4885 __clear_bit(msr, msr_bitmap + 0x000 / f);
4886
4887 if (type & MSR_TYPE_W)
4888 /* write-low */
4889 __clear_bit(msr, msr_bitmap + 0x800 / f);
4890
Sheng Yang25c5f222008-03-28 13:18:56 +08004891 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4892 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004893 if (type & MSR_TYPE_R)
4894 /* read-high */
4895 __clear_bit(msr, msr_bitmap + 0x400 / f);
4896
4897 if (type & MSR_TYPE_W)
4898 /* write-high */
4899 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4900
4901 }
4902}
4903
Paolo Bonzini6236b782018-01-16 16:51:18 +01004904static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4905 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004906{
4907 int f = sizeof(unsigned long);
4908
4909 if (!cpu_has_vmx_msr_bitmap())
4910 return;
4911
4912 /*
4913 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4914 * have the write-low and read-high bitmap offsets the wrong way round.
4915 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4916 */
4917 if (msr <= 0x1fff) {
4918 if (type & MSR_TYPE_R)
4919 /* read-low */
4920 __set_bit(msr, msr_bitmap + 0x000 / f);
4921
4922 if (type & MSR_TYPE_W)
4923 /* write-low */
4924 __set_bit(msr, msr_bitmap + 0x800 / f);
4925
4926 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4927 msr &= 0x1fff;
4928 if (type & MSR_TYPE_R)
4929 /* read-high */
4930 __set_bit(msr, msr_bitmap + 0x400 / f);
4931
4932 if (type & MSR_TYPE_W)
4933 /* write-high */
4934 __set_bit(msr, msr_bitmap + 0xc00 / f);
4935
Sheng Yang25c5f222008-03-28 13:18:56 +08004936 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004937}
4938
Paolo Bonzini6236b782018-01-16 16:51:18 +01004939static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4940 u32 msr, int type, bool value)
4941{
4942 if (value)
4943 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4944 else
4945 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4946}
4947
Wincy Vanf2b93282015-02-03 23:56:03 +08004948/*
4949 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4950 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4951 */
4952static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4953 unsigned long *msr_bitmap_nested,
4954 u32 msr, int type)
4955{
4956 int f = sizeof(unsigned long);
4957
4958 if (!cpu_has_vmx_msr_bitmap()) {
4959 WARN_ON(1);
4960 return;
4961 }
4962
4963 /*
4964 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4965 * have the write-low and read-high bitmap offsets the wrong way round.
4966 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4967 */
4968 if (msr <= 0x1fff) {
4969 if (type & MSR_TYPE_R &&
4970 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4971 /* read-low */
4972 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4973
4974 if (type & MSR_TYPE_W &&
4975 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4976 /* write-low */
4977 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4978
4979 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4980 msr &= 0x1fff;
4981 if (type & MSR_TYPE_R &&
4982 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4983 /* read-high */
4984 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4985
4986 if (type & MSR_TYPE_W &&
4987 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4988 /* write-high */
4989 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4990
4991 }
4992}
4993
Paolo Bonzini6236b782018-01-16 16:51:18 +01004994static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004995{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004996 u8 mode = 0;
4997
4998 if (cpu_has_secondary_exec_ctrls() &&
4999 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5000 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5001 mode |= MSR_BITMAP_MODE_X2APIC;
5002 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5003 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5004 }
5005
5006 if (is_long_mode(vcpu))
5007 mode |= MSR_BITMAP_MODE_LM;
5008
5009 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005010}
5011
Paolo Bonzini6236b782018-01-16 16:51:18 +01005012#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5013
5014static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5015 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005016{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005017 int msr;
5018
5019 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5020 unsigned word = msr / BITS_PER_LONG;
5021 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5022 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5023 }
5024
5025 if (mode & MSR_BITMAP_MODE_X2APIC) {
5026 /*
5027 * TPR reads and writes can be virtualized even if virtual interrupt
5028 * delivery is not in use.
5029 */
5030 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5031 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5032 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5033 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5034 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5035 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005036 }
Yang Zhang8d146952013-01-25 10:18:50 +08005037}
5038
Paolo Bonzini6236b782018-01-16 16:51:18 +01005039static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005040{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005041 struct vcpu_vmx *vmx = to_vmx(vcpu);
5042 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5043 u8 mode = vmx_msr_bitmap_mode(vcpu);
5044 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005045
Paolo Bonzini6236b782018-01-16 16:51:18 +01005046 if (!changed)
5047 return;
5048
5049 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5050 !(mode & MSR_BITMAP_MODE_LM));
5051
5052 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5053 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5054
5055 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005056}
5057
Andrey Smetanind62caab2015-11-10 15:36:33 +03005058static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005059{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005060 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005061}
5062
David Matlackb7649e12017-08-01 14:00:40 -07005063static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5064{
5065 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5066 gfn_t gfn;
5067
5068 /*
5069 * Don't need to mark the APIC access page dirty; it is never
5070 * written to by the CPU during APIC virtualization.
5071 */
5072
5073 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5074 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5075 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5076 }
5077
5078 if (nested_cpu_has_posted_intr(vmcs12)) {
5079 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5080 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5081 }
5082}
5083
5084
David Hildenbrand1edccf22017-01-25 11:58:58 +01005085static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005086{
5087 struct vcpu_vmx *vmx = to_vmx(vcpu);
5088 int max_irr;
5089 void *vapic_page;
5090 u16 status;
5091
David Matlackb7649e12017-08-01 14:00:40 -07005092 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5093 return;
Wincy Van705699a2015-02-03 23:58:17 +08005094
David Matlackb7649e12017-08-01 14:00:40 -07005095 vmx->nested.pi_pending = false;
5096 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5097 return;
Wincy Van705699a2015-02-03 23:58:17 +08005098
David Matlackb7649e12017-08-01 14:00:40 -07005099 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5100 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005101 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005102 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5103 kunmap(vmx->nested.virtual_apic_page);
5104
5105 status = vmcs_read16(GUEST_INTR_STATUS);
5106 if ((u8)max_irr > ((u8)status & 0xff)) {
5107 status &= ~0xff;
5108 status |= (u8)max_irr;
5109 vmcs_write16(GUEST_INTR_STATUS, status);
5110 }
5111 }
David Matlackb7649e12017-08-01 14:00:40 -07005112
5113 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005114}
5115
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005116static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5117{
5118#ifdef CONFIG_SMP
5119 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005120 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005121 * The vector of interrupt to be delivered to vcpu had
5122 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005123 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005124 * Following cases will be reached in this block, and
5125 * we always send a notification event in all cases as
5126 * explained below.
5127 *
5128 * Case 1: vcpu keeps in non-root mode. Sending a
5129 * notification event posts the interrupt to vcpu.
5130 *
5131 * Case 2: vcpu exits to root mode and is still
5132 * runnable. PIR will be synced to vIRR before the
5133 * next vcpu entry. Sending a notification event in
5134 * this case has no effect, as vcpu is not in root
5135 * mode.
5136 *
5137 * Case 3: vcpu exits to root mode and is blocked.
5138 * vcpu_block() has already synced PIR to vIRR and
5139 * never blocks vcpu if vIRR is not cleared. Therefore,
5140 * a blocked vcpu here does not wait for any requested
5141 * interrupts in PIR, and sending a notification event
5142 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005143 */
Feng Wu28b835d2015-09-18 22:29:54 +08005144
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005145 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5146 POSTED_INTR_VECTOR);
5147 return true;
5148 }
5149#endif
5150 return false;
5151}
5152
Wincy Van705699a2015-02-03 23:58:17 +08005153static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5154 int vector)
5155{
5156 struct vcpu_vmx *vmx = to_vmx(vcpu);
5157
5158 if (is_guest_mode(vcpu) &&
5159 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005160 /*
5161 * If a posted intr is not recognized by hardware,
5162 * we will accomplish it in the next vmentry.
5163 */
5164 vmx->nested.pi_pending = true;
5165 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005166 /* the PIR and ON have been set by L1. */
5167 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5168 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005169 return 0;
5170 }
5171 return -1;
5172}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005173/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005174 * Send interrupt to vcpu via posted interrupt way.
5175 * 1. If target vcpu is running(non-root mode), send posted interrupt
5176 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5177 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5178 * interrupt from PIR in next vmentry.
5179 */
5180static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5181{
5182 struct vcpu_vmx *vmx = to_vmx(vcpu);
5183 int r;
5184
Wincy Van705699a2015-02-03 23:58:17 +08005185 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5186 if (!r)
5187 return;
5188
Yang Zhanga20ed542013-04-11 19:25:15 +08005189 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5190 return;
5191
5192 r = pi_test_and_set_on(&vmx->pi_desc);
5193 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005194 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005195 kvm_vcpu_kick(vcpu);
5196}
5197
5198static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5199{
5200 struct vcpu_vmx *vmx = to_vmx(vcpu);
5201
5202 if (!pi_test_and_clear_on(&vmx->pi_desc))
5203 return;
5204
5205 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5206}
5207
Avi Kivity6aa8b732006-12-10 02:21:36 -08005208/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005209 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5210 * will not change in the lifetime of the guest.
5211 * Note that host-state that does change is set elsewhere. E.g., host-state
5212 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5213 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005214static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005215{
5216 u32 low32, high32;
5217 unsigned long tmpl;
5218 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005219 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005220
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005221 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005222 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5223
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005224 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005225 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005226 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5227 vmx->host_state.vmcs_host_cr4 = cr4;
5228
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005229 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005230#ifdef CONFIG_X86_64
5231 /*
5232 * Load null selectors, so we can avoid reloading them in
5233 * __vmx_load_host_state(), in case userspace uses the null selectors
5234 * too (the expected case).
5235 */
5236 vmcs_write16(HOST_DS_SELECTOR, 0);
5237 vmcs_write16(HOST_ES_SELECTOR, 0);
5238#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005239 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5240 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005241#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005242 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5243 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5244
5245 native_store_idt(&dt);
5246 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005247 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005248
Avi Kivity83287ea422012-09-16 15:10:57 +03005249 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005250
5251 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5252 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5253 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5254 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5255
5256 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5257 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5258 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5259 }
5260}
5261
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005262static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5263{
5264 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5265 if (enable_ept)
5266 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005267 if (is_guest_mode(&vmx->vcpu))
5268 vmx->vcpu.arch.cr4_guest_owned_bits &=
5269 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005270 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5271}
5272
Yang Zhang01e439b2013-04-11 19:25:12 +08005273static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5274{
5275 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5276
Andrey Smetanind62caab2015-11-10 15:36:33 +03005277 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005278 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005279 /* Enable the preemption timer dynamically */
5280 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005281 return pin_based_exec_ctrl;
5282}
5283
Andrey Smetanind62caab2015-11-10 15:36:33 +03005284static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5285{
5286 struct vcpu_vmx *vmx = to_vmx(vcpu);
5287
5288 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005289 if (cpu_has_secondary_exec_ctrls()) {
5290 if (kvm_vcpu_apicv_active(vcpu))
5291 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5292 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5293 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5294 else
5295 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5296 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5297 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5298 }
5299
5300 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005301 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005302}
5303
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005304static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5305{
5306 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005307
5308 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5309 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5310
Paolo Bonzini35754c92015-07-29 12:05:37 +02005311 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005312 exec_control &= ~CPU_BASED_TPR_SHADOW;
5313#ifdef CONFIG_X86_64
5314 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5315 CPU_BASED_CR8_LOAD_EXITING;
5316#endif
5317 }
5318 if (!enable_ept)
5319 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5320 CPU_BASED_CR3_LOAD_EXITING |
5321 CPU_BASED_INVLPG_EXITING;
5322 return exec_control;
5323}
5324
5325static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5326{
5327 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005328 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005329 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5330 if (vmx->vpid == 0)
5331 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5332 if (!enable_ept) {
5333 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5334 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005335 /* Enable INVPCID for non-ept guests may cause performance regression. */
5336 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005337 }
5338 if (!enable_unrestricted_guest)
5339 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5340 if (!ple_gap)
5341 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005342 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005343 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5344 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005345 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005346 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5347 (handle_vmptrld).
5348 We can NOT enable shadow_vmcs here because we don't have yet
5349 a current VMCS12
5350 */
5351 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005352
5353 if (!enable_pml)
5354 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005355
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005356 return exec_control;
5357}
5358
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005359static void ept_set_mmio_spte_mask(void)
5360{
5361 /*
5362 * EPT Misconfigurations can be generated if the value of bits 2:0
5363 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005364 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005365 * spte.
5366 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005367 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005368}
5369
Wanpeng Lif53cd632014-12-02 19:14:58 +08005370#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005371/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005372 * Sets up the vmcs for emulated real mode.
5373 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005374static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005375{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005376#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005377 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005378#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005380
Avi Kivity6aa8b732006-12-10 02:21:36 -08005381 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005382 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5383 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005384
Abel Gordon4607c2d2013-04-18 14:35:55 +03005385 if (enable_shadow_vmcs) {
5386 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5387 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5388 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005389 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005390 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005391
Avi Kivity6aa8b732006-12-10 02:21:36 -08005392 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5393
Avi Kivity6aa8b732006-12-10 02:21:36 -08005394 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005395 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005396 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005397
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005398 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005399
Dan Williamsdfa169b2016-06-02 11:17:24 -07005400 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005401 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5402 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005403 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005404
Andrey Smetanind62caab2015-11-10 15:36:33 +03005405 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005406 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5407 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5408 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5409 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5410
5411 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005412
Li RongQing0bcf2612015-12-03 13:29:34 +08005413 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005414 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005415 }
5416
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005417 if (ple_gap) {
5418 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005419 vmx->ple_window = ple_window;
5420 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005421 }
5422
Xiao Guangrongc3707952011-07-12 03:28:04 +08005423 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5424 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005425 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5426
Avi Kivity9581d442010-10-19 16:46:55 +02005427 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5428 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005429 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005430#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005431 rdmsrl(MSR_FS_BASE, a);
5432 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5433 rdmsrl(MSR_GS_BASE, a);
5434 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5435#else
5436 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5437 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5438#endif
5439
Eddie Dong2cc51562007-05-21 07:28:09 +03005440 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5441 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005442 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005443 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005444 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005445
Radim Krčmář74545702015-04-27 15:11:25 +02005446 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5447 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005448
Paolo Bonzini03916db2014-07-24 14:21:57 +02005449 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450 u32 index = vmx_msr_index[i];
5451 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005452 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005453
5454 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5455 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005456 if (wrmsr_safe(index, data_low, data_high) < 0)
5457 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005458 vmx->guest_msrs[j].index = i;
5459 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005460 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005461 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005463
Paolo Bonzinif56c8ee2018-08-05 16:07:47 +02005464 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02005465
5466 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005467
5468 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005469 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005470
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005471 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005472 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005473
Wanpeng Lif53cd632014-12-02 19:14:58 +08005474 if (vmx_xsaves_supported())
5475 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5476
Peter Feiner4e595162016-07-07 14:49:58 -07005477 if (enable_pml) {
5478 ASSERT(vmx->pml_pg);
5479 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5480 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5481 }
5482
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005483 return 0;
5484}
5485
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005486static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005487{
5488 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005489 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005490 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005491
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005492 vmx->rmode.vm86_active = 0;
Wanpeng Li7a1eac82018-02-28 14:03:31 +08005493 vcpu->arch.microcode_version = 0x100000000ULL;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005494 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005495
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005496 vmx->soft_vnmi_blocked = 0;
5497
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005498 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005499 kvm_set_cr8(vcpu, 0);
5500
5501 if (!init_event) {
5502 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5503 MSR_IA32_APICBASE_ENABLE;
5504 if (kvm_vcpu_is_reset_bsp(vcpu))
5505 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5506 apic_base_msr.host_initiated = true;
5507 kvm_set_apic_base(vcpu, &apic_base_msr);
5508 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005509
Avi Kivity2fb92db2011-04-27 19:42:18 +03005510 vmx_segment_cache_clear(vmx);
5511
Avi Kivity5706be02008-08-20 15:07:31 +03005512 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005513 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005514 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005515
5516 seg_setup(VCPU_SREG_DS);
5517 seg_setup(VCPU_SREG_ES);
5518 seg_setup(VCPU_SREG_FS);
5519 seg_setup(VCPU_SREG_GS);
5520 seg_setup(VCPU_SREG_SS);
5521
5522 vmcs_write16(GUEST_TR_SELECTOR, 0);
5523 vmcs_writel(GUEST_TR_BASE, 0);
5524 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5525 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5526
5527 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5528 vmcs_writel(GUEST_LDTR_BASE, 0);
5529 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5530 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5531
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005532 if (!init_event) {
5533 vmcs_write32(GUEST_SYSENTER_CS, 0);
5534 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5535 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5536 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5537 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005538
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005539 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005540 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005541
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005542 vmcs_writel(GUEST_GDTR_BASE, 0);
5543 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5544
5545 vmcs_writel(GUEST_IDTR_BASE, 0);
5546 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5547
Anthony Liguori443381a2010-12-06 10:53:38 -06005548 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005549 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005550 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005551
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005552 setup_msrs(vmx);
5553
Avi Kivity6aa8b732006-12-10 02:21:36 -08005554 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5555
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005556 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005557 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005558 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005559 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005560 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005561 vmcs_write32(TPR_THRESHOLD, 0);
5562 }
5563
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005564 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005565
Andrey Smetanind62caab2015-11-10 15:36:33 +03005566 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005567 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5568
Sheng Yang2384d2b2008-01-17 15:14:33 +08005569 if (vmx->vpid != 0)
5570 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5571
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005572 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005573 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005574 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005575 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005576 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005577 vmx_fpu_activate(vcpu);
5578 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005580 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005581}
5582
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005583/*
5584 * In nested virtualization, check if L1 asked to exit on external interrupts.
5585 * For most existing hypervisors, this will always return true.
5586 */
5587static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5588{
5589 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5590 PIN_BASED_EXT_INTR_MASK;
5591}
5592
Bandan Das77b0f5d2014-04-19 18:17:45 -04005593/*
5594 * In nested virtualization, check if L1 has set
5595 * VM_EXIT_ACK_INTR_ON_EXIT
5596 */
5597static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5598{
5599 return get_vmcs12(vcpu)->vm_exit_controls &
5600 VM_EXIT_ACK_INTR_ON_EXIT;
5601}
5602
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005603static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5604{
5605 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5606 PIN_BASED_NMI_EXITING;
5607}
5608
Jan Kiszkac9a79532014-03-07 20:03:15 +01005609static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005610{
5611 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005612
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005613 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5614 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5615 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5616}
5617
Jan Kiszkac9a79532014-03-07 20:03:15 +01005618static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005619{
5620 u32 cpu_based_vm_exec_control;
5621
Jan Kiszkac9a79532014-03-07 20:03:15 +01005622 if (!cpu_has_virtual_nmis() ||
5623 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5624 enable_irq_window(vcpu);
5625 return;
5626 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005627
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005628 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5629 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5630 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5631}
5632
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005633static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005634{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005635 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005636 uint32_t intr;
5637 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005638
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005639 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005640
Avi Kivityfa89a812008-09-01 15:57:51 +03005641 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005642 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005643 int inc_eip = 0;
5644 if (vcpu->arch.interrupt.soft)
5645 inc_eip = vcpu->arch.event_exit_inst_len;
5646 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005647 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005648 return;
5649 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005650 intr = irq | INTR_INFO_VALID_MASK;
5651 if (vcpu->arch.interrupt.soft) {
5652 intr |= INTR_TYPE_SOFT_INTR;
5653 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5654 vmx->vcpu.arch.event_exit_inst_len);
5655 } else
5656 intr |= INTR_TYPE_EXT_INTR;
5657 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005658}
5659
Sheng Yangf08864b2008-05-15 18:23:25 +08005660static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5661{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005662 struct vcpu_vmx *vmx = to_vmx(vcpu);
5663
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005664 if (!is_guest_mode(vcpu)) {
5665 if (!cpu_has_virtual_nmis()) {
5666 /*
5667 * Tracking the NMI-blocked state in software is built upon
5668 * finding the next open IRQ window. This, in turn, depends on
5669 * well-behaving guests: They have to keep IRQs disabled at
5670 * least as long as the NMI handler runs. Otherwise we may
5671 * cause NMI nesting, maybe breaking the guest. But as this is
5672 * highly unlikely, we can live with the residual risk.
5673 */
5674 vmx->soft_vnmi_blocked = 1;
5675 vmx->vnmi_blocked_time = 0;
5676 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005677
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005678 ++vcpu->stat.nmi_injections;
5679 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005680 }
5681
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005682 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005683 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005684 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005685 return;
5686 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005687
Sheng Yangf08864b2008-05-15 18:23:25 +08005688 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5689 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005690}
5691
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005692static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5693{
5694 if (!cpu_has_virtual_nmis())
5695 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005696 if (to_vmx(vcpu)->nmi_known_unmasked)
5697 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005698 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005699}
5700
5701static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5702{
5703 struct vcpu_vmx *vmx = to_vmx(vcpu);
5704
5705 if (!cpu_has_virtual_nmis()) {
5706 if (vmx->soft_vnmi_blocked != masked) {
5707 vmx->soft_vnmi_blocked = masked;
5708 vmx->vnmi_blocked_time = 0;
5709 }
5710 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005711 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005712 if (masked)
5713 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5714 GUEST_INTR_STATE_NMI);
5715 else
5716 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5717 GUEST_INTR_STATE_NMI);
5718 }
5719}
5720
Jan Kiszka2505dc92013-04-14 12:12:47 +02005721static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5722{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005723 if (to_vmx(vcpu)->nested.nested_run_pending)
5724 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005725
Jan Kiszka2505dc92013-04-14 12:12:47 +02005726 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5727 return 0;
5728
5729 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5730 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5731 | GUEST_INTR_STATE_NMI));
5732}
5733
Gleb Natapov78646122009-03-23 12:12:11 +02005734static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5735{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005736 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5737 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005738 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5739 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005740}
5741
Izik Eiduscbc94022007-10-25 00:29:55 +02005742static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5743{
5744 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005745
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005746 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5747 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005748 if (ret)
5749 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005750 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005751 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005752}
5753
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005754static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005755{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005756 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005757 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005758 /*
5759 * Update instruction length as we may reinject the exception
5760 * from user space while in guest debugging mode.
5761 */
5762 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5763 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005764 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005765 return false;
5766 /* fall through */
5767 case DB_VECTOR:
5768 if (vcpu->guest_debug &
5769 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5770 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005771 /* fall through */
5772 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005773 case OF_VECTOR:
5774 case BR_VECTOR:
5775 case UD_VECTOR:
5776 case DF_VECTOR:
5777 case SS_VECTOR:
5778 case GP_VECTOR:
5779 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005780 return true;
5781 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005782 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005783 return false;
5784}
5785
5786static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5787 int vec, u32 err_code)
5788{
5789 /*
5790 * Instruction with address size override prefix opcode 0x67
5791 * Cause the #SS fault with 0 error code in VM86 mode.
5792 */
5793 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5794 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5795 if (vcpu->arch.halt_request) {
5796 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005797 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005798 }
5799 return 1;
5800 }
5801 return 0;
5802 }
5803
5804 /*
5805 * Forward all other exceptions that are valid in real mode.
5806 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5807 * the required debugging infrastructure rework.
5808 */
5809 kvm_queue_exception(vcpu, vec);
5810 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005811}
5812
Andi Kleena0861c02009-06-08 17:37:09 +08005813/*
5814 * Trigger machine check on the host. We assume all the MSRs are already set up
5815 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5816 * We pass a fake environment to the machine check handler because we want
5817 * the guest to be always treated like user space, no matter what context
5818 * it used internally.
5819 */
5820static void kvm_machine_check(void)
5821{
5822#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5823 struct pt_regs regs = {
5824 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5825 .flags = X86_EFLAGS_IF,
5826 };
5827
5828 do_machine_check(&regs, 0);
5829#endif
5830}
5831
Avi Kivity851ba692009-08-24 11:10:17 +03005832static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005833{
5834 /* already handled by vcpu_run */
5835 return 1;
5836}
5837
Avi Kivity851ba692009-08-24 11:10:17 +03005838static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005839{
Avi Kivity1155f762007-11-22 11:30:47 +02005840 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005841 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005842 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005843 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005844 u32 vect_info;
5845 enum emulation_result er;
5846
Avi Kivity1155f762007-11-22 11:30:47 +02005847 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005848 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005849
Andi Kleena0861c02009-06-08 17:37:09 +08005850 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005851 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005852
Jim Mattson3f618a02016-12-12 11:01:37 -08005853 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005854 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005855
5856 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005857 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005858 return 1;
5859 }
5860
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005861 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005862 if (is_guest_mode(vcpu)) {
5863 kvm_queue_exception(vcpu, UD_VECTOR);
5864 return 1;
5865 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005866 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005867 if (er == EMULATE_USER_EXIT)
5868 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005869 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005870 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005871 return 1;
5872 }
5873
Avi Kivity6aa8b732006-12-10 02:21:36 -08005874 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005875 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005876 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005877
5878 /*
5879 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5880 * MMIO, it is better to report an internal error.
5881 * See the comments in vmx_handle_exit.
5882 */
5883 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5884 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5885 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5886 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005887 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005888 vcpu->run->internal.data[0] = vect_info;
5889 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005890 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005891 return 0;
5892 }
5893
Avi Kivity6aa8b732006-12-10 02:21:36 -08005894 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005895 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005896 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005897 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005898 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005899 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005900
Gleb Natapov3298b752009-05-11 13:35:46 +03005901 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005902 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005903 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005904 }
5905
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005906 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005907
5908 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5909 return handle_rmode_exception(vcpu, ex_no, error_code);
5910
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005911 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005912 case AC_VECTOR:
5913 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5914 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005915 case DB_VECTOR:
5916 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5917 if (!(vcpu->guest_debug &
5918 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005919 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005920 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005921 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005922 skip_emulated_instruction(vcpu);
5923
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005924 kvm_queue_exception(vcpu, DB_VECTOR);
5925 return 1;
5926 }
5927 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5928 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5929 /* fall through */
5930 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005931 /*
5932 * Update instruction length as we may reinject #BP from
5933 * user space while in guest debugging mode. Reading it for
5934 * #DB as well causes no harm, it is not used in that case.
5935 */
5936 vmx->vcpu.arch.event_exit_inst_len =
5937 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005938 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005939 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005940 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5941 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005942 break;
5943 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005944 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5945 kvm_run->ex.exception = ex_no;
5946 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005947 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005948 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005949 return 0;
5950}
5951
Avi Kivity851ba692009-08-24 11:10:17 +03005952static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005953{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005954 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005955 return 1;
5956}
5957
Avi Kivity851ba692009-08-24 11:10:17 +03005958static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005959{
Avi Kivity851ba692009-08-24 11:10:17 +03005960 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005961 return 0;
5962}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005963
Avi Kivity851ba692009-08-24 11:10:17 +03005964static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005965{
He, Qingbfdaab02007-09-12 14:18:28 +08005966 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005967 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005968 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005969
He, Qingbfdaab02007-09-12 14:18:28 +08005970 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005971 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005972 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005973
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005974 ++vcpu->stat.io_exits;
5975
5976 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005977 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005978
5979 port = exit_qualification >> 16;
5980 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005981 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005982
5983 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005984}
5985
Ingo Molnar102d8322007-02-19 14:37:47 +02005986static void
5987vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5988{
5989 /*
5990 * Patch in the VMCALL instruction:
5991 */
5992 hypercall[0] = 0x0f;
5993 hypercall[1] = 0x01;
5994 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005995}
5996
Wincy Vanb9c237b2015-02-03 23:56:30 +08005997static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005998{
5999 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08006000 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006001
Wincy Vanb9c237b2015-02-03 23:56:30 +08006002 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006003 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
6004 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
6005 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
6006 return (val & always_on) == always_on;
6007}
6008
Guo Chao0fa06072012-06-28 15:16:19 +08006009/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006010static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6011{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006012 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006013 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6014 unsigned long orig_val = val;
6015
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006016 /*
6017 * We get here when L2 changed cr0 in a way that did not change
6018 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006019 * but did change L0 shadowed bits. So we first calculate the
6020 * effective cr0 value that L1 would like to write into the
6021 * hardware. It consists of the L2-owned bits from the new
6022 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006023 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006024 val = (val & ~vmcs12->cr0_guest_host_mask) |
6025 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6026
Wincy Vanb9c237b2015-02-03 23:56:30 +08006027 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006028 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006029
6030 if (kvm_set_cr0(vcpu, val))
6031 return 1;
6032 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006033 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006034 } else {
6035 if (to_vmx(vcpu)->nested.vmxon &&
6036 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
6037 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006038 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006039 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006040}
6041
6042static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6043{
6044 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006045 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6046 unsigned long orig_val = val;
6047
6048 /* analogously to handle_set_cr0 */
6049 val = (val & ~vmcs12->cr4_guest_host_mask) |
6050 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6051 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006052 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006053 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006054 return 0;
6055 } else
6056 return kvm_set_cr4(vcpu, val);
6057}
6058
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08006059/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006060static void handle_clts(struct kvm_vcpu *vcpu)
6061{
6062 if (is_guest_mode(vcpu)) {
6063 /*
6064 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
6065 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
6066 * just pretend it's off (also in arch.cr0 for fpu_activate).
6067 */
6068 vmcs_writel(CR0_READ_SHADOW,
6069 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
6070 vcpu->arch.cr0 &= ~X86_CR0_TS;
6071 } else
6072 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6073}
6074
Avi Kivity851ba692009-08-24 11:10:17 +03006075static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006076{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006077 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006078 int cr;
6079 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006080 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006081
He, Qingbfdaab02007-09-12 14:18:28 +08006082 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006083 cr = exit_qualification & 15;
6084 reg = (exit_qualification >> 8) & 15;
6085 switch ((exit_qualification >> 4) & 3) {
6086 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006087 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006088 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006089 switch (cr) {
6090 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006091 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006092 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006093 return 1;
6094 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006095 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006096 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006097 return 1;
6098 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006099 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006100 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006101 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006102 case 8: {
6103 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006104 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006105 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006106 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006107 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006108 return 1;
6109 if (cr8_prev <= cr8)
6110 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03006111 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006112 return 0;
6113 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006114 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006115 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006116 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006117 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006118 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006119 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006120 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006121 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006122 case 1: /*mov from cr*/
6123 switch (cr) {
6124 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006125 val = kvm_read_cr3(vcpu);
6126 kvm_register_write(vcpu, reg, val);
6127 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006128 skip_emulated_instruction(vcpu);
6129 return 1;
6130 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006131 val = kvm_get_cr8(vcpu);
6132 kvm_register_write(vcpu, reg, val);
6133 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006134 skip_emulated_instruction(vcpu);
6135 return 1;
6136 }
6137 break;
6138 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006139 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006140 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006141 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006142
6143 skip_emulated_instruction(vcpu);
6144 return 1;
6145 default:
6146 break;
6147 }
Avi Kivity851ba692009-08-24 11:10:17 +03006148 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006149 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006150 (int)(exit_qualification >> 4) & 3, cr);
6151 return 0;
6152}
6153
Avi Kivity851ba692009-08-24 11:10:17 +03006154static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006155{
He, Qingbfdaab02007-09-12 14:18:28 +08006156 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006157 int dr, dr7, reg;
6158
6159 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6160 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6161
6162 /* First, if DR does not exist, trigger UD */
6163 if (!kvm_require_dr(vcpu, dr))
6164 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006165
Jan Kiszkaf2483412010-01-20 18:20:20 +01006166 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006167 if (!kvm_require_cpl(vcpu, 0))
6168 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006169 dr7 = vmcs_readl(GUEST_DR7);
6170 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006171 /*
6172 * As the vm-exit takes precedence over the debug trap, we
6173 * need to emulate the latter, either for the host or the
6174 * guest debugging itself.
6175 */
6176 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006177 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006178 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006179 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006180 vcpu->run->debug.arch.exception = DB_VECTOR;
6181 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006182 return 0;
6183 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006184 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006185 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006186 kvm_queue_exception(vcpu, DB_VECTOR);
6187 return 1;
6188 }
6189 }
6190
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006191 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006192 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6193 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006194
6195 /*
6196 * No more DR vmexits; force a reload of the debug registers
6197 * and reenter on this instruction. The next vmexit will
6198 * retrieve the full state of the debug registers.
6199 */
6200 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6201 return 1;
6202 }
6203
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006204 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6205 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006206 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006207
6208 if (kvm_get_dr(vcpu, dr, &val))
6209 return 1;
6210 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006211 } else
Nadav Amit57773922014-06-18 17:19:23 +03006212 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006213 return 1;
6214
Avi Kivity6aa8b732006-12-10 02:21:36 -08006215 skip_emulated_instruction(vcpu);
6216 return 1;
6217}
6218
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006219static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6220{
6221 return vcpu->arch.dr6;
6222}
6223
6224static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6225{
6226}
6227
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006228static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6229{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006230 get_debugreg(vcpu->arch.db[0], 0);
6231 get_debugreg(vcpu->arch.db[1], 1);
6232 get_debugreg(vcpu->arch.db[2], 2);
6233 get_debugreg(vcpu->arch.db[3], 3);
6234 get_debugreg(vcpu->arch.dr6, 6);
6235 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6236
6237 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006238 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006239}
6240
Gleb Natapov020df072010-04-13 10:05:23 +03006241static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6242{
6243 vmcs_writel(GUEST_DR7, val);
6244}
6245
Avi Kivity851ba692009-08-24 11:10:17 +03006246static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006247{
Avi Kivity06465c52007-02-28 20:46:53 +02006248 kvm_emulate_cpuid(vcpu);
6249 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006250}
6251
Avi Kivity851ba692009-08-24 11:10:17 +03006252static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006253{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006254 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006255 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006256
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006257 msr_info.index = ecx;
6258 msr_info.host_initiated = false;
6259 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006260 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006261 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006262 return 1;
6263 }
6264
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006265 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006266
Avi Kivity6aa8b732006-12-10 02:21:36 -08006267 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006268 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6269 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006270 skip_emulated_instruction(vcpu);
6271 return 1;
6272}
6273
Avi Kivity851ba692009-08-24 11:10:17 +03006274static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006275{
Will Auld8fe8ab42012-11-29 12:42:12 -08006276 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006277 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6278 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6279 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006280
Will Auld8fe8ab42012-11-29 12:42:12 -08006281 msr.data = data;
6282 msr.index = ecx;
6283 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006284 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006285 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006286 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006287 return 1;
6288 }
6289
Avi Kivity59200272010-01-25 19:47:02 +02006290 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006291 skip_emulated_instruction(vcpu);
6292 return 1;
6293}
6294
Avi Kivity851ba692009-08-24 11:10:17 +03006295static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006296{
Avi Kivity3842d132010-07-27 12:30:24 +03006297 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006298 return 1;
6299}
6300
Avi Kivity851ba692009-08-24 11:10:17 +03006301static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006302{
Eddie Dong85f455f2007-07-06 12:20:49 +03006303 u32 cpu_based_vm_exec_control;
6304
6305 /* clear pending irq */
6306 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6307 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6308 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006309
Avi Kivity3842d132010-07-27 12:30:24 +03006310 kvm_make_request(KVM_REQ_EVENT, vcpu);
6311
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006312 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006313 return 1;
6314}
6315
Avi Kivity851ba692009-08-24 11:10:17 +03006316static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317{
Avi Kivityd3bef152007-06-05 15:53:05 +03006318 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006319}
6320
Avi Kivity851ba692009-08-24 11:10:17 +03006321static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006322{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006323 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006324}
6325
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006326static int handle_invd(struct kvm_vcpu *vcpu)
6327{
Andre Przywara51d8b662010-12-21 11:12:02 +01006328 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006329}
6330
Avi Kivity851ba692009-08-24 11:10:17 +03006331static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006332{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006333 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006334
6335 kvm_mmu_invlpg(vcpu, exit_qualification);
6336 skip_emulated_instruction(vcpu);
6337 return 1;
6338}
6339
Avi Kivityfee84b02011-11-10 14:57:25 +02006340static int handle_rdpmc(struct kvm_vcpu *vcpu)
6341{
6342 int err;
6343
6344 err = kvm_rdpmc(vcpu);
6345 kvm_complete_insn_gp(vcpu, err);
6346
6347 return 1;
6348}
6349
Avi Kivity851ba692009-08-24 11:10:17 +03006350static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006351{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006352 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006353 return 1;
6354}
6355
Dexuan Cui2acf9232010-06-10 11:27:12 +08006356static int handle_xsetbv(struct kvm_vcpu *vcpu)
6357{
6358 u64 new_bv = kvm_read_edx_eax(vcpu);
6359 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6360
6361 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6362 skip_emulated_instruction(vcpu);
6363 return 1;
6364}
6365
Wanpeng Lif53cd632014-12-02 19:14:58 +08006366static int handle_xsaves(struct kvm_vcpu *vcpu)
6367{
6368 skip_emulated_instruction(vcpu);
6369 WARN(1, "this should never happen\n");
6370 return 1;
6371}
6372
6373static int handle_xrstors(struct kvm_vcpu *vcpu)
6374{
6375 skip_emulated_instruction(vcpu);
6376 WARN(1, "this should never happen\n");
6377 return 1;
6378}
6379
Avi Kivity851ba692009-08-24 11:10:17 +03006380static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006381{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006382 if (likely(fasteoi)) {
6383 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6384 int access_type, offset;
6385
6386 access_type = exit_qualification & APIC_ACCESS_TYPE;
6387 offset = exit_qualification & APIC_ACCESS_OFFSET;
6388 /*
6389 * Sane guest uses MOV to write EOI, with written value
6390 * not cared. So make a short-circuit here by avoiding
6391 * heavy instruction emulation.
6392 */
6393 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6394 (offset == APIC_EOI)) {
6395 kvm_lapic_set_eoi(vcpu);
6396 skip_emulated_instruction(vcpu);
6397 return 1;
6398 }
6399 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006400 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006401}
6402
Yang Zhangc7c9c562013-01-25 10:18:51 +08006403static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6404{
6405 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6406 int vector = exit_qualification & 0xff;
6407
6408 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6409 kvm_apic_set_eoi_accelerated(vcpu, vector);
6410 return 1;
6411}
6412
Yang Zhang83d4c282013-01-25 10:18:49 +08006413static int handle_apic_write(struct kvm_vcpu *vcpu)
6414{
6415 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6416 u32 offset = exit_qualification & 0xfff;
6417
6418 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6419 kvm_apic_write_nodecode(vcpu, offset);
6420 return 1;
6421}
6422
Avi Kivity851ba692009-08-24 11:10:17 +03006423static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006424{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006426 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006427 bool has_error_code = false;
6428 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006429 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006430 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006431
6432 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006433 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006434 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006435
6436 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6437
6438 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006439 if (reason == TASK_SWITCH_GATE && idt_v) {
6440 switch (type) {
6441 case INTR_TYPE_NMI_INTR:
6442 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006443 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006444 break;
6445 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006446 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006447 kvm_clear_interrupt_queue(vcpu);
6448 break;
6449 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006450 if (vmx->idt_vectoring_info &
6451 VECTORING_INFO_DELIVER_CODE_MASK) {
6452 has_error_code = true;
6453 error_code =
6454 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6455 }
6456 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006457 case INTR_TYPE_SOFT_EXCEPTION:
6458 kvm_clear_exception_queue(vcpu);
6459 break;
6460 default:
6461 break;
6462 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006463 }
Izik Eidus37817f22008-03-24 23:14:53 +02006464 tss_selector = exit_qualification;
6465
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006466 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6467 type != INTR_TYPE_EXT_INTR &&
6468 type != INTR_TYPE_NMI_INTR))
6469 skip_emulated_instruction(vcpu);
6470
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006471 if (kvm_task_switch(vcpu, tss_selector,
6472 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6473 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006474 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6475 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6476 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006477 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006478 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006479
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006480 /*
6481 * TODO: What about debug traps on tss switch?
6482 * Are we supposed to inject them and update dr6?
6483 */
6484
6485 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006486}
6487
Avi Kivity851ba692009-08-24 11:10:17 +03006488static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006489{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006490 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006491 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006492 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006493 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006494
Sheng Yangf9c617f2009-03-25 10:08:52 +08006495 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006496
Sheng Yang14394422008-04-28 12:24:45 +08006497 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006498 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006499 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6500 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6501 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006502 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006503 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6504 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006505 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6506 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006507 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006508 }
6509
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006510 /*
6511 * EPT violation happened while executing iret from NMI,
6512 * "blocked by NMI" bit has to be set before next VM entry.
6513 * There are errata that may cause this bit to not be set:
6514 * AAK134, BY25.
6515 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006516 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6517 cpu_has_virtual_nmis() &&
6518 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006519 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6520
Sheng Yang14394422008-04-28 12:24:45 +08006521 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006522 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006523
Bandan Dasd95c5562016-07-12 18:18:51 -04006524 /* it is a read fault? */
6525 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6526 /* it is a write fault? */
6527 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006528 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006529 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006530 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006531 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006532
Yang Zhang25d92082013-08-06 12:00:32 +03006533 vcpu->arch.exit_qualification = exit_qualification;
6534
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006535 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006536}
6537
Avi Kivity851ba692009-08-24 11:10:17 +03006538static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006539{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006540 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006541 gpa_t gpa;
6542
6543 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006544 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006545 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006546 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006547 return 1;
6548 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006549
Paolo Bonzini450869d2015-11-04 13:41:21 +01006550 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006551 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006552 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6553 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006554
6555 if (unlikely(ret == RET_MMIO_PF_INVALID))
6556 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6557
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006558 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006559 return 1;
6560
6561 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006562 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006563
Avi Kivity851ba692009-08-24 11:10:17 +03006564 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6565 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006566
6567 return 0;
6568}
6569
Avi Kivity851ba692009-08-24 11:10:17 +03006570static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006571{
6572 u32 cpu_based_vm_exec_control;
6573
6574 /* clear pending NMI */
6575 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6576 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6577 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6578 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006579 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006580
6581 return 1;
6582}
6583
Mohammed Gamal80ced182009-09-01 12:48:18 +02006584static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006585{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006586 struct vcpu_vmx *vmx = to_vmx(vcpu);
6587 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006588 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006589 u32 cpu_exec_ctrl;
6590 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006591 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006592
6593 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6594 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006595
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006596 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006597 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006598 return handle_interrupt_window(&vmx->vcpu);
6599
Avi Kivityde87dcd2012-06-12 20:21:38 +03006600 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6601 return 1;
6602
Liran Alon114de9b2017-11-05 16:56:34 +02006603 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006604
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006605 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006606 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006607 ret = 0;
6608 goto out;
6609 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006610
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006611 if (err != EMULATE_DONE)
6612 goto emulation_error;
6613
6614 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6615 vcpu->arch.exception.pending)
6616 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006617
Gleb Natapov8d76c492013-05-08 18:38:44 +03006618 if (vcpu->arch.halt_request) {
6619 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006620 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006621 goto out;
6622 }
6623
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006624 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006625 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006626 if (need_resched())
6627 schedule();
6628 }
6629
Mohammed Gamal80ced182009-09-01 12:48:18 +02006630out:
6631 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006632
6633emulation_error:
6634 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6635 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6636 vcpu->run->internal.ndata = 0;
6637 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006638}
6639
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006640static int __grow_ple_window(int val)
6641{
6642 if (ple_window_grow < 1)
6643 return ple_window;
6644
6645 val = min(val, ple_window_actual_max);
6646
6647 if (ple_window_grow < ple_window)
6648 val *= ple_window_grow;
6649 else
6650 val += ple_window_grow;
6651
6652 return val;
6653}
6654
6655static int __shrink_ple_window(int val, int modifier, int minimum)
6656{
6657 if (modifier < 1)
6658 return ple_window;
6659
6660 if (modifier < ple_window)
6661 val /= modifier;
6662 else
6663 val -= modifier;
6664
6665 return max(val, minimum);
6666}
6667
6668static void grow_ple_window(struct kvm_vcpu *vcpu)
6669{
6670 struct vcpu_vmx *vmx = to_vmx(vcpu);
6671 int old = vmx->ple_window;
6672
6673 vmx->ple_window = __grow_ple_window(old);
6674
6675 if (vmx->ple_window != old)
6676 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006677
6678 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006679}
6680
6681static void shrink_ple_window(struct kvm_vcpu *vcpu)
6682{
6683 struct vcpu_vmx *vmx = to_vmx(vcpu);
6684 int old = vmx->ple_window;
6685
6686 vmx->ple_window = __shrink_ple_window(old,
6687 ple_window_shrink, ple_window);
6688
6689 if (vmx->ple_window != old)
6690 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006691
6692 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006693}
6694
6695/*
6696 * ple_window_actual_max is computed to be one grow_ple_window() below
6697 * ple_window_max. (See __grow_ple_window for the reason.)
6698 * This prevents overflows, because ple_window_max is int.
6699 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6700 * this process.
6701 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6702 */
6703static void update_ple_window_actual_max(void)
6704{
6705 ple_window_actual_max =
6706 __shrink_ple_window(max(ple_window_max, ple_window),
6707 ple_window_grow, INT_MIN);
6708}
6709
Feng Wubf9f6ac2015-09-18 22:29:55 +08006710/*
6711 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6712 */
6713static void wakeup_handler(void)
6714{
6715 struct kvm_vcpu *vcpu;
6716 int cpu = smp_processor_id();
6717
6718 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6719 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6720 blocked_vcpu_list) {
6721 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6722
6723 if (pi_test_on(pi_desc) == 1)
6724 kvm_vcpu_kick(vcpu);
6725 }
6726 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6727}
6728
Tiejun Chenf2c76482014-10-28 10:14:47 +08006729static __init int hardware_setup(void)
6730{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006731 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006732
6733 rdmsrl_safe(MSR_EFER, &host_efer);
6734
6735 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6736 kvm_define_shared_msr(i, vmx_msr_index[i]);
6737
6738 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6739 if (!vmx_io_bitmap_a)
6740 return r;
6741
6742 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6743 if (!vmx_io_bitmap_b)
6744 goto out;
6745
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006746 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6747 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006748 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006749
6750 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6751 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006752 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006753
6754 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6755 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6756
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006757 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006758
6759 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6760
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006761 if (setup_vmcs_config(&vmcs_config) < 0) {
6762 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006763 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006764 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006765
6766 if (boot_cpu_has(X86_FEATURE_NX))
6767 kvm_enable_efer_bits(EFER_NX);
6768
Wanpeng Li2df19692017-03-23 05:30:08 -07006769 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6770 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006771 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006772
Tiejun Chenf2c76482014-10-28 10:14:47 +08006773 if (!cpu_has_vmx_shadow_vmcs())
6774 enable_shadow_vmcs = 0;
6775 if (enable_shadow_vmcs)
6776 init_vmcs_shadow_fields();
6777
6778 if (!cpu_has_vmx_ept() ||
6779 !cpu_has_vmx_ept_4levels()) {
6780 enable_ept = 0;
6781 enable_unrestricted_guest = 0;
6782 enable_ept_ad_bits = 0;
6783 }
6784
6785 if (!cpu_has_vmx_ept_ad_bits())
6786 enable_ept_ad_bits = 0;
6787
6788 if (!cpu_has_vmx_unrestricted_guest())
6789 enable_unrestricted_guest = 0;
6790
Paolo Bonziniad15a292015-01-30 16:18:49 +01006791 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006792 flexpriority_enabled = 0;
6793
Paolo Bonziniad15a292015-01-30 16:18:49 +01006794 /*
6795 * set_apic_access_page_addr() is used to reload apic access
6796 * page upon invalidation. No need to do anything if not
6797 * using the APIC_ACCESS_ADDR VMCS field.
6798 */
6799 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006800 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006801
6802 if (!cpu_has_vmx_tpr_shadow())
6803 kvm_x86_ops->update_cr8_intercept = NULL;
6804
6805 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6806 kvm_disable_largepages();
6807
6808 if (!cpu_has_vmx_ple())
6809 ple_gap = 0;
6810
6811 if (!cpu_has_vmx_apicv())
6812 enable_apicv = 0;
6813
Haozhong Zhang64903d62015-10-20 15:39:09 +08006814 if (cpu_has_vmx_tsc_scaling()) {
6815 kvm_has_tsc_control = true;
6816 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6817 kvm_tsc_scaling_ratio_frac_bits = 48;
6818 }
6819
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006820 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6821
Tiejun Chenbaa03522014-12-23 16:21:11 +08006822 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006823 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006824 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6825 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006826 0ull, VMX_EPT_EXECUTABLE_MASK,
6827 cpu_has_vmx_ept_execute_only() ?
6828 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006829 ept_set_mmio_spte_mask();
6830 kvm_enable_tdp();
6831 } else
6832 kvm_disable_tdp();
6833
6834 update_ple_window_actual_max();
6835
Kai Huang843e4332015-01-28 10:54:28 +08006836 /*
6837 * Only enable PML when hardware supports PML feature, and both EPT
6838 * and EPT A/D bit features are enabled -- PML depends on them to work.
6839 */
6840 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6841 enable_pml = 0;
6842
6843 if (!enable_pml) {
6844 kvm_x86_ops->slot_enable_log_dirty = NULL;
6845 kvm_x86_ops->slot_disable_log_dirty = NULL;
6846 kvm_x86_ops->flush_log_dirty = NULL;
6847 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6848 }
6849
Yunhong Jiang64672c92016-06-13 14:19:59 -07006850 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6851 u64 vmx_msr;
6852
6853 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6854 cpu_preemption_timer_multi =
6855 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6856 } else {
6857 kvm_x86_ops->set_hv_timer = NULL;
6858 kvm_x86_ops->cancel_hv_timer = NULL;
6859 }
6860
Feng Wubf9f6ac2015-09-18 22:29:55 +08006861 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6862
Ashok Rajc45dcc72016-06-22 14:59:56 +08006863 kvm_mce_cap_supported |= MCG_LMCE_P;
6864
Tiejun Chenf2c76482014-10-28 10:14:47 +08006865 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006866
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006867out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006868 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006869out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006870 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006871out1:
6872 free_page((unsigned long)vmx_io_bitmap_b);
6873out:
6874 free_page((unsigned long)vmx_io_bitmap_a);
6875
6876 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006877}
6878
6879static __exit void hardware_unsetup(void)
6880{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006881 free_page((unsigned long)vmx_io_bitmap_b);
6882 free_page((unsigned long)vmx_io_bitmap_a);
6883 free_page((unsigned long)vmx_vmwrite_bitmap);
6884 free_page((unsigned long)vmx_vmread_bitmap);
6885
Tiejun Chenf2c76482014-10-28 10:14:47 +08006886 free_kvm_area();
6887}
6888
Avi Kivity6aa8b732006-12-10 02:21:36 -08006889/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006890 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6891 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6892 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006893static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006894{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006895 if (ple_gap)
6896 grow_ple_window(vcpu);
6897
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006898 skip_emulated_instruction(vcpu);
6899 kvm_vcpu_on_spin(vcpu);
6900
6901 return 1;
6902}
6903
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006904static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006905{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006906 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006907 return 1;
6908}
6909
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006910static int handle_mwait(struct kvm_vcpu *vcpu)
6911{
6912 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6913 return handle_nop(vcpu);
6914}
6915
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006916static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6917{
6918 return 1;
6919}
6920
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006921static int handle_monitor(struct kvm_vcpu *vcpu)
6922{
6923 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6924 return handle_nop(vcpu);
6925}
6926
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006927/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006928 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6929 * set the success or error code of an emulated VMX instruction, as specified
6930 * by Vol 2B, VMX Instruction Reference, "Conventions".
6931 */
6932static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6933{
6934 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6935 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6936 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6937}
6938
6939static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6940{
6941 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6942 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6943 X86_EFLAGS_SF | X86_EFLAGS_OF))
6944 | X86_EFLAGS_CF);
6945}
6946
Abel Gordon145c28d2013-04-18 14:36:55 +03006947static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006948 u32 vm_instruction_error)
6949{
6950 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6951 /*
6952 * failValid writes the error number to the current VMCS, which
6953 * can't be done there isn't a current VMCS.
6954 */
6955 nested_vmx_failInvalid(vcpu);
6956 return;
6957 }
6958 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6959 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6960 X86_EFLAGS_SF | X86_EFLAGS_OF))
6961 | X86_EFLAGS_ZF);
6962 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6963 /*
6964 * We don't need to force a shadow sync because
6965 * VM_INSTRUCTION_ERROR is not shadowed
6966 */
6967}
Abel Gordon145c28d2013-04-18 14:36:55 +03006968
Wincy Vanff651cb2014-12-11 08:52:58 +03006969static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6970{
6971 /* TODO: not to reset guest simply here. */
6972 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006973 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006974}
6975
Jan Kiszkaf4124502014-03-07 20:03:13 +01006976static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6977{
6978 struct vcpu_vmx *vmx =
6979 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6980
6981 vmx->nested.preemption_timer_expired = true;
6982 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6983 kvm_vcpu_kick(&vmx->vcpu);
6984
6985 return HRTIMER_NORESTART;
6986}
6987
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006988/*
Bandan Das19677e32014-05-06 02:19:15 -04006989 * Decode the memory-address operand of a vmx instruction, as recorded on an
6990 * exit caused by such an instruction (run by a guest hypervisor).
6991 * On success, returns 0. When the operand is invalid, returns 1 and throws
6992 * #UD or #GP.
6993 */
6994static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6995 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006996 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006997{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006998 gva_t off;
6999 bool exn;
7000 struct kvm_segment s;
7001
Bandan Das19677e32014-05-06 02:19:15 -04007002 /*
7003 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7004 * Execution", on an exit, vmx_instruction_info holds most of the
7005 * addressing components of the operand. Only the displacement part
7006 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7007 * For how an actual address is calculated from all these components,
7008 * refer to Vol. 1, "Operand Addressing".
7009 */
7010 int scaling = vmx_instruction_info & 3;
7011 int addr_size = (vmx_instruction_info >> 7) & 7;
7012 bool is_reg = vmx_instruction_info & (1u << 10);
7013 int seg_reg = (vmx_instruction_info >> 15) & 7;
7014 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7015 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7016 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7017 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7018
7019 if (is_reg) {
7020 kvm_queue_exception(vcpu, UD_VECTOR);
7021 return 1;
7022 }
7023
7024 /* Addr = segment_base + offset */
7025 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007026 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007027 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007028 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007029 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007030 off += kvm_register_read(vcpu, index_reg)<<scaling;
7031 vmx_get_segment(vcpu, &s, seg_reg);
7032 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007033
7034 if (addr_size == 1) /* 32 bit */
7035 *ret &= 0xffffffff;
7036
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007037 /* Checks for #GP/#SS exceptions. */
7038 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007039 if (is_long_mode(vcpu)) {
7040 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7041 * non-canonical form. This is the only check on the memory
7042 * destination for long mode!
7043 */
7044 exn = is_noncanonical_address(*ret);
7045 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007046 /* Protected mode: apply checks for segment validity in the
7047 * following order:
7048 * - segment type check (#GP(0) may be thrown)
7049 * - usability check (#GP(0)/#SS(0))
7050 * - limit check (#GP(0)/#SS(0))
7051 */
7052 if (wr)
7053 /* #GP(0) if the destination operand is located in a
7054 * read-only data segment or any code segment.
7055 */
7056 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7057 else
7058 /* #GP(0) if the source operand is located in an
7059 * execute-only code segment
7060 */
7061 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007062 if (exn) {
7063 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7064 return 1;
7065 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007066 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7067 */
7068 exn = (s.unusable != 0);
7069 /* Protected mode: #GP(0)/#SS(0) if the memory
7070 * operand is outside the segment limit.
7071 */
7072 exn = exn || (off + sizeof(u64) > s.limit);
7073 }
7074 if (exn) {
7075 kvm_queue_exception_e(vcpu,
7076 seg_reg == VCPU_SREG_SS ?
7077 SS_VECTOR : GP_VECTOR,
7078 0);
7079 return 1;
7080 }
7081
Bandan Das19677e32014-05-06 02:19:15 -04007082 return 0;
7083}
7084
7085/*
Bandan Das3573e222014-05-06 02:19:16 -04007086 * This function performs the various checks including
7087 * - if it's 4KB aligned
7088 * - No bits beyond the physical address width are set
7089 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04007090 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04007091 */
Bandan Das4291b582014-05-06 02:19:18 -04007092static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
7093 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007094{
7095 gva_t gva;
7096 gpa_t vmptr;
7097 struct x86_exception e;
7098 struct page *page;
7099 struct vcpu_vmx *vmx = to_vmx(vcpu);
7100 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7101
7102 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007103 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007104 return 1;
7105
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007106 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007107 kvm_inject_page_fault(vcpu, &e);
7108 return 1;
7109 }
7110
7111 switch (exit_reason) {
7112 case EXIT_REASON_VMON:
7113 /*
7114 * SDM 3: 24.11.5
7115 * The first 4 bytes of VMXON region contain the supported
7116 * VMCS revision identifier
7117 *
7118 * Note - IA32_VMX_BASIC[48] will never be 1
7119 * for the nested case;
7120 * which replaces physical address width with 32
7121 *
7122 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007123 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007124 nested_vmx_failInvalid(vcpu);
7125 skip_emulated_instruction(vcpu);
7126 return 1;
7127 }
7128
7129 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007130 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007131 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007132 skip_emulated_instruction(vcpu);
7133 return 1;
7134 }
7135 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007136 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007137 nested_release_page_clean(page);
7138 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007139 skip_emulated_instruction(vcpu);
7140 return 1;
7141 }
7142 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007143 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007144 vmx->nested.vmxon_ptr = vmptr;
7145 break;
Bandan Das4291b582014-05-06 02:19:18 -04007146 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007147 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007148 nested_vmx_failValid(vcpu,
7149 VMXERR_VMCLEAR_INVALID_ADDRESS);
7150 skip_emulated_instruction(vcpu);
7151 return 1;
7152 }
Bandan Das3573e222014-05-06 02:19:16 -04007153
Bandan Das4291b582014-05-06 02:19:18 -04007154 if (vmptr == vmx->nested.vmxon_ptr) {
7155 nested_vmx_failValid(vcpu,
7156 VMXERR_VMCLEAR_VMXON_POINTER);
7157 skip_emulated_instruction(vcpu);
7158 return 1;
7159 }
7160 break;
7161 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007162 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007163 nested_vmx_failValid(vcpu,
7164 VMXERR_VMPTRLD_INVALID_ADDRESS);
7165 skip_emulated_instruction(vcpu);
7166 return 1;
7167 }
7168
7169 if (vmptr == vmx->nested.vmxon_ptr) {
7170 nested_vmx_failValid(vcpu,
7171 VMXERR_VMCLEAR_VMXON_POINTER);
7172 skip_emulated_instruction(vcpu);
7173 return 1;
7174 }
7175 break;
Bandan Das3573e222014-05-06 02:19:16 -04007176 default:
7177 return 1; /* shouldn't happen */
7178 }
7179
Bandan Das4291b582014-05-06 02:19:18 -04007180 if (vmpointer)
7181 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007182 return 0;
7183}
7184
7185/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007186 * Emulate the VMXON instruction.
7187 * Currently, we just remember that VMX is active, and do not save or even
7188 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7189 * do not currently need to store anything in that guest-allocated memory
7190 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7191 * argument is different from the VMXON pointer (which the spec says they do).
7192 */
7193static int handle_vmon(struct kvm_vcpu *vcpu)
7194{
7195 struct kvm_segment cs;
7196 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007197 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007198 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7199 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007200 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007201
7202 /* The Intel VMX Instruction Reference lists a bunch of bits that
7203 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7204 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7205 * Otherwise, we should fail with #UD. We test these now:
7206 */
7207 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7208 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7209 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7210 kvm_queue_exception(vcpu, UD_VECTOR);
7211 return 1;
7212 }
7213
7214 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7215 if (is_long_mode(vcpu) && !cs.l) {
7216 kvm_queue_exception(vcpu, UD_VECTOR);
7217 return 1;
7218 }
7219
7220 if (vmx_get_cpl(vcpu)) {
7221 kvm_inject_gp(vcpu, 0);
7222 return 1;
7223 }
Bandan Das3573e222014-05-06 02:19:16 -04007224
Bandan Das4291b582014-05-06 02:19:18 -04007225 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007226 return 1;
7227
Abel Gordon145c28d2013-04-18 14:36:55 +03007228 if (vmx->nested.vmxon) {
7229 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7230 skip_emulated_instruction(vcpu);
7231 return 1;
7232 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007233
Haozhong Zhang3b840802016-06-22 14:59:54 +08007234 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007235 != VMXON_NEEDED_FEATURES) {
7236 kvm_inject_gp(vcpu, 0);
7237 return 1;
7238 }
7239
Paolo Bonziniff546f92018-01-11 12:16:15 +01007240 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7241 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007242 goto out_vmcs02;
Radim Krčmářd048c092016-08-08 20:16:22 +02007243
David Matlack4f2777b2016-07-13 17:16:37 -07007244 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7245 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007246 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007247
Abel Gordon8de48832013-04-18 14:37:25 +03007248 if (enable_shadow_vmcs) {
7249 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007250 if (!shadow_vmcs)
7251 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007252 /* mark vmcs as shadow */
7253 shadow_vmcs->revision_id |= (1u << 31);
7254 /* init shadow vmcs */
7255 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007256 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007257 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007258
Jan Kiszkaf4124502014-03-07 20:03:13 +01007259 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007260 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007261 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7262
Roman Kagan020a90f2018-07-19 21:59:07 +03007263 vmx->nested.vpid02 = allocate_vpid();
7264
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007265 vmx->nested.vmxon = true;
7266
7267 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007268 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007269 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007270
7271out_shadow_vmcs:
7272 kfree(vmx->nested.cached_vmcs12);
7273
7274out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007275 free_loaded_vmcs(&vmx->nested.vmcs02);
Radim Krčmářd048c092016-08-08 20:16:22 +02007276
Jim Mattson46e24df2017-11-27 17:22:25 -06007277out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007278 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007279}
7280
7281/*
7282 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7283 * for running VMX instructions (except VMXON, whose prerequisites are
7284 * slightly different). It also specifies what exception to inject otherwise.
7285 */
7286static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7287{
7288 struct kvm_segment cs;
7289 struct vcpu_vmx *vmx = to_vmx(vcpu);
7290
7291 if (!vmx->nested.vmxon) {
7292 kvm_queue_exception(vcpu, UD_VECTOR);
7293 return 0;
7294 }
7295
7296 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7297 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7298 (is_long_mode(vcpu) && !cs.l)) {
7299 kvm_queue_exception(vcpu, UD_VECTOR);
7300 return 0;
7301 }
7302
7303 if (vmx_get_cpl(vcpu)) {
7304 kvm_inject_gp(vcpu, 0);
7305 return 0;
7306 }
7307
7308 return 1;
7309}
7310
Abel Gordone7953d72013-04-18 14:37:55 +03007311static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7312{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007313 if (vmx->nested.current_vmptr == -1ull)
7314 return;
7315
7316 /* current_vmptr and current_vmcs12 are always set/reset together */
7317 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7318 return;
7319
Abel Gordon012f83c2013-04-18 14:39:25 +03007320 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007321 /* copy to memory all shadowed fields in case
7322 they were modified */
7323 copy_shadow_to_vmcs12(vmx);
7324 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007325 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7326 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007327 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007328 }
Wincy Van705699a2015-02-03 23:58:17 +08007329 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007330
7331 /* Flush VMCS12 to guest memory */
7332 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7333 VMCS12_SIZE);
7334
Abel Gordone7953d72013-04-18 14:37:55 +03007335 kunmap(vmx->nested.current_vmcs12_page);
7336 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007337 vmx->nested.current_vmptr = -1ull;
7338 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007339}
7340
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007341/*
7342 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7343 * just stops using VMX.
7344 */
7345static void free_nested(struct vcpu_vmx *vmx)
7346{
7347 if (!vmx->nested.vmxon)
7348 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007349
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007350 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007351 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007352 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007353 if (enable_shadow_vmcs) {
7354 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7355 free_vmcs(vmx->vmcs01.shadow_vmcs);
7356 vmx->vmcs01.shadow_vmcs = NULL;
7357 }
David Matlack4f2777b2016-07-13 17:16:37 -07007358 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007359 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007360 if (vmx->nested.apic_access_page) {
7361 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007362 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007363 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007364 if (vmx->nested.virtual_apic_page) {
7365 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007366 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007367 }
Wincy Van705699a2015-02-03 23:58:17 +08007368 if (vmx->nested.pi_desc_page) {
7369 kunmap(vmx->nested.pi_desc_page);
7370 nested_release_page(vmx->nested.pi_desc_page);
7371 vmx->nested.pi_desc_page = NULL;
7372 vmx->nested.pi_desc = NULL;
7373 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007374
Jim Mattson46e24df2017-11-27 17:22:25 -06007375 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007376}
7377
7378/* Emulate the VMXOFF instruction */
7379static int handle_vmoff(struct kvm_vcpu *vcpu)
7380{
7381 if (!nested_vmx_check_permission(vcpu))
7382 return 1;
7383 free_nested(to_vmx(vcpu));
7384 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007385 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007386 return 1;
7387}
7388
Nadav Har'El27d6c862011-05-25 23:06:59 +03007389/* Emulate the VMCLEAR instruction */
7390static int handle_vmclear(struct kvm_vcpu *vcpu)
7391{
7392 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007393 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007394 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007395
7396 if (!nested_vmx_check_permission(vcpu))
7397 return 1;
7398
Bandan Das4291b582014-05-06 02:19:18 -04007399 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007400 return 1;
7401
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007402 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007403 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007404
Jim Mattson29deec42017-03-02 12:41:48 -08007405 kvm_vcpu_write_guest(vcpu,
7406 vmptr + offsetof(struct vmcs12, launch_state),
7407 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007408
Nadav Har'El27d6c862011-05-25 23:06:59 +03007409 skip_emulated_instruction(vcpu);
7410 nested_vmx_succeed(vcpu);
7411 return 1;
7412}
7413
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007414static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7415
7416/* Emulate the VMLAUNCH instruction */
7417static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7418{
7419 return nested_vmx_run(vcpu, true);
7420}
7421
7422/* Emulate the VMRESUME instruction */
7423static int handle_vmresume(struct kvm_vcpu *vcpu)
7424{
7425
7426 return nested_vmx_run(vcpu, false);
7427}
7428
Nadav Har'El49f705c2011-05-25 23:08:30 +03007429enum vmcs_field_type {
7430 VMCS_FIELD_TYPE_U16 = 0,
7431 VMCS_FIELD_TYPE_U64 = 1,
7432 VMCS_FIELD_TYPE_U32 = 2,
7433 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7434};
7435
7436static inline int vmcs_field_type(unsigned long field)
7437{
7438 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7439 return VMCS_FIELD_TYPE_U32;
7440 return (field >> 13) & 0x3 ;
7441}
7442
7443static inline int vmcs_field_readonly(unsigned long field)
7444{
7445 return (((field >> 10) & 0x3) == 1);
7446}
7447
7448/*
7449 * Read a vmcs12 field. Since these can have varying lengths and we return
7450 * one type, we chose the biggest type (u64) and zero-extend the return value
7451 * to that size. Note that the caller, handle_vmread, might need to use only
7452 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7453 * 64-bit fields are to be returned).
7454 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007455static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7456 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007457{
7458 short offset = vmcs_field_to_offset(field);
7459 char *p;
7460
7461 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007462 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007463
7464 p = ((char *)(get_vmcs12(vcpu))) + offset;
7465
7466 switch (vmcs_field_type(field)) {
7467 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7468 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007469 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007470 case VMCS_FIELD_TYPE_U16:
7471 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007472 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007473 case VMCS_FIELD_TYPE_U32:
7474 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007475 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007476 case VMCS_FIELD_TYPE_U64:
7477 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007478 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007479 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007480 WARN_ON(1);
7481 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007482 }
7483}
7484
Abel Gordon20b97fe2013-04-18 14:36:25 +03007485
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007486static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7487 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007488 short offset = vmcs_field_to_offset(field);
7489 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7490 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007491 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007492
7493 switch (vmcs_field_type(field)) {
7494 case VMCS_FIELD_TYPE_U16:
7495 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007496 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007497 case VMCS_FIELD_TYPE_U32:
7498 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007499 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007500 case VMCS_FIELD_TYPE_U64:
7501 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007502 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007503 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7504 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007505 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007506 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007507 WARN_ON(1);
7508 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007509 }
7510
7511}
7512
Abel Gordon16f5b902013-04-18 14:38:25 +03007513static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7514{
7515 int i;
7516 unsigned long field;
7517 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007518 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007519 const unsigned long *fields = shadow_read_write_fields;
7520 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007521
Jan Kiszka282da872014-10-08 18:05:39 +02007522 preempt_disable();
7523
Abel Gordon16f5b902013-04-18 14:38:25 +03007524 vmcs_load(shadow_vmcs);
7525
7526 for (i = 0; i < num_fields; i++) {
7527 field = fields[i];
7528 switch (vmcs_field_type(field)) {
7529 case VMCS_FIELD_TYPE_U16:
7530 field_value = vmcs_read16(field);
7531 break;
7532 case VMCS_FIELD_TYPE_U32:
7533 field_value = vmcs_read32(field);
7534 break;
7535 case VMCS_FIELD_TYPE_U64:
7536 field_value = vmcs_read64(field);
7537 break;
7538 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7539 field_value = vmcs_readl(field);
7540 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007541 default:
7542 WARN_ON(1);
7543 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007544 }
7545 vmcs12_write_any(&vmx->vcpu, field, field_value);
7546 }
7547
7548 vmcs_clear(shadow_vmcs);
7549 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007550
7551 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007552}
7553
Abel Gordonc3114422013-04-18 14:38:55 +03007554static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7555{
Mathias Krausec2bae892013-06-26 20:36:21 +02007556 const unsigned long *fields[] = {
7557 shadow_read_write_fields,
7558 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007559 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007560 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007561 max_shadow_read_write_fields,
7562 max_shadow_read_only_fields
7563 };
7564 int i, q;
7565 unsigned long field;
7566 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007567 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007568
7569 vmcs_load(shadow_vmcs);
7570
Mathias Krausec2bae892013-06-26 20:36:21 +02007571 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007572 for (i = 0; i < max_fields[q]; i++) {
7573 field = fields[q][i];
7574 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7575
7576 switch (vmcs_field_type(field)) {
7577 case VMCS_FIELD_TYPE_U16:
7578 vmcs_write16(field, (u16)field_value);
7579 break;
7580 case VMCS_FIELD_TYPE_U32:
7581 vmcs_write32(field, (u32)field_value);
7582 break;
7583 case VMCS_FIELD_TYPE_U64:
7584 vmcs_write64(field, (u64)field_value);
7585 break;
7586 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7587 vmcs_writel(field, (long)field_value);
7588 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007589 default:
7590 WARN_ON(1);
7591 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007592 }
7593 }
7594 }
7595
7596 vmcs_clear(shadow_vmcs);
7597 vmcs_load(vmx->loaded_vmcs->vmcs);
7598}
7599
Nadav Har'El49f705c2011-05-25 23:08:30 +03007600/*
7601 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7602 * used before) all generate the same failure when it is missing.
7603 */
7604static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7605{
7606 struct vcpu_vmx *vmx = to_vmx(vcpu);
7607 if (vmx->nested.current_vmptr == -1ull) {
7608 nested_vmx_failInvalid(vcpu);
7609 skip_emulated_instruction(vcpu);
7610 return 0;
7611 }
7612 return 1;
7613}
7614
7615static int handle_vmread(struct kvm_vcpu *vcpu)
7616{
7617 unsigned long field;
7618 u64 field_value;
7619 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7620 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7621 gva_t gva = 0;
7622
7623 if (!nested_vmx_check_permission(vcpu) ||
7624 !nested_vmx_check_vmcs12(vcpu))
7625 return 1;
7626
7627 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007628 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007629 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007630 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007631 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7632 skip_emulated_instruction(vcpu);
7633 return 1;
7634 }
7635 /*
7636 * Now copy part of this value to register or memory, as requested.
7637 * Note that the number of bits actually copied is 32 or 64 depending
7638 * on the guest's mode (32 or 64 bit), not on the given field's length.
7639 */
7640 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007641 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007642 field_value);
7643 } else {
7644 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007645 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007646 return 1;
7647 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007648 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7649 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007650 }
7651
7652 nested_vmx_succeed(vcpu);
7653 skip_emulated_instruction(vcpu);
7654 return 1;
7655}
7656
7657
7658static int handle_vmwrite(struct kvm_vcpu *vcpu)
7659{
7660 unsigned long field;
7661 gva_t gva;
7662 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7663 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007664 /* The value to write might be 32 or 64 bits, depending on L1's long
7665 * mode, and eventually we need to write that into a field of several
7666 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007667 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007668 * bits into the vmcs12 field.
7669 */
7670 u64 field_value = 0;
7671 struct x86_exception e;
7672
7673 if (!nested_vmx_check_permission(vcpu) ||
7674 !nested_vmx_check_vmcs12(vcpu))
7675 return 1;
7676
7677 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007678 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007679 (((vmx_instruction_info) >> 3) & 0xf));
7680 else {
7681 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007682 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007683 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007684 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7685 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007686 kvm_inject_page_fault(vcpu, &e);
7687 return 1;
7688 }
7689 }
7690
7691
Nadav Amit27e6fb52014-06-18 17:19:26 +03007692 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007693 if (vmcs_field_readonly(field)) {
7694 nested_vmx_failValid(vcpu,
7695 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7696 skip_emulated_instruction(vcpu);
7697 return 1;
7698 }
7699
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007700 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007701 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7702 skip_emulated_instruction(vcpu);
7703 return 1;
7704 }
7705
7706 nested_vmx_succeed(vcpu);
7707 skip_emulated_instruction(vcpu);
7708 return 1;
7709}
7710
Nadav Har'El63846662011-05-25 23:07:29 +03007711/* Emulate the VMPTRLD instruction */
7712static int handle_vmptrld(struct kvm_vcpu *vcpu)
7713{
7714 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007715 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007716
7717 if (!nested_vmx_check_permission(vcpu))
7718 return 1;
7719
Bandan Das4291b582014-05-06 02:19:18 -04007720 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007721 return 1;
7722
Nadav Har'El63846662011-05-25 23:07:29 +03007723 if (vmx->nested.current_vmptr != vmptr) {
7724 struct vmcs12 *new_vmcs12;
7725 struct page *page;
7726 page = nested_get_page(vcpu, vmptr);
7727 if (page == NULL) {
7728 nested_vmx_failInvalid(vcpu);
7729 skip_emulated_instruction(vcpu);
7730 return 1;
7731 }
7732 new_vmcs12 = kmap(page);
7733 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7734 kunmap(page);
7735 nested_release_page_clean(page);
7736 nested_vmx_failValid(vcpu,
7737 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7738 skip_emulated_instruction(vcpu);
7739 return 1;
7740 }
Nadav Har'El63846662011-05-25 23:07:29 +03007741
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007742 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007743 vmx->nested.current_vmptr = vmptr;
7744 vmx->nested.current_vmcs12 = new_vmcs12;
7745 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007746 /*
7747 * Load VMCS12 from guest memory since it is not already
7748 * cached.
7749 */
7750 memcpy(vmx->nested.cached_vmcs12,
7751 vmx->nested.current_vmcs12, VMCS12_SIZE);
7752
Abel Gordon012f83c2013-04-18 14:39:25 +03007753 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007754 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7755 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007756 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007757 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007758 vmx->nested.sync_shadow_vmcs = true;
7759 }
Nadav Har'El63846662011-05-25 23:07:29 +03007760 }
7761
7762 nested_vmx_succeed(vcpu);
7763 skip_emulated_instruction(vcpu);
7764 return 1;
7765}
7766
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007767/* Emulate the VMPTRST instruction */
7768static int handle_vmptrst(struct kvm_vcpu *vcpu)
7769{
7770 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7771 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7772 gva_t vmcs_gva;
7773 struct x86_exception e;
7774
7775 if (!nested_vmx_check_permission(vcpu))
7776 return 1;
7777
7778 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007779 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007780 return 1;
7781 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007782 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7783 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7784 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007785 kvm_inject_page_fault(vcpu, &e);
7786 return 1;
7787 }
7788 nested_vmx_succeed(vcpu);
7789 skip_emulated_instruction(vcpu);
7790 return 1;
7791}
7792
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007793/* Emulate the INVEPT instruction */
7794static int handle_invept(struct kvm_vcpu *vcpu)
7795{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007796 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007797 u32 vmx_instruction_info, types;
7798 unsigned long type;
7799 gva_t gva;
7800 struct x86_exception e;
7801 struct {
7802 u64 eptp, gpa;
7803 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007804
Wincy Vanb9c237b2015-02-03 23:56:30 +08007805 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7806 SECONDARY_EXEC_ENABLE_EPT) ||
7807 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007808 kvm_queue_exception(vcpu, UD_VECTOR);
7809 return 1;
7810 }
7811
7812 if (!nested_vmx_check_permission(vcpu))
7813 return 1;
7814
7815 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7816 kvm_queue_exception(vcpu, UD_VECTOR);
7817 return 1;
7818 }
7819
7820 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007821 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007822
Wincy Vanb9c237b2015-02-03 23:56:30 +08007823 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007824
Jim Mattson85c856b2016-10-26 08:38:38 -07007825 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007826 nested_vmx_failValid(vcpu,
7827 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007828 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007829 return 1;
7830 }
7831
7832 /* According to the Intel VMX instruction reference, the memory
7833 * operand is read even if it isn't needed (e.g., for type==global)
7834 */
7835 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007836 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007837 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007838 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007839 kvm_inject_page_fault(vcpu, &e);
7840 return 1;
7841 }
7842
7843 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007844 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007845 /*
7846 * TODO: track mappings and invalidate
7847 * single context requests appropriately
7848 */
7849 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007850 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007851 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007852 nested_vmx_succeed(vcpu);
7853 break;
7854 default:
7855 BUG_ON(1);
7856 break;
7857 }
7858
7859 skip_emulated_instruction(vcpu);
7860 return 1;
7861}
7862
Petr Matouseka642fc32014-09-23 20:22:30 +02007863static int handle_invvpid(struct kvm_vcpu *vcpu)
7864{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007865 struct vcpu_vmx *vmx = to_vmx(vcpu);
7866 u32 vmx_instruction_info;
7867 unsigned long type, types;
7868 gva_t gva;
7869 struct x86_exception e;
7870 int vpid;
7871
7872 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7873 SECONDARY_EXEC_ENABLE_VPID) ||
7874 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7875 kvm_queue_exception(vcpu, UD_VECTOR);
7876 return 1;
7877 }
7878
7879 if (!nested_vmx_check_permission(vcpu))
7880 return 1;
7881
7882 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7883 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7884
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007885 types = (vmx->nested.nested_vmx_vpid_caps &
7886 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007887
Jim Mattson85c856b2016-10-26 08:38:38 -07007888 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007889 nested_vmx_failValid(vcpu,
7890 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007891 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007892 return 1;
7893 }
7894
7895 /* according to the intel vmx instruction reference, the memory
7896 * operand is read even if it isn't needed (e.g., for type==global)
7897 */
7898 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7899 vmx_instruction_info, false, &gva))
7900 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007901 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007902 kvm_inject_page_fault(vcpu, &e);
7903 return 1;
7904 }
7905
7906 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007907 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007908 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007909 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7910 if (!vpid) {
7911 nested_vmx_failValid(vcpu,
7912 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7913 skip_emulated_instruction(vcpu);
7914 return 1;
7915 }
7916 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007917 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007918 break;
7919 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007920 WARN_ON_ONCE(1);
7921 skip_emulated_instruction(vcpu);
7922 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007923 }
7924
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007925 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7926 nested_vmx_succeed(vcpu);
7927
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007928 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007929 return 1;
7930}
7931
Kai Huang843e4332015-01-28 10:54:28 +08007932static int handle_pml_full(struct kvm_vcpu *vcpu)
7933{
7934 unsigned long exit_qualification;
7935
7936 trace_kvm_pml_full(vcpu->vcpu_id);
7937
7938 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7939
7940 /*
7941 * PML buffer FULL happened while executing iret from NMI,
7942 * "blocked by NMI" bit has to be set before next VM entry.
7943 */
7944 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7945 cpu_has_virtual_nmis() &&
7946 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7947 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7948 GUEST_INTR_STATE_NMI);
7949
7950 /*
7951 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7952 * here.., and there's no userspace involvement needed for PML.
7953 */
7954 return 1;
7955}
7956
Yunhong Jiang64672c92016-06-13 14:19:59 -07007957static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7958{
7959 kvm_lapic_expired_hv_timer(vcpu);
7960 return 1;
7961}
7962
Nadav Har'El0140cae2011-05-25 23:06:28 +03007963/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007964 * The exit handlers return 1 if the exit was handled fully and guest execution
7965 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7966 * to be done to userspace and return 0.
7967 */
Mathias Krause772e0312012-08-30 01:30:19 +02007968static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007969 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7970 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007971 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007972 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007973 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007974 [EXIT_REASON_CR_ACCESS] = handle_cr,
7975 [EXIT_REASON_DR_ACCESS] = handle_dr,
7976 [EXIT_REASON_CPUID] = handle_cpuid,
7977 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7978 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7979 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7980 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007981 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007982 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007983 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007984 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007985 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007986 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007987 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007988 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007989 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007990 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007991 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007992 [EXIT_REASON_VMOFF] = handle_vmoff,
7993 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007994 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7995 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007996 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007997 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007998 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007999 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008000 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008001 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008002 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8003 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008004 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008005 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008006 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008007 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008008 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008009 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008010 [EXIT_REASON_XSAVES] = handle_xsaves,
8011 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008012 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008013 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008014};
8015
8016static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008017 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008018
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008019static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8020 struct vmcs12 *vmcs12)
8021{
8022 unsigned long exit_qualification;
8023 gpa_t bitmap, last_bitmap;
8024 unsigned int port;
8025 int size;
8026 u8 b;
8027
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008028 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008029 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008030
8031 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8032
8033 port = exit_qualification >> 16;
8034 size = (exit_qualification & 7) + 1;
8035
8036 last_bitmap = (gpa_t)-1;
8037 b = -1;
8038
8039 while (size > 0) {
8040 if (port < 0x8000)
8041 bitmap = vmcs12->io_bitmap_a;
8042 else if (port < 0x10000)
8043 bitmap = vmcs12->io_bitmap_b;
8044 else
Joe Perches1d804d02015-03-30 16:46:09 -07008045 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008046 bitmap += (port & 0x7fff) / 8;
8047
8048 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008049 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008050 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008051 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008052 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008053
8054 port++;
8055 size--;
8056 last_bitmap = bitmap;
8057 }
8058
Joe Perches1d804d02015-03-30 16:46:09 -07008059 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008060}
8061
Nadav Har'El644d7112011-05-25 23:12:35 +03008062/*
8063 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8064 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8065 * disinterest in the current event (read or write a specific MSR) by using an
8066 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8067 */
8068static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8069 struct vmcs12 *vmcs12, u32 exit_reason)
8070{
8071 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8072 gpa_t bitmap;
8073
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008074 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008075 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008076
8077 /*
8078 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8079 * for the four combinations of read/write and low/high MSR numbers.
8080 * First we need to figure out which of the four to use:
8081 */
8082 bitmap = vmcs12->msr_bitmap;
8083 if (exit_reason == EXIT_REASON_MSR_WRITE)
8084 bitmap += 2048;
8085 if (msr_index >= 0xc0000000) {
8086 msr_index -= 0xc0000000;
8087 bitmap += 1024;
8088 }
8089
8090 /* Then read the msr_index'th bit from this bitmap: */
8091 if (msr_index < 1024*8) {
8092 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008093 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008094 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008095 return 1 & (b >> (msr_index & 7));
8096 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008097 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008098}
8099
8100/*
8101 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8102 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8103 * intercept (via guest_host_mask etc.) the current event.
8104 */
8105static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8106 struct vmcs12 *vmcs12)
8107{
8108 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8109 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008110 int reg;
8111 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008112
8113 switch ((exit_qualification >> 4) & 3) {
8114 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008115 reg = (exit_qualification >> 8) & 15;
8116 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008117 switch (cr) {
8118 case 0:
8119 if (vmcs12->cr0_guest_host_mask &
8120 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008121 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008122 break;
8123 case 3:
8124 if ((vmcs12->cr3_target_count >= 1 &&
8125 vmcs12->cr3_target_value0 == val) ||
8126 (vmcs12->cr3_target_count >= 2 &&
8127 vmcs12->cr3_target_value1 == val) ||
8128 (vmcs12->cr3_target_count >= 3 &&
8129 vmcs12->cr3_target_value2 == val) ||
8130 (vmcs12->cr3_target_count >= 4 &&
8131 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008132 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008133 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008134 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008135 break;
8136 case 4:
8137 if (vmcs12->cr4_guest_host_mask &
8138 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008139 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008140 break;
8141 case 8:
8142 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008143 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008144 break;
8145 }
8146 break;
8147 case 2: /* clts */
8148 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8149 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008150 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008151 break;
8152 case 1: /* mov from cr */
8153 switch (cr) {
8154 case 3:
8155 if (vmcs12->cpu_based_vm_exec_control &
8156 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008157 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008158 break;
8159 case 8:
8160 if (vmcs12->cpu_based_vm_exec_control &
8161 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008162 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008163 break;
8164 }
8165 break;
8166 case 3: /* lmsw */
8167 /*
8168 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8169 * cr0. Other attempted changes are ignored, with no exit.
8170 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008171 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008172 if (vmcs12->cr0_guest_host_mask & 0xe &
8173 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008174 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008175 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8176 !(vmcs12->cr0_read_shadow & 0x1) &&
8177 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008178 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008179 break;
8180 }
Joe Perches1d804d02015-03-30 16:46:09 -07008181 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008182}
8183
8184/*
8185 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8186 * should handle it ourselves in L0 (and then continue L2). Only call this
8187 * when in is_guest_mode (L2).
8188 */
8189static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8190{
Nadav Har'El644d7112011-05-25 23:12:35 +03008191 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8192 struct vcpu_vmx *vmx = to_vmx(vcpu);
8193 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008194 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008195
Jan Kiszka542060e2014-01-04 18:47:21 +01008196 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8197 vmcs_readl(EXIT_QUALIFICATION),
8198 vmx->idt_vectoring_info,
8199 intr_info,
8200 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8201 KVM_ISA_VMX);
8202
David Matlackb7649e12017-08-01 14:00:40 -07008203 /*
8204 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008205 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8206 * Page). The CPU may write to these pages via their host
8207 * physical address while L2 is running, bypassing any
8208 * address-translation-based dirty tracking (e.g. EPT write
8209 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008210 *
8211 * Mark them dirty on every exit from L2 to prevent them from
8212 * getting out of sync with dirty tracking.
8213 */
8214 nested_mark_vmcs12_pages_dirty(vcpu);
8215
Nadav Har'El644d7112011-05-25 23:12:35 +03008216 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008217 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008218
8219 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008220 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8221 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008222 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008223 }
8224
8225 switch (exit_reason) {
8226 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008227 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008228 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008229 else if (is_page_fault(intr_info))
8230 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008231 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008232 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008233 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008234 else if (is_debug(intr_info) &&
8235 vcpu->guest_debug &
8236 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8237 return false;
8238 else if (is_breakpoint(intr_info) &&
8239 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8240 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008241 return vmcs12->exception_bitmap &
8242 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8243 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008244 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008245 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008246 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008247 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008248 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008249 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008250 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008251 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008252 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008253 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008254 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008255 case EXIT_REASON_HLT:
8256 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8257 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008258 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008259 case EXIT_REASON_INVLPG:
8260 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8261 case EXIT_REASON_RDPMC:
8262 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008263 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008264 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8265 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8266 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8267 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8268 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8269 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008270 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008271 /*
8272 * VMX instructions trap unconditionally. This allows L1 to
8273 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8274 */
Joe Perches1d804d02015-03-30 16:46:09 -07008275 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008276 case EXIT_REASON_CR_ACCESS:
8277 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8278 case EXIT_REASON_DR_ACCESS:
8279 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8280 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008281 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008282 case EXIT_REASON_MSR_READ:
8283 case EXIT_REASON_MSR_WRITE:
8284 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8285 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008286 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008287 case EXIT_REASON_MWAIT_INSTRUCTION:
8288 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008289 case EXIT_REASON_MONITOR_TRAP_FLAG:
8290 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008291 case EXIT_REASON_MONITOR_INSTRUCTION:
8292 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8293 case EXIT_REASON_PAUSE_INSTRUCTION:
8294 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8295 nested_cpu_has2(vmcs12,
8296 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8297 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008298 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008299 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008300 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008301 case EXIT_REASON_APIC_ACCESS:
8302 return nested_cpu_has2(vmcs12,
8303 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008304 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008305 case EXIT_REASON_EOI_INDUCED:
8306 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008307 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008308 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008309 /*
8310 * L0 always deals with the EPT violation. If nested EPT is
8311 * used, and the nested mmu code discovers that the address is
8312 * missing in the guest EPT table (EPT12), the EPT violation
8313 * will be injected with nested_ept_inject_page_fault()
8314 */
Joe Perches1d804d02015-03-30 16:46:09 -07008315 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008316 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008317 /*
8318 * L2 never uses directly L1's EPT, but rather L0's own EPT
8319 * table (shadow on EPT) or a merged EPT table that L0 built
8320 * (EPT on EPT). So any problems with the structure of the
8321 * table is L0's fault.
8322 */
Joe Perches1d804d02015-03-30 16:46:09 -07008323 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008324 case EXIT_REASON_WBINVD:
8325 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8326 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008327 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008328 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8329 /*
8330 * This should never happen, since it is not possible to
8331 * set XSS to a non-zero value---neither in L1 nor in L2.
8332 * If if it were, XSS would have to be checked against
8333 * the XSS exit bitmap in vmcs12.
8334 */
8335 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008336 case EXIT_REASON_PREEMPTION_TIMER:
8337 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008338 case EXIT_REASON_PML_FULL:
8339 /* We don't expose PML support to L1. */
8340 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008341 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008342 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008343 }
8344}
8345
Avi Kivity586f9602010-11-18 13:09:54 +02008346static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8347{
8348 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8349 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8350}
8351
Kai Huanga3eaa862015-11-04 13:46:05 +08008352static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008353{
Kai Huanga3eaa862015-11-04 13:46:05 +08008354 if (vmx->pml_pg) {
8355 __free_page(vmx->pml_pg);
8356 vmx->pml_pg = NULL;
8357 }
Kai Huang843e4332015-01-28 10:54:28 +08008358}
8359
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008360static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008361{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008362 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008363 u64 *pml_buf;
8364 u16 pml_idx;
8365
8366 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8367
8368 /* Do nothing if PML buffer is empty */
8369 if (pml_idx == (PML_ENTITY_NUM - 1))
8370 return;
8371
8372 /* PML index always points to next available PML buffer entity */
8373 if (pml_idx >= PML_ENTITY_NUM)
8374 pml_idx = 0;
8375 else
8376 pml_idx++;
8377
8378 pml_buf = page_address(vmx->pml_pg);
8379 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8380 u64 gpa;
8381
8382 gpa = pml_buf[pml_idx];
8383 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008384 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008385 }
8386
8387 /* reset PML index */
8388 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8389}
8390
8391/*
8392 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8393 * Called before reporting dirty_bitmap to userspace.
8394 */
8395static void kvm_flush_pml_buffers(struct kvm *kvm)
8396{
8397 int i;
8398 struct kvm_vcpu *vcpu;
8399 /*
8400 * We only need to kick vcpu out of guest mode here, as PML buffer
8401 * is flushed at beginning of all VMEXITs, and it's obvious that only
8402 * vcpus running in guest are possible to have unflushed GPAs in PML
8403 * buffer.
8404 */
8405 kvm_for_each_vcpu(i, vcpu, kvm)
8406 kvm_vcpu_kick(vcpu);
8407}
8408
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008409static void vmx_dump_sel(char *name, uint32_t sel)
8410{
8411 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008412 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008413 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8414 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8415 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8416}
8417
8418static void vmx_dump_dtsel(char *name, uint32_t limit)
8419{
8420 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8421 name, vmcs_read32(limit),
8422 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8423}
8424
8425static void dump_vmcs(void)
8426{
8427 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8428 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8429 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8430 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8431 u32 secondary_exec_control = 0;
8432 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008433 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008434 int i, n;
8435
8436 if (cpu_has_secondary_exec_ctrls())
8437 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8438
8439 pr_err("*** Guest State ***\n");
8440 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8441 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8442 vmcs_readl(CR0_GUEST_HOST_MASK));
8443 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8444 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8445 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8446 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8447 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8448 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008449 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8450 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8451 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8452 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008453 }
8454 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8455 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8456 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8457 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8458 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8459 vmcs_readl(GUEST_SYSENTER_ESP),
8460 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8461 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8462 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8463 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8464 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8465 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8466 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8467 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8468 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8469 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8470 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8471 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8472 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008473 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8474 efer, vmcs_read64(GUEST_IA32_PAT));
8475 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8476 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008477 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8478 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008479 pr_err("PerfGlobCtl = 0x%016llx\n",
8480 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008481 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008482 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008483 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8484 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8485 vmcs_read32(GUEST_ACTIVITY_STATE));
8486 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8487 pr_err("InterruptStatus = %04x\n",
8488 vmcs_read16(GUEST_INTR_STATUS));
8489
8490 pr_err("*** Host State ***\n");
8491 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8492 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8493 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8494 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8495 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8496 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8497 vmcs_read16(HOST_TR_SELECTOR));
8498 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8499 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8500 vmcs_readl(HOST_TR_BASE));
8501 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8502 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8503 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8504 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8505 vmcs_readl(HOST_CR4));
8506 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8507 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8508 vmcs_read32(HOST_IA32_SYSENTER_CS),
8509 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8510 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008511 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8512 vmcs_read64(HOST_IA32_EFER),
8513 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008514 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008515 pr_err("PerfGlobCtl = 0x%016llx\n",
8516 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008517
8518 pr_err("*** Control State ***\n");
8519 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8520 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8521 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8522 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8523 vmcs_read32(EXCEPTION_BITMAP),
8524 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8525 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8526 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8527 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8528 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8529 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8530 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8531 vmcs_read32(VM_EXIT_INTR_INFO),
8532 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8533 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8534 pr_err(" reason=%08x qualification=%016lx\n",
8535 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8536 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8537 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8538 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008539 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008540 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008541 pr_err("TSC Multiplier = 0x%016llx\n",
8542 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008543 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8544 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8545 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8546 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8547 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008548 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008549 n = vmcs_read32(CR3_TARGET_COUNT);
8550 for (i = 0; i + 1 < n; i += 4)
8551 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8552 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8553 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8554 if (i < n)
8555 pr_err("CR3 target%u=%016lx\n",
8556 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8557 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8558 pr_err("PLE Gap=%08x Window=%08x\n",
8559 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8560 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8561 pr_err("Virtual processor ID = 0x%04x\n",
8562 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8563}
8564
Avi Kivity6aa8b732006-12-10 02:21:36 -08008565/*
8566 * The guest has exited. See if we can fix it or if we need userspace
8567 * assistance.
8568 */
Avi Kivity851ba692009-08-24 11:10:17 +03008569static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008570{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008571 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008572 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008573 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008574
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008575 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8576
Kai Huang843e4332015-01-28 10:54:28 +08008577 /*
8578 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8579 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8580 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8581 * mode as if vcpus is in root mode, the PML buffer must has been
8582 * flushed already.
8583 */
8584 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008585 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008586
Mohammed Gamal80ced182009-09-01 12:48:18 +02008587 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008588 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008589 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008590
Nadav Har'El644d7112011-05-25 23:12:35 +03008591 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008592 nested_vmx_vmexit(vcpu, exit_reason,
8593 vmcs_read32(VM_EXIT_INTR_INFO),
8594 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008595 return 1;
8596 }
8597
Mohammed Gamal51207022010-05-31 22:40:54 +03008598 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008599 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008600 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8601 vcpu->run->fail_entry.hardware_entry_failure_reason
8602 = exit_reason;
8603 return 0;
8604 }
8605
Avi Kivity29bd8a72007-09-10 17:27:03 +03008606 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008607 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8608 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008609 = vmcs_read32(VM_INSTRUCTION_ERROR);
8610 return 0;
8611 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008612
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008613 /*
8614 * Note:
8615 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8616 * delivery event since it indicates guest is accessing MMIO.
8617 * The vm-exit can be triggered again after return to guest that
8618 * will cause infinite loop.
8619 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008620 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008621 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008622 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008623 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008624 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8625 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8626 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8627 vcpu->run->internal.ndata = 2;
8628 vcpu->run->internal.data[0] = vectoring_info;
8629 vcpu->run->internal.data[1] = exit_reason;
8630 return 0;
8631 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008632
Nadav Har'El644d7112011-05-25 23:12:35 +03008633 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8634 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008635 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008636 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008637 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008638 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008639 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008640 /*
8641 * This CPU don't support us in finding the end of an
8642 * NMI-blocked window if the guest runs with IRQs
8643 * disabled. So we pull the trigger after 1 s of
8644 * futile waiting, but inform the user about this.
8645 */
8646 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8647 "state on VCPU %d after 1 s timeout\n",
8648 __func__, vcpu->vcpu_id);
8649 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008650 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008651 }
8652
Avi Kivity6aa8b732006-12-10 02:21:36 -08008653 if (exit_reason < kvm_vmx_max_exit_handlers
8654 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008655 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008656 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008657 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8658 kvm_queue_exception(vcpu, UD_VECTOR);
8659 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008660 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008661}
8662
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008663/*
8664 * Software based L1D cache flush which is used when microcode providing
8665 * the cache control MSR is not loaded.
8666 *
8667 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8668 * flush it is required to read in 64 KiB because the replacement algorithm
8669 * is not exactly LRU. This could be sized at runtime via topology
8670 * information but as all relevant affected CPUs have 32KiB L1D cache size
8671 * there is no point in doing so.
8672 */
8673#define L1D_CACHE_ORDER 4
8674static void *vmx_l1d_flush_pages;
8675
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008676static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008677{
8678 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008679
8680 /*
Thomas Gleixner31282cf2018-07-13 16:23:17 +02008681 * This code is only executed when the the flush mode is 'cond' or
8682 * 'always'
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008683 */
Nicolai Stange936f5662018-07-21 22:25:00 +02008684 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stangee371c922018-07-27 13:22:16 +02008685 bool flush_l1d;
Nicolai Stange90bc3062018-07-21 22:35:28 +02008686
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008687 /*
Nicolai Stangee371c922018-07-27 13:22:16 +02008688 * Clear the per-vcpu flush bit, it gets set again
8689 * either from vcpu_run() or from one of the unsafe
8690 * VMEXIT handlers.
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008691 */
Nicolai Stangee371c922018-07-27 13:22:16 +02008692 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixnerdff09822018-07-13 16:23:20 +02008693 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stangee371c922018-07-27 13:22:16 +02008694
8695 /*
8696 * Clear the per-cpu flush bit, it gets set again from
8697 * the interrupt handlers.
8698 */
8699 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
8700 kvm_clear_cpu_l1tf_flush_l1d();
8701
Nicolai Stange90bc3062018-07-21 22:35:28 +02008702 if (!flush_l1d)
8703 return;
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008704 }
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008705
8706 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008707
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008708 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8709 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8710 return;
8711 }
8712
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008713 asm volatile(
8714 /* First ensure the pages are in the TLB */
8715 "xorl %%eax, %%eax\n"
8716 ".Lpopulate_tlb:\n\t"
Nicolai Stange587d4992018-07-18 19:07:38 +02008717 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008718 "addl $4096, %%eax\n\t"
8719 "cmpl %%eax, %[size]\n\t"
8720 "jne .Lpopulate_tlb\n\t"
8721 "xorl %%eax, %%eax\n\t"
8722 "cpuid\n\t"
8723 /* Now fill the cache */
8724 "xorl %%eax, %%eax\n"
8725 ".Lfill_cache:\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008726 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008727 "addl $64, %%eax\n\t"
8728 "cmpl %%eax, %[size]\n\t"
8729 "jne .Lfill_cache\n\t"
8730 "lfence\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008731 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008732 [size] "r" (size)
8733 : "eax", "ebx", "ecx", "edx");
8734}
8735
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008736static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008737{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008738 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8739
8740 if (is_guest_mode(vcpu) &&
8741 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8742 return;
8743
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008744 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008745 vmcs_write32(TPR_THRESHOLD, 0);
8746 return;
8747 }
8748
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008749 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008750}
8751
Yang Zhang8d146952013-01-25 10:18:50 +08008752static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8753{
8754 u32 sec_exec_control;
8755
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008756 /* Postpone execution until vmcs01 is the current VMCS. */
8757 if (is_guest_mode(vcpu)) {
8758 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8759 return;
8760 }
8761
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008762 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008763 return;
8764
Paolo Bonzini35754c92015-07-29 12:05:37 +02008765 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008766 return;
8767
8768 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8769
8770 if (set) {
8771 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8772 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8773 } else {
8774 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8775 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008776 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008777 }
8778 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8779
Paolo Bonzini6236b782018-01-16 16:51:18 +01008780 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008781}
8782
Tang Chen38b99172014-09-24 15:57:54 +08008783static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8784{
8785 struct vcpu_vmx *vmx = to_vmx(vcpu);
8786
8787 /*
8788 * Currently we do not handle the nested case where L2 has an
8789 * APIC access page of its own; that page is still pinned.
8790 * Hence, we skip the case where the VCPU is in guest mode _and_
8791 * L1 prepared an APIC access page for L2.
8792 *
8793 * For the case where L1 and L2 share the same APIC access page
8794 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8795 * in the vmcs12), this function will only update either the vmcs01
8796 * or the vmcs02. If the former, the vmcs02 will be updated by
8797 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8798 * the next L2->L1 exit.
8799 */
8800 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008801 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008802 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008803 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008804 vmx_flush_tlb_ept_only(vcpu);
8805 }
Tang Chen38b99172014-09-24 15:57:54 +08008806}
8807
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008808static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008809{
8810 u16 status;
8811 u8 old;
8812
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008813 if (max_isr == -1)
8814 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008815
8816 status = vmcs_read16(GUEST_INTR_STATUS);
8817 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008818 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008819 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008820 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008821 vmcs_write16(GUEST_INTR_STATUS, status);
8822 }
8823}
8824
8825static void vmx_set_rvi(int vector)
8826{
8827 u16 status;
8828 u8 old;
8829
Wei Wang4114c272014-11-05 10:53:43 +08008830 if (vector == -1)
8831 vector = 0;
8832
Yang Zhangc7c9c562013-01-25 10:18:51 +08008833 status = vmcs_read16(GUEST_INTR_STATUS);
8834 old = (u8)status & 0xff;
8835 if ((u8)vector != old) {
8836 status &= ~0xff;
8837 status |= (u8)vector;
8838 vmcs_write16(GUEST_INTR_STATUS, status);
8839 }
8840}
8841
8842static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8843{
Wanpeng Li963fee12014-07-17 19:03:00 +08008844 if (!is_guest_mode(vcpu)) {
8845 vmx_set_rvi(max_irr);
8846 return;
8847 }
8848
Wei Wang4114c272014-11-05 10:53:43 +08008849 if (max_irr == -1)
8850 return;
8851
Wanpeng Li963fee12014-07-17 19:03:00 +08008852 /*
Wei Wang4114c272014-11-05 10:53:43 +08008853 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8854 * handles it.
8855 */
8856 if (nested_exit_on_intr(vcpu))
8857 return;
8858
8859 /*
8860 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008861 * is run without virtual interrupt delivery.
8862 */
8863 if (!kvm_event_needs_reinjection(vcpu) &&
8864 vmx_interrupt_allowed(vcpu)) {
8865 kvm_queue_interrupt(vcpu, max_irr, false);
8866 vmx_inject_irq(vcpu);
8867 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008868}
8869
Andrey Smetanin63086302015-11-10 15:36:32 +03008870static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008871{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008872 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008873 return;
8874
Yang Zhangc7c9c562013-01-25 10:18:51 +08008875 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8876 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8877 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8878 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8879}
8880
Avi Kivity51aa01d2010-07-20 14:31:20 +03008881static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008882{
Avi Kivity00eba012011-03-07 17:24:54 +02008883 u32 exit_intr_info;
8884
8885 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8886 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8887 return;
8888
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008889 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008890 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008891
8892 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008893 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008894 kvm_machine_check();
8895
Gleb Natapov20f65982009-05-11 13:35:55 +03008896 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008897 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008898 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008899 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008900 kvm_after_handle_nmi(&vmx->vcpu);
8901 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008902}
Gleb Natapov20f65982009-05-11 13:35:55 +03008903
Yang Zhanga547c6d2013-04-11 19:25:10 +08008904static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8905{
8906 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8907
8908 /*
8909 * If external interrupt exists, IF bit is set in rflags/eflags on the
8910 * interrupt stack frame, and interrupt will be enabled on a return
8911 * from interrupt handler.
8912 */
8913 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8914 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8915 unsigned int vector;
8916 unsigned long entry;
8917 gate_desc *desc;
8918 struct vcpu_vmx *vmx = to_vmx(vcpu);
8919#ifdef CONFIG_X86_64
8920 unsigned long tmp;
8921#endif
8922
8923 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8924 desc = (gate_desc *)vmx->host_idt_base + vector;
8925 entry = gate_offset(*desc);
8926 asm volatile(
8927#ifdef CONFIG_X86_64
8928 "mov %%" _ASM_SP ", %[sp]\n\t"
8929 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8930 "push $%c[ss]\n\t"
8931 "push %[sp]\n\t"
8932#endif
8933 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008934 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008935 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008936 :
8937#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008938 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008939#endif
Josh Poimboeufd5ea93e2017-09-20 16:24:33 -05008940 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08008941 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008942 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008943 [ss]"i"(__KERNEL_DS),
8944 [cs]"i"(__KERNEL_CS)
8945 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008946 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008947}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008948STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008949
Tom Lendackyb9655922018-05-10 22:06:39 +02008950static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008951{
Tom Lendackyb9655922018-05-10 22:06:39 +02008952 switch (index) {
8953 case MSR_IA32_SMBASE:
8954 /*
8955 * We cannot do SMM unless we can run the guest in big
8956 * real mode.
8957 */
8958 return enable_unrestricted_guest || emulate_invalid_guest_state;
8959 case MSR_AMD64_VIRT_SPEC_CTRL:
8960 /* This is AMD only. */
8961 return false;
8962 default:
8963 return true;
8964 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008965}
8966
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008967static bool vmx_mpx_supported(void)
8968{
8969 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8970 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8971}
8972
Wanpeng Li55412b22014-12-02 19:21:30 +08008973static bool vmx_xsaves_supported(void)
8974{
8975 return vmcs_config.cpu_based_2nd_exec_ctrl &
8976 SECONDARY_EXEC_XSAVES;
8977}
8978
Avi Kivity51aa01d2010-07-20 14:31:20 +03008979static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8980{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008981 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008982 bool unblock_nmi;
8983 u8 vector;
8984 bool idtv_info_valid;
8985
8986 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008987
Avi Kivitycf393f72008-07-01 16:20:21 +03008988 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008989 if (vmx->nmi_known_unmasked)
8990 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008991 /*
8992 * Can't use vmx->exit_intr_info since we're not sure what
8993 * the exit reason is.
8994 */
8995 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008996 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8997 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8998 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008999 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03009000 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9001 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009002 * SDM 3: 23.2.2 (September 2008)
9003 * Bit 12 is undefined in any of the following cases:
9004 * If the VM exit sets the valid bit in the IDT-vectoring
9005 * information field.
9006 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03009007 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009008 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9009 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03009010 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9011 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02009012 else
9013 vmx->nmi_known_unmasked =
9014 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9015 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009016 } else if (unlikely(vmx->soft_vnmi_blocked))
9017 vmx->vnmi_blocked_time +=
9018 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009019}
9020
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009021static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009022 u32 idt_vectoring_info,
9023 int instr_len_field,
9024 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009025{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009026 u8 vector;
9027 int type;
9028 bool idtv_info_valid;
9029
9030 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009031
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009032 vcpu->arch.nmi_injected = false;
9033 kvm_clear_exception_queue(vcpu);
9034 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009035
9036 if (!idtv_info_valid)
9037 return;
9038
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009039 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009040
Avi Kivity668f6122008-07-02 09:28:55 +03009041 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9042 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009043
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009044 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009045 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009046 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009047 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009048 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009049 * Clear bit "block by NMI" before VM entry if a NMI
9050 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009051 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009052 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009053 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009054 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009055 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009056 /* fall through */
9057 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009058 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009059 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009060 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009061 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009062 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009063 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009064 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009065 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009066 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009067 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009068 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009069 break;
9070 default:
9071 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009072 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009073}
9074
Avi Kivity83422e12010-07-20 14:43:23 +03009075static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9076{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009077 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009078 VM_EXIT_INSTRUCTION_LEN,
9079 IDT_VECTORING_ERROR_CODE);
9080}
9081
Avi Kivityb463a6f2010-07-20 15:06:17 +03009082static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9083{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009084 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009085 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9086 VM_ENTRY_INSTRUCTION_LEN,
9087 VM_ENTRY_EXCEPTION_ERROR_CODE);
9088
9089 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9090}
9091
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009092static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9093{
9094 int i, nr_msrs;
9095 struct perf_guest_switch_msr *msrs;
9096
9097 msrs = perf_guest_get_msrs(&nr_msrs);
9098
9099 if (!msrs)
9100 return;
9101
9102 for (i = 0; i < nr_msrs; i++)
9103 if (msrs[i].host == msrs[i].guest)
9104 clear_atomic_switch_msr(vmx, msrs[i].msr);
9105 else
9106 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04009107 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009108}
9109
Yunhong Jiang64672c92016-06-13 14:19:59 -07009110void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9111{
9112 struct vcpu_vmx *vmx = to_vmx(vcpu);
9113 u64 tscl;
9114 u32 delta_tsc;
9115
9116 if (vmx->hv_deadline_tsc == -1)
9117 return;
9118
9119 tscl = rdtsc();
9120 if (vmx->hv_deadline_tsc > tscl)
9121 /* sure to be 32 bit only because checked on set_hv_timer */
9122 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9123 cpu_preemption_timer_multi);
9124 else
9125 delta_tsc = 0;
9126
9127 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9128}
9129
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009130static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009131{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009132 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009133 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009134
9135 /* Record the guest's net vcpu time for enforced NMI injections. */
9136 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9137 vmx->entry_time = ktime_get();
9138
9139 /* Don't enter VMX if guest state is invalid, let the exit handler
9140 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009141 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009142 return;
9143
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009144 if (vmx->ple_window_dirty) {
9145 vmx->ple_window_dirty = false;
9146 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9147 }
9148
Abel Gordon012f83c2013-04-18 14:39:25 +03009149 if (vmx->nested.sync_shadow_vmcs) {
9150 copy_vmcs12_to_shadow(vmx);
9151 vmx->nested.sync_shadow_vmcs = false;
9152 }
9153
Avi Kivity104f2262010-11-18 13:12:52 +02009154 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9155 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9156 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9157 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9158
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009159 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009160 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9161 vmcs_writel(HOST_CR4, cr4);
9162 vmx->host_state.vmcs_host_cr4 = cr4;
9163 }
9164
Avi Kivity104f2262010-11-18 13:12:52 +02009165 /* When single-stepping over STI and MOV SS, we must clear the
9166 * corresponding interruptibility bits in the guest state. Otherwise
9167 * vmentry fails as it then expects bit 14 (BS) in pending debug
9168 * exceptions being set, but that's not correct for the guest debugging
9169 * case. */
9170 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9171 vmx_set_interrupt_shadow(vcpu, 0);
9172
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009173 if (vmx->guest_pkru_valid)
9174 __write_pkru(vmx->guest_pkru);
9175
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009176 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009177 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009178
Yunhong Jiang64672c92016-06-13 14:19:59 -07009179 vmx_arm_hv_timer(vcpu);
9180
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009181 /*
9182 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9183 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9184 * is no need to worry about the conditional branch over the wrmsr
9185 * being speculatively taken.
9186 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009187 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009188
Nadav Har'Eld462b812011-05-24 15:26:10 +03009189 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009190
Nicolai Stange90bc3062018-07-21 22:35:28 +02009191 if (static_branch_unlikely(&vmx_l1d_should_flush))
9192 vmx_l1d_flush(vcpu);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009193
Avi Kivity104f2262010-11-18 13:12:52 +02009194 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009195 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009196 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9197 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9198 "push %%" _ASM_CX " \n\t"
9199 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009200 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009201 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009202 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009203 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009204 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009205 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9206 "mov %%cr2, %%" _ASM_DX " \n\t"
9207 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009208 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009209 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009210 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009211 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009212 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009213 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009214 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9215 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9216 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9217 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9218 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9219 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009220#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009221 "mov %c[r8](%0), %%r8 \n\t"
9222 "mov %c[r9](%0), %%r9 \n\t"
9223 "mov %c[r10](%0), %%r10 \n\t"
9224 "mov %c[r11](%0), %%r11 \n\t"
9225 "mov %c[r12](%0), %%r12 \n\t"
9226 "mov %c[r13](%0), %%r13 \n\t"
9227 "mov %c[r14](%0), %%r14 \n\t"
9228 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009229#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009230 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009231
Avi Kivity6aa8b732006-12-10 02:21:36 -08009232 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009233 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009234 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009235 "jmp 2f \n\t"
9236 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9237 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009238 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009239 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009240 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009241 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009242 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9243 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9244 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9245 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9246 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9247 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9248 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009249#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009250 "mov %%r8, %c[r8](%0) \n\t"
9251 "mov %%r9, %c[r9](%0) \n\t"
9252 "mov %%r10, %c[r10](%0) \n\t"
9253 "mov %%r11, %c[r11](%0) \n\t"
9254 "mov %%r12, %c[r12](%0) \n\t"
9255 "mov %%r13, %c[r13](%0) \n\t"
9256 "mov %%r14, %c[r14](%0) \n\t"
9257 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009258 "xor %%r8d, %%r8d \n\t"
9259 "xor %%r9d, %%r9d \n\t"
9260 "xor %%r10d, %%r10d \n\t"
9261 "xor %%r11d, %%r11d \n\t"
9262 "xor %%r12d, %%r12d \n\t"
9263 "xor %%r13d, %%r13d \n\t"
9264 "xor %%r14d, %%r14d \n\t"
9265 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009266#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009267 "mov %%cr2, %%" _ASM_AX " \n\t"
9268 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009269
Jim Mattson491c0ca2018-01-03 14:31:38 -08009270 "xor %%eax, %%eax \n\t"
9271 "xor %%ebx, %%ebx \n\t"
9272 "xor %%esi, %%esi \n\t"
9273 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009274 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009275 ".pushsection .rodata \n\t"
9276 ".global vmx_return \n\t"
9277 "vmx_return: " _ASM_PTR " 2b \n\t"
9278 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009279 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009280 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009281 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009282 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009283 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9284 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9285 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9286 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9287 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9288 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9289 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009290#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009291 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9292 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9293 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9294 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9295 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9296 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9297 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9298 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009299#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009300 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9301 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009302 : "cc", "memory"
9303#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009304 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009305 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009306#else
9307 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009308#endif
9309 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009310
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009311 /*
9312 * We do not use IBRS in the kernel. If this vCPU has used the
9313 * SPEC_CTRL MSR it may have left it on; save the value and
9314 * turn it off. This is much more efficient than blindly adding
9315 * it to the atomic save/restore list. Especially as the former
9316 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9317 *
9318 * For non-nested case:
9319 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9320 * save it.
9321 *
9322 * For nested case:
9323 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9324 * save it.
9325 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009326 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009327 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009328
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009329 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009330
David Woodhousec1ddd992018-01-12 11:11:27 +00009331 /* Eliminate branch target predictions from guest mode */
9332 vmexit_fill_RSB();
9333
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009334 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9335 if (debugctlmsr)
9336 update_debugctlmsr(debugctlmsr);
9337
Avi Kivityaa67f602012-08-01 16:48:03 +03009338#ifndef CONFIG_X86_64
9339 /*
9340 * The sysexit path does not restore ds/es, so we must set them to
9341 * a reasonable value ourselves.
9342 *
9343 * We can't defer this to vmx_load_host_state() since that function
9344 * may be executed in interrupt context, which saves and restore segments
9345 * around it, nullifying its effect.
9346 */
9347 loadsegment(ds, __USER_DS);
9348 loadsegment(es, __USER_DS);
9349#endif
9350
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009351 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009352 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009353 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009354 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009355 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009356 vcpu->arch.regs_dirty = 0;
9357
Avi Kivity1155f762007-11-22 11:30:47 +02009358 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9359
Nadav Har'Eld462b812011-05-24 15:26:10 +03009360 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009361
Avi Kivity51aa01d2010-07-20 14:31:20 +03009362 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009363
Gleb Natapove0b890d2013-09-25 12:51:33 +03009364 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009365 * eager fpu is enabled if PKEY is supported and CR4 is switched
9366 * back on host, so it is safe to read guest PKRU from current
9367 * XSAVE.
9368 */
9369 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9370 vmx->guest_pkru = __read_pkru();
9371 if (vmx->guest_pkru != vmx->host_pkru) {
9372 vmx->guest_pkru_valid = true;
9373 __write_pkru(vmx->host_pkru);
9374 } else
9375 vmx->guest_pkru_valid = false;
9376 }
9377
9378 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009379 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9380 * we did not inject a still-pending event to L1 now because of
9381 * nested_run_pending, we need to re-enable this bit.
9382 */
9383 if (vmx->nested.nested_run_pending)
9384 kvm_make_request(KVM_REQ_EVENT, vcpu);
9385
9386 vmx->nested.nested_run_pending = 0;
9387
Avi Kivity51aa01d2010-07-20 14:31:20 +03009388 vmx_complete_atomic_exit(vmx);
9389 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009390 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009391}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009392STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009393
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009394static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9395{
9396 struct vcpu_vmx *vmx = to_vmx(vcpu);
9397 int cpu;
9398
9399 if (vmx->loaded_vmcs == &vmx->vmcs01)
9400 return;
9401
9402 cpu = get_cpu();
9403 vmx->loaded_vmcs = &vmx->vmcs01;
9404 vmx_vcpu_put(vcpu);
9405 vmx_vcpu_load(vcpu, cpu);
9406 vcpu->cpu = cpu;
9407 put_cpu();
9408}
9409
Jim Mattson2f1fe812016-07-08 15:36:06 -07009410/*
9411 * Ensure that the current vmcs of the logical processor is the
9412 * vmcs01 of the vcpu before calling free_nested().
9413 */
9414static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9415{
9416 struct vcpu_vmx *vmx = to_vmx(vcpu);
9417 int r;
9418
9419 r = vcpu_load(vcpu);
9420 BUG_ON(r);
9421 vmx_load_vmcs01(vcpu);
9422 free_nested(vmx);
9423 vcpu_put(vcpu);
9424}
9425
Avi Kivity6aa8b732006-12-10 02:21:36 -08009426static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9427{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009428 struct vcpu_vmx *vmx = to_vmx(vcpu);
9429
Kai Huang843e4332015-01-28 10:54:28 +08009430 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009431 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009432 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009433 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009434 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009435 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009436 kfree(vmx->guest_msrs);
9437 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009438 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009439}
9440
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009441static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009442{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009443 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009444 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009445 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009446 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009447
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009448 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009449 return ERR_PTR(-ENOMEM);
9450
Wanpeng Li991e7a02015-09-16 17:30:05 +08009451 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009452
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009453 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9454 if (err)
9455 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009456
Peter Feiner4e595162016-07-07 14:49:58 -07009457 err = -ENOMEM;
9458
9459 /*
9460 * If PML is turned on, failure on enabling PML just results in failure
9461 * of creating the vcpu, therefore we can simplify PML logic (by
9462 * avoiding dealing with cases, such as enabling PML partially on vcpus
9463 * for the guest, etc.
9464 */
9465 if (enable_pml) {
9466 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9467 if (!vmx->pml_pg)
9468 goto uninit_vcpu;
9469 }
9470
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009471 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009472 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9473 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009474
Peter Feiner4e595162016-07-07 14:49:58 -07009475 if (!vmx->guest_msrs)
9476 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009477
Nadav Har'Eld462b812011-05-24 15:26:10 +03009478 if (!vmm_exclusive)
9479 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009480 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009481 if (!vmm_exclusive)
9482 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009483 if (err < 0)
9484 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009485
Paolo Bonzini6236b782018-01-16 16:51:18 +01009486 msr_bitmap = vmx->vmcs01.msr_bitmap;
9487 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9488 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9489 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9490 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9491 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9492 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9493 vmx->msr_bitmap_mode = 0;
9494
Paolo Bonziniff546f92018-01-11 12:16:15 +01009495 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009496 cpu = get_cpu();
9497 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009498 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009499 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009500 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009501 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009502 if (err)
9503 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009504 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009505 err = alloc_apic_access_page(kvm);
9506 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009507 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009508 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009509
Sheng Yangb927a3c2009-07-21 10:42:48 +08009510 if (enable_ept) {
9511 if (!kvm->arch.ept_identity_map_addr)
9512 kvm->arch.ept_identity_map_addr =
9513 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009514 err = init_rmode_identity_map(kvm);
9515 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009516 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009517 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009518
Roman Kagan020a90f2018-07-19 21:59:07 +03009519 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009520 nested_vmx_setup_ctls_msrs(vmx);
9521
Wincy Van705699a2015-02-03 23:58:17 +08009522 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009523 vmx->nested.current_vmptr = -1ull;
9524 vmx->nested.current_vmcs12 = NULL;
9525
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009526 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9527
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009528 /*
9529 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9530 * or POSTED_INTR_WAKEUP_VECTOR.
9531 */
9532 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9533 vmx->pi_desc.sn = 1;
9534
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009535 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009536
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009537free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009538 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009539free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009540 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009541free_pml:
9542 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009543uninit_vcpu:
9544 kvm_vcpu_uninit(&vmx->vcpu);
9545free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009546 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009547 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009548 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009549}
9550
Jiri Kosina2decbf52018-07-13 16:23:25 +02009551#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"
9552#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 Wilka0695af2018-06-20 11:29:53 -04009553
9554static int vmx_vm_init(struct kvm *kvm)
9555{
Jiri Kosina2decbf52018-07-13 16:23:25 +02009556 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
9557 switch (l1tf_mitigation) {
9558 case L1TF_MITIGATION_OFF:
9559 case L1TF_MITIGATION_FLUSH_NOWARN:
9560 /* 'I explicitly don't care' is set */
9561 break;
9562 case L1TF_MITIGATION_FLUSH:
9563 case L1TF_MITIGATION_FLUSH_NOSMT:
9564 case L1TF_MITIGATION_FULL:
9565 /*
9566 * Warn upon starting the first VM in a potentially
9567 * insecure environment.
9568 */
9569 if (cpu_smt_control == CPU_SMT_ENABLED)
9570 pr_warn_once(L1TF_MSG_SMT);
9571 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
9572 pr_warn_once(L1TF_MSG_L1D);
9573 break;
9574 case L1TF_MITIGATION_FULL_FORCE:
9575 /* Flush is enforced */
9576 break;
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009577 }
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009578 }
9579 return 0;
9580}
9581
Yang, Sheng002c7f72007-07-31 14:23:01 +03009582static void __init vmx_check_processor_compat(void *rtn)
9583{
9584 struct vmcs_config vmcs_conf;
9585
9586 *(int *)rtn = 0;
9587 if (setup_vmcs_config(&vmcs_conf) < 0)
9588 *(int *)rtn = -EIO;
9589 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9590 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9591 smp_processor_id());
9592 *(int *)rtn = -EIO;
9593 }
9594}
9595
Sheng Yang67253af2008-04-25 10:20:22 +08009596static int get_ept_level(void)
9597{
9598 return VMX_EPT_DEFAULT_GAW + 1;
9599}
9600
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009601static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009602{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009603 u8 cache;
9604 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009605
Sheng Yang522c68c2009-04-27 20:35:43 +08009606 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009607 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009608 * 2. EPT with VT-d:
9609 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009610 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009611 * b. VT-d with snooping control feature: snooping control feature of
9612 * VT-d engine can guarantee the cache correctness. Just set it
9613 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009614 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009615 * consistent with host MTRR
9616 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009617 if (is_mmio) {
9618 cache = MTRR_TYPE_UNCACHABLE;
9619 goto exit;
9620 }
9621
9622 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009623 ipat = VMX_EPT_IPAT_BIT;
9624 cache = MTRR_TYPE_WRBACK;
9625 goto exit;
9626 }
9627
9628 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9629 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009630 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009631 cache = MTRR_TYPE_WRBACK;
9632 else
9633 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009634 goto exit;
9635 }
9636
Xiao Guangrongff536042015-06-15 16:55:22 +08009637 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009638
9639exit:
9640 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009641}
9642
Sheng Yang17cc3932010-01-05 19:02:27 +08009643static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009644{
Sheng Yang878403b2010-01-05 19:02:29 +08009645 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9646 return PT_DIRECTORY_LEVEL;
9647 else
9648 /* For shadow and EPT supported 1GB page */
9649 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009650}
9651
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009652static void vmcs_set_secondary_exec_control(u32 new_ctl)
9653{
9654 /*
9655 * These bits in the secondary execution controls field
9656 * are dynamic, the others are mostly based on the hypervisor
9657 * architecture and the guest's CPUID. Do not touch the
9658 * dynamic bits.
9659 */
9660 u32 mask =
9661 SECONDARY_EXEC_SHADOW_VMCS |
9662 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9663 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9664
9665 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9666
9667 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9668 (new_ctl & ~mask) | (cur_ctl & mask));
9669}
9670
Sheng Yang0e851882009-12-18 16:48:46 +08009671static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9672{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009673 struct kvm_cpuid_entry2 *best;
9674 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009675 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009676
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009677 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009678 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9679 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009680 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009681
Paolo Bonzini8b972652015-09-15 17:34:42 +02009682 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009683 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009684 vmx->nested.nested_vmx_secondary_ctls_high |=
9685 SECONDARY_EXEC_RDTSCP;
9686 else
9687 vmx->nested.nested_vmx_secondary_ctls_high &=
9688 ~SECONDARY_EXEC_RDTSCP;
9689 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009690 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009691
Mao, Junjiead756a12012-07-02 01:18:48 +00009692 /* Exposing INVPCID only when PCID is exposed */
9693 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9694 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009695 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9696 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009697 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009698
Mao, Junjiead756a12012-07-02 01:18:48 +00009699 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009700 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009701 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009702
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009703 if (cpu_has_secondary_exec_ctrls())
9704 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009705
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009706 if (nested_vmx_allowed(vcpu))
9707 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9708 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9709 else
9710 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9711 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009712}
9713
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009714static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9715{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009716 if (func == 1 && nested)
9717 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009718}
9719
Yang Zhang25d92082013-08-06 12:00:32 +03009720static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9721 struct x86_exception *fault)
9722{
Jan Kiszka533558b2014-01-04 18:47:20 +01009723 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9724 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009725
9726 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009727 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009728 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009729 exit_reason = EXIT_REASON_EPT_VIOLATION;
9730 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009731 vmcs12->guest_physical_address = fault->address;
9732}
9733
Nadav Har'El155a97a2013-08-05 11:07:16 +03009734/* Callbacks for nested_ept_init_mmu_context: */
9735
9736static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9737{
9738 /* return the page table to be shadowed - in our case, EPT12 */
9739 return get_vmcs12(vcpu)->ept_pointer;
9740}
9741
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009742static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009743{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009744 WARN_ON(mmu_is_nested(vcpu));
9745 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009746 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9747 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009748 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9749 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9750 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9751
9752 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009753}
9754
9755static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9756{
9757 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9758}
9759
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009760static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9761 u16 error_code)
9762{
9763 bool inequality, bit;
9764
9765 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9766 inequality =
9767 (error_code & vmcs12->page_fault_error_code_mask) !=
9768 vmcs12->page_fault_error_code_match;
9769 return inequality ^ bit;
9770}
9771
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009772static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9773 struct x86_exception *fault)
9774{
9775 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9776
9777 WARN_ON(!is_guest_mode(vcpu));
9778
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009779 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009780 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9781 vmcs_read32(VM_EXIT_INTR_INFO),
9782 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009783 else
9784 kvm_inject_page_fault(vcpu, fault);
9785}
9786
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009787static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9788 struct vmcs12 *vmcs12)
9789{
9790 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009791 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009792
9793 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009794 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9795 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009796 return false;
9797
9798 /*
9799 * Translate L1 physical address to host physical
9800 * address for vmcs02. Keep the page pinned, so this
9801 * physical address remains valid. We keep a reference
9802 * to it so we can release it later.
9803 */
9804 if (vmx->nested.apic_access_page) /* shouldn't happen */
9805 nested_release_page(vmx->nested.apic_access_page);
9806 vmx->nested.apic_access_page =
9807 nested_get_page(vcpu, vmcs12->apic_access_addr);
9808 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009809
9810 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009811 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9812 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009813 return false;
9814
9815 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9816 nested_release_page(vmx->nested.virtual_apic_page);
9817 vmx->nested.virtual_apic_page =
9818 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9819
9820 /*
9821 * Failing the vm entry is _not_ what the processor does
9822 * but it's basically the only possibility we have.
9823 * We could still enter the guest if CR8 load exits are
9824 * enabled, CR8 store exits are enabled, and virtualize APIC
9825 * access is disabled; in this case the processor would never
9826 * use the TPR shadow and we could simply clear the bit from
9827 * the execution control. But such a configuration is useless,
9828 * so let's keep the code simple.
9829 */
9830 if (!vmx->nested.virtual_apic_page)
9831 return false;
9832 }
9833
Wincy Van705699a2015-02-03 23:58:17 +08009834 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009835 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9836 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009837 return false;
9838
9839 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9840 kunmap(vmx->nested.pi_desc_page);
9841 nested_release_page(vmx->nested.pi_desc_page);
9842 }
9843 vmx->nested.pi_desc_page =
9844 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9845 if (!vmx->nested.pi_desc_page)
9846 return false;
9847
9848 vmx->nested.pi_desc =
9849 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9850 if (!vmx->nested.pi_desc) {
9851 nested_release_page_clean(vmx->nested.pi_desc_page);
9852 return false;
9853 }
9854 vmx->nested.pi_desc =
9855 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9856 (unsigned long)(vmcs12->posted_intr_desc_addr &
9857 (PAGE_SIZE - 1)));
9858 }
9859
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009860 return true;
9861}
9862
Jan Kiszkaf4124502014-03-07 20:03:13 +01009863static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9864{
9865 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9866 struct vcpu_vmx *vmx = to_vmx(vcpu);
9867
9868 if (vcpu->arch.virtual_tsc_khz == 0)
9869 return;
9870
9871 /* Make sure short timeouts reliably trigger an immediate vmexit.
9872 * hrtimer_start does not guarantee this. */
9873 if (preemption_timeout <= 1) {
9874 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9875 return;
9876 }
9877
9878 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9879 preemption_timeout *= 1000000;
9880 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9881 hrtimer_start(&vmx->nested.preemption_timer,
9882 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9883}
9884
Wincy Van3af18d92015-02-03 23:49:31 +08009885static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9886 struct vmcs12 *vmcs12)
9887{
9888 int maxphyaddr;
9889 u64 addr;
9890
9891 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9892 return 0;
9893
9894 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9895 WARN_ON(1);
9896 return -EINVAL;
9897 }
9898 maxphyaddr = cpuid_maxphyaddr(vcpu);
9899
9900 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9901 ((addr + PAGE_SIZE) >> maxphyaddr))
9902 return -EINVAL;
9903
9904 return 0;
9905}
9906
9907/*
9908 * Merge L0's and L1's MSR bitmap, return false to indicate that
9909 * we do not use the hardware.
9910 */
9911static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9912 struct vmcs12 *vmcs12)
9913{
Wincy Van82f0dd42015-02-03 23:57:18 +08009914 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009915 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009916 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009917 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009918 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009919 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009920 *
9921 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9922 * ensures that we do not accidentally generate an L02 MSR bitmap
9923 * from the L12 MSR bitmap that is too permissive.
9924 * 2. That L1 or L2s have actually used the MSR. This avoids
9925 * unnecessarily merging of the bitmap if the MSR is unused. This
9926 * works properly because we only update the L01 MSR bitmap lazily.
9927 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9928 * updated to reflect this when L1 (or its L2s) actually write to
9929 * the MSR.
9930 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009931 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9932 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009933
Ashok Raj70131292018-02-01 22:59:43 +01009934 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009935 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009936 return false;
9937
9938 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009939 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009940 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009941 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009942
Radim Krčmářd048c092016-08-08 20:16:22 +02009943 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9944
Wincy Vanf2b93282015-02-03 23:56:03 +08009945 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009946 if (nested_cpu_has_apic_reg_virt(vmcs12))
9947 for (msr = 0x800; msr <= 0x8ff; msr++)
9948 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009949 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009950 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009951
9952 nested_vmx_disable_intercept_for_msr(
9953 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009954 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9955 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009956
Wincy Van608406e2015-02-03 23:57:51 +08009957 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009958 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009959 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009960 APIC_BASE_MSR + (APIC_EOI >> 4),
9961 MSR_TYPE_W);
9962 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009963 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009964 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9965 MSR_TYPE_W);
9966 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009967 }
Ashok Raj70131292018-02-01 22:59:43 +01009968
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009969 if (spec_ctrl)
9970 nested_vmx_disable_intercept_for_msr(
9971 msr_bitmap_l1, msr_bitmap_l0,
9972 MSR_IA32_SPEC_CTRL,
9973 MSR_TYPE_R | MSR_TYPE_W);
9974
Ashok Raj70131292018-02-01 22:59:43 +01009975 if (pred_cmd)
9976 nested_vmx_disable_intercept_for_msr(
9977 msr_bitmap_l1, msr_bitmap_l0,
9978 MSR_IA32_PRED_CMD,
9979 MSR_TYPE_W);
9980
Wincy Vanf2b93282015-02-03 23:56:03 +08009981 kunmap(page);
9982 nested_release_page_clean(page);
9983
9984 return true;
9985}
9986
9987static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9988 struct vmcs12 *vmcs12)
9989{
Wincy Van82f0dd42015-02-03 23:57:18 +08009990 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009991 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009992 !nested_cpu_has_vid(vmcs12) &&
9993 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009994 return 0;
9995
9996 /*
9997 * If virtualize x2apic mode is enabled,
9998 * virtualize apic access must be disabled.
9999 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010000 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10001 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010002 return -EINVAL;
10003
Wincy Van608406e2015-02-03 23:57:51 +080010004 /*
10005 * If virtual interrupt delivery is enabled,
10006 * we must exit on external interrupts.
10007 */
10008 if (nested_cpu_has_vid(vmcs12) &&
10009 !nested_exit_on_intr(vcpu))
10010 return -EINVAL;
10011
Wincy Van705699a2015-02-03 23:58:17 +080010012 /*
10013 * bits 15:8 should be zero in posted_intr_nv,
10014 * the descriptor address has been already checked
10015 * in nested_get_vmcs12_pages.
10016 */
10017 if (nested_cpu_has_posted_intr(vmcs12) &&
10018 (!nested_cpu_has_vid(vmcs12) ||
10019 !nested_exit_intr_ack_set(vcpu) ||
10020 vmcs12->posted_intr_nv & 0xff00))
10021 return -EINVAL;
10022
Wincy Vanf2b93282015-02-03 23:56:03 +080010023 /* tpr shadow is needed by all apicv features. */
10024 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10025 return -EINVAL;
10026
10027 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010028}
10029
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010030static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10031 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010032 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010033{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010034 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010035 u64 count, addr;
10036
10037 if (vmcs12_read_any(vcpu, count_field, &count) ||
10038 vmcs12_read_any(vcpu, addr_field, &addr)) {
10039 WARN_ON(1);
10040 return -EINVAL;
10041 }
10042 if (count == 0)
10043 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010044 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010045 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10046 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010047 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010048 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10049 addr_field, maxphyaddr, count, addr);
10050 return -EINVAL;
10051 }
10052 return 0;
10053}
10054
10055static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10056 struct vmcs12 *vmcs12)
10057{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010058 if (vmcs12->vm_exit_msr_load_count == 0 &&
10059 vmcs12->vm_exit_msr_store_count == 0 &&
10060 vmcs12->vm_entry_msr_load_count == 0)
10061 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010062 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010063 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010064 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010065 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010066 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010067 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010068 return -EINVAL;
10069 return 0;
10070}
10071
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010072static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10073 struct vmx_msr_entry *e)
10074{
10075 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010076 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010077 return -EINVAL;
10078 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10079 e->index == MSR_IA32_UCODE_REV)
10080 return -EINVAL;
10081 if (e->reserved != 0)
10082 return -EINVAL;
10083 return 0;
10084}
10085
10086static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10087 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010088{
10089 if (e->index == MSR_FS_BASE ||
10090 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010091 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10092 nested_vmx_msr_check_common(vcpu, e))
10093 return -EINVAL;
10094 return 0;
10095}
10096
10097static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10098 struct vmx_msr_entry *e)
10099{
10100 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10101 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010102 return -EINVAL;
10103 return 0;
10104}
10105
10106/*
10107 * Load guest's/host's msr at nested entry/exit.
10108 * return 0 for success, entry index for failure.
10109 */
10110static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10111{
10112 u32 i;
10113 struct vmx_msr_entry e;
10114 struct msr_data msr;
10115
10116 msr.host_initiated = false;
10117 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010118 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10119 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010120 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010121 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10122 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010123 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010124 }
10125 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010126 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010127 "%s check failed (%u, 0x%x, 0x%x)\n",
10128 __func__, i, e.index, e.reserved);
10129 goto fail;
10130 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010131 msr.index = e.index;
10132 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010133 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010134 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010135 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10136 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010137 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010138 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010139 }
10140 return 0;
10141fail:
10142 return i + 1;
10143}
10144
10145static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10146{
10147 u32 i;
10148 struct vmx_msr_entry e;
10149
10150 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010151 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010152 if (kvm_vcpu_read_guest(vcpu,
10153 gpa + i * sizeof(e),
10154 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010155 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010156 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10157 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010158 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010159 }
10160 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010161 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010162 "%s check failed (%u, 0x%x, 0x%x)\n",
10163 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010164 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010165 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010166 msr_info.host_initiated = false;
10167 msr_info.index = e.index;
10168 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010169 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010170 "%s cannot read MSR (%u, 0x%x)\n",
10171 __func__, i, e.index);
10172 return -EINVAL;
10173 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010174 if (kvm_vcpu_write_guest(vcpu,
10175 gpa + i * sizeof(e) +
10176 offsetof(struct vmx_msr_entry, value),
10177 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010178 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010179 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010180 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010181 return -EINVAL;
10182 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010183 }
10184 return 0;
10185}
10186
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010187/*
10188 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10189 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010190 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010191 * guest in a way that will both be appropriate to L1's requests, and our
10192 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10193 * function also has additional necessary side-effects, like setting various
10194 * vcpu->arch fields.
10195 */
10196static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10197{
10198 struct vcpu_vmx *vmx = to_vmx(vcpu);
10199 u32 exec_control;
10200
10201 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10202 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10203 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10204 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10205 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10206 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10207 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10208 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10209 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10210 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10211 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10212 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10213 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10214 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10215 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10216 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10217 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10218 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10219 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10220 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10221 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10222 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10223 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10224 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10225 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10226 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10227 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10228 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10229 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10230 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10231 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10232 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10233 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10234 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10235 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10236 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10237
Jan Kiszka2996fca2014-06-16 13:59:43 +020010238 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10239 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10240 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10241 } else {
10242 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10243 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10244 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010245 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10246 vmcs12->vm_entry_intr_info_field);
10247 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10248 vmcs12->vm_entry_exception_error_code);
10249 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10250 vmcs12->vm_entry_instruction_len);
10251 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10252 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010253 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010254 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010255 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10256 vmcs12->guest_pending_dbg_exceptions);
10257 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10258 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10259
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010260 if (nested_cpu_has_xsaves(vmcs12))
10261 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010262 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10263
Jan Kiszkaf4124502014-03-07 20:03:13 +010010264 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010265
Paolo Bonzini93140062016-07-06 13:23:51 +020010266 /* Preemption timer setting is only taken from vmcs01. */
10267 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10268 exec_control |= vmcs_config.pin_based_exec_ctrl;
10269 if (vmx->hv_deadline_tsc == -1)
10270 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10271
10272 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010273 if (nested_cpu_has_posted_intr(vmcs12)) {
10274 /*
10275 * Note that we use L0's vector here and in
10276 * vmx_deliver_nested_posted_interrupt.
10277 */
10278 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10279 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010280 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010281 vmcs_write64(POSTED_INTR_DESC_ADDR,
10282 page_to_phys(vmx->nested.pi_desc_page) +
10283 (unsigned long)(vmcs12->posted_intr_desc_addr &
10284 (PAGE_SIZE - 1)));
10285 } else
10286 exec_control &= ~PIN_BASED_POSTED_INTR;
10287
Jan Kiszkaf4124502014-03-07 20:03:13 +010010288 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010289
Jan Kiszkaf4124502014-03-07 20:03:13 +010010290 vmx->nested.preemption_timer_expired = false;
10291 if (nested_cpu_has_preemption_timer(vmcs12))
10292 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010293
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010294 /*
10295 * Whether page-faults are trapped is determined by a combination of
10296 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10297 * If enable_ept, L0 doesn't care about page faults and we should
10298 * set all of these to L1's desires. However, if !enable_ept, L0 does
10299 * care about (at least some) page faults, and because it is not easy
10300 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10301 * to exit on each and every L2 page fault. This is done by setting
10302 * MASK=MATCH=0 and (see below) EB.PF=1.
10303 * Note that below we don't need special code to set EB.PF beyond the
10304 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10305 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10306 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10307 *
10308 * A problem with this approach (when !enable_ept) is that L1 may be
10309 * injected with more page faults than it asked for. This could have
10310 * caused problems, but in practice existing hypervisors don't care.
10311 * To fix this, we will need to emulate the PFEC checking (on the L1
10312 * page tables), using walk_addr(), when injecting PFs to L1.
10313 */
10314 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10315 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10316 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10317 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10318
10319 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010320 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010321
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010322 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010323 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010324 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010325 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010326 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010327 if (nested_cpu_has(vmcs12,
10328 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10329 exec_control |= vmcs12->secondary_vm_exec_control;
10330
10331 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10332 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010333 * If translation failed, no matter: This feature asks
10334 * to exit when accessing the given address, and if it
10335 * can never be accessed, this feature won't do
10336 * anything anyway.
10337 */
10338 if (!vmx->nested.apic_access_page)
10339 exec_control &=
10340 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10341 else
10342 vmcs_write64(APIC_ACCESS_ADDR,
10343 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010344 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010345 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010346 exec_control |=
10347 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010348 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010349 }
10350
Wincy Van608406e2015-02-03 23:57:51 +080010351 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10352 vmcs_write64(EOI_EXIT_BITMAP0,
10353 vmcs12->eoi_exit_bitmap0);
10354 vmcs_write64(EOI_EXIT_BITMAP1,
10355 vmcs12->eoi_exit_bitmap1);
10356 vmcs_write64(EOI_EXIT_BITMAP2,
10357 vmcs12->eoi_exit_bitmap2);
10358 vmcs_write64(EOI_EXIT_BITMAP3,
10359 vmcs12->eoi_exit_bitmap3);
10360 vmcs_write16(GUEST_INTR_STATUS,
10361 vmcs12->guest_intr_status);
10362 }
10363
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010364 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10365 }
10366
10367
10368 /*
10369 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10370 * Some constant fields are set here by vmx_set_constant_host_state().
10371 * Other fields are different per CPU, and will be set later when
10372 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10373 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010374 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010375
10376 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010377 * Set the MSR load/store lists to match L0's settings.
10378 */
10379 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010380 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10381 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10382 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10383 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010384
10385 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010386 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10387 * entry, but only if the current (host) sp changed from the value
10388 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10389 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10390 * here we just force the write to happen on entry.
10391 */
10392 vmx->host_rsp = 0;
10393
10394 exec_control = vmx_exec_control(vmx); /* L0's desires */
10395 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10396 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10397 exec_control &= ~CPU_BASED_TPR_SHADOW;
10398 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010399
10400 if (exec_control & CPU_BASED_TPR_SHADOW) {
10401 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10402 page_to_phys(vmx->nested.virtual_apic_page));
10403 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010404 } else {
10405#ifdef CONFIG_X86_64
10406 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10407 CPU_BASED_CR8_STORE_EXITING;
10408#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010409 }
10410
Wincy Van3af18d92015-02-03 23:49:31 +080010411 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010412 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10413 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10414 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10415 else
Wincy Van3af18d92015-02-03 23:49:31 +080010416 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10417
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010418 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010419 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010420 * Rather, exit every time.
10421 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010422 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10423 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10424
10425 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10426
10427 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10428 * bitwise-or of what L1 wants to trap for L2, and what we want to
10429 * trap. Note that CR0.TS also needs updating - we do this later.
10430 */
10431 update_exception_bitmap(vcpu);
10432 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10433 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10434
Nadav Har'El8049d652013-08-05 11:07:06 +030010435 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10436 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10437 * bits are further modified by vmx_set_efer() below.
10438 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010439 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010440
10441 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10442 * emulated by vmx_set_efer(), below.
10443 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010444 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010445 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10446 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010447 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10448
Jan Kiszka44811c02013-08-04 17:17:27 +020010449 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010450 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010451 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10452 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010453 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10454
10455
10456 set_cr4_guest_host_mask(vmx);
10457
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010458 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10459 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10460
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010461 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10462 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010463 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010464 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010465 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010466 if (kvm_has_tsc_control)
10467 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010468
Paolo Bonzini6236b782018-01-16 16:51:18 +010010469 if (cpu_has_vmx_msr_bitmap())
10470 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10471
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010472 if (enable_vpid) {
10473 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010474 * There is no direct mapping between vpid02 and vpid12, the
10475 * vpid02 is per-vCPU for L0 and reused while the value of
10476 * vpid12 is changed w/ one invvpid during nested vmentry.
10477 * The vpid12 is allocated by L1 for L2, so it will not
10478 * influence global bitmap(for vpid01 and vpid02 allocation)
10479 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010480 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010481 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10482 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10483 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10484 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10485 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10486 }
10487 } else {
10488 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10489 vmx_flush_tlb(vcpu);
10490 }
10491
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010492 }
10493
Ladi Prosek560a9792017-04-04 14:18:53 +020010494 if (enable_pml) {
10495 /*
10496 * Conceptually we want to copy the PML address and index from
10497 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10498 * since we always flush the log on each vmexit, this happens
10499 * to be equivalent to simply resetting the fields in vmcs02.
10500 */
10501 ASSERT(vmx->pml_pg);
10502 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10503 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10504 }
10505
Nadav Har'El155a97a2013-08-05 11:07:16 +030010506 if (nested_cpu_has_ept(vmcs12)) {
10507 kvm_mmu_unload(vcpu);
10508 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010509 } else if (nested_cpu_has2(vmcs12,
10510 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10511 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010512 }
10513
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010514 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10515 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010516 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010517 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10518 else
10519 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10520 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10521 vmx_set_efer(vcpu, vcpu->arch.efer);
10522
10523 /*
10524 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10525 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10526 * The CR0_READ_SHADOW is what L2 should have expected to read given
10527 * the specifications by L1; It's not enough to take
10528 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10529 * have more bits than L1 expected.
10530 */
10531 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10532 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10533
10534 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10535 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10536
10537 /* shadow page tables on either EPT or shadow page tables */
10538 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10539 kvm_mmu_reset_context(vcpu);
10540
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010541 if (!enable_ept)
10542 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10543
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010544 /*
10545 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10546 */
10547 if (enable_ept) {
10548 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10549 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10550 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10551 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10552 }
10553
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010554 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10555 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10556}
10557
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010558/*
10559 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10560 * for running an L2 nested guest.
10561 */
10562static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10563{
10564 struct vmcs12 *vmcs12;
10565 struct vcpu_vmx *vmx = to_vmx(vcpu);
10566 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010567 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010568 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010569
10570 if (!nested_vmx_check_permission(vcpu) ||
10571 !nested_vmx_check_vmcs12(vcpu))
10572 return 1;
10573
10574 skip_emulated_instruction(vcpu);
10575 vmcs12 = get_vmcs12(vcpu);
10576
Abel Gordon012f83c2013-04-18 14:39:25 +030010577 if (enable_shadow_vmcs)
10578 copy_shadow_to_vmcs12(vmx);
10579
Nadav Har'El7c177932011-05-25 23:12:04 +030010580 /*
10581 * The nested entry process starts with enforcing various prerequisites
10582 * on vmcs12 as required by the Intel SDM, and act appropriately when
10583 * they fail: As the SDM explains, some conditions should cause the
10584 * instruction to fail, while others will cause the instruction to seem
10585 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10586 * To speed up the normal (success) code path, we should avoid checking
10587 * for misconfigurations which will anyway be caught by the processor
10588 * when using the merged vmcs02.
10589 */
10590 if (vmcs12->launch_state == launch) {
10591 nested_vmx_failValid(vcpu,
10592 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10593 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10594 return 1;
10595 }
10596
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010597 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10598 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010599 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10600 return 1;
10601 }
10602
Wincy Van3af18d92015-02-03 23:49:31 +080010603 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010604 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10605 return 1;
10606 }
10607
Wincy Van3af18d92015-02-03 23:49:31 +080010608 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010609 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10610 return 1;
10611 }
10612
Wincy Vanf2b93282015-02-03 23:56:03 +080010613 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10614 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10615 return 1;
10616 }
10617
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010618 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10619 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10620 return 1;
10621 }
10622
Nadav Har'El7c177932011-05-25 23:12:04 +030010623 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010624 vmx->nested.nested_vmx_true_procbased_ctls_low,
10625 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010626 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010627 vmx->nested.nested_vmx_secondary_ctls_low,
10628 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010629 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010630 vmx->nested.nested_vmx_pinbased_ctls_low,
10631 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010632 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010633 vmx->nested.nested_vmx_true_exit_ctls_low,
10634 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010635 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010636 vmx->nested.nested_vmx_true_entry_ctls_low,
10637 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010638 {
10639 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10640 return 1;
10641 }
10642
10643 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10644 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10645 nested_vmx_failValid(vcpu,
10646 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10647 return 1;
10648 }
10649
Wincy Vanb9c237b2015-02-03 23:56:30 +080010650 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010651 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10652 nested_vmx_entry_failure(vcpu, vmcs12,
10653 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10654 return 1;
10655 }
10656 if (vmcs12->vmcs_link_pointer != -1ull) {
10657 nested_vmx_entry_failure(vcpu, vmcs12,
10658 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10659 return 1;
10660 }
10661
10662 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010663 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010664 * are performed on the field for the IA32_EFER MSR:
10665 * - Bits reserved in the IA32_EFER MSR must be 0.
10666 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10667 * the IA-32e mode guest VM-exit control. It must also be identical
10668 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10669 * CR0.PG) is 1.
10670 */
10671 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10672 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10673 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10674 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10675 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10676 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10677 nested_vmx_entry_failure(vcpu, vmcs12,
10678 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10679 return 1;
10680 }
10681 }
10682
10683 /*
10684 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10685 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10686 * the values of the LMA and LME bits in the field must each be that of
10687 * the host address-space size VM-exit control.
10688 */
10689 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10690 ia32e = (vmcs12->vm_exit_controls &
10691 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10692 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10693 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10694 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10695 nested_vmx_entry_failure(vcpu, vmcs12,
10696 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10697 return 1;
10698 }
10699 }
10700
10701 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010702 * We're finally done with prerequisite checking, and can start with
10703 * the nested entry.
10704 */
10705
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010706 enter_guest_mode(vcpu);
10707
Jan Kiszka2996fca2014-06-16 13:59:43 +020010708 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10709 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10710
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010711 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010712 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010713 vmx_vcpu_put(vcpu);
10714 vmx_vcpu_load(vcpu, cpu);
10715 vcpu->cpu = cpu;
10716 put_cpu();
10717
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010718 vmx_segment_cache_clear(vmx);
10719
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010720 prepare_vmcs02(vcpu, vmcs12);
10721
Wincy Vanff651cb2014-12-11 08:52:58 +030010722 msr_entry_idx = nested_vmx_load_msr(vcpu,
10723 vmcs12->vm_entry_msr_load_addr,
10724 vmcs12->vm_entry_msr_load_count);
10725 if (msr_entry_idx) {
10726 leave_guest_mode(vcpu);
10727 vmx_load_vmcs01(vcpu);
10728 nested_vmx_entry_failure(vcpu, vmcs12,
10729 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10730 return 1;
10731 }
10732
10733 vmcs12->launch_state = 1;
10734
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010735 /* Hide L1D cache contents from the nested guest. */
10736 vmx->vcpu.arch.l1tf_flush_l1d = true;
10737
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010738 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010739 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010740
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010741 vmx->nested.nested_run_pending = 1;
10742
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010743 /*
10744 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10745 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10746 * returned as far as L1 is concerned. It will only return (and set
10747 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10748 */
10749 return 1;
10750}
10751
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010752/*
10753 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10754 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10755 * This function returns the new value we should put in vmcs12.guest_cr0.
10756 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10757 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10758 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10759 * didn't trap the bit, because if L1 did, so would L0).
10760 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10761 * been modified by L2, and L1 knows it. So just leave the old value of
10762 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10763 * isn't relevant, because if L0 traps this bit it can set it to anything.
10764 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10765 * changed these bits, and therefore they need to be updated, but L0
10766 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10767 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10768 */
10769static inline unsigned long
10770vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10771{
10772 return
10773 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10774 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10775 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10776 vcpu->arch.cr0_guest_owned_bits));
10777}
10778
10779static inline unsigned long
10780vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10781{
10782 return
10783 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10784 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10785 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10786 vcpu->arch.cr4_guest_owned_bits));
10787}
10788
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010789static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10790 struct vmcs12 *vmcs12)
10791{
10792 u32 idt_vectoring;
10793 unsigned int nr;
10794
Gleb Natapov851eb6672013-09-25 12:51:34 +030010795 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010796 nr = vcpu->arch.exception.nr;
10797 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10798
10799 if (kvm_exception_is_soft(nr)) {
10800 vmcs12->vm_exit_instruction_len =
10801 vcpu->arch.event_exit_inst_len;
10802 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10803 } else
10804 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10805
10806 if (vcpu->arch.exception.has_error_code) {
10807 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10808 vmcs12->idt_vectoring_error_code =
10809 vcpu->arch.exception.error_code;
10810 }
10811
10812 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010813 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010814 vmcs12->idt_vectoring_info_field =
10815 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10816 } else if (vcpu->arch.interrupt.pending) {
10817 nr = vcpu->arch.interrupt.nr;
10818 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10819
10820 if (vcpu->arch.interrupt.soft) {
10821 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10822 vmcs12->vm_entry_instruction_len =
10823 vcpu->arch.event_exit_inst_len;
10824 } else
10825 idt_vectoring |= INTR_TYPE_EXT_INTR;
10826
10827 vmcs12->idt_vectoring_info_field = idt_vectoring;
10828 }
10829}
10830
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010831static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10832{
10833 struct vcpu_vmx *vmx = to_vmx(vcpu);
10834
Jan Kiszkaf4124502014-03-07 20:03:13 +010010835 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10836 vmx->nested.preemption_timer_expired) {
10837 if (vmx->nested.nested_run_pending)
10838 return -EBUSY;
10839 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10840 return 0;
10841 }
10842
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010843 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010844 if (vmx->nested.nested_run_pending ||
10845 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010846 return -EBUSY;
10847 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10848 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10849 INTR_INFO_VALID_MASK, 0);
10850 /*
10851 * The NMI-triggered VM exit counts as injection:
10852 * clear this one and block further NMIs.
10853 */
10854 vcpu->arch.nmi_pending = 0;
10855 vmx_set_nmi_mask(vcpu, true);
10856 return 0;
10857 }
10858
10859 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10860 nested_exit_on_intr(vcpu)) {
10861 if (vmx->nested.nested_run_pending)
10862 return -EBUSY;
10863 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010864 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010865 }
10866
David Hildenbrand1edccf22017-01-25 11:58:58 +010010867 vmx_complete_nested_posted_interrupt(vcpu);
10868 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010869}
10870
Jan Kiszkaf4124502014-03-07 20:03:13 +010010871static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10872{
10873 ktime_t remaining =
10874 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10875 u64 value;
10876
10877 if (ktime_to_ns(remaining) <= 0)
10878 return 0;
10879
10880 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10881 do_div(value, 1000000);
10882 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10883}
10884
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010885/*
10886 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10887 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10888 * and this function updates it to reflect the changes to the guest state while
10889 * L2 was running (and perhaps made some exits which were handled directly by L0
10890 * without going back to L1), and to reflect the exit reason.
10891 * Note that we do not have to copy here all VMCS fields, just those that
10892 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10893 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10894 * which already writes to vmcs12 directly.
10895 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010896static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10897 u32 exit_reason, u32 exit_intr_info,
10898 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010899{
10900 /* update guest state fields: */
10901 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10902 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10903
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010904 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10905 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10906 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10907
10908 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10909 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10910 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10911 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10912 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10913 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10914 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10915 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10916 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10917 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10918 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10919 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10920 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10921 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10922 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10923 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10924 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10925 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10926 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10927 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10928 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10929 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10930 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10931 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10932 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10933 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10934 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10935 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10936 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10937 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10938 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10939 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10940 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10941 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10942 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10943 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10944
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010945 vmcs12->guest_interruptibility_info =
10946 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10947 vmcs12->guest_pending_dbg_exceptions =
10948 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010949 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10950 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10951 else
10952 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010953
Jan Kiszkaf4124502014-03-07 20:03:13 +010010954 if (nested_cpu_has_preemption_timer(vmcs12)) {
10955 if (vmcs12->vm_exit_controls &
10956 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10957 vmcs12->vmx_preemption_timer_value =
10958 vmx_get_preemption_timer_value(vcpu);
10959 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10960 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010961
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010962 /*
10963 * In some cases (usually, nested EPT), L2 is allowed to change its
10964 * own CR3 without exiting. If it has changed it, we must keep it.
10965 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10966 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10967 *
10968 * Additionally, restore L2's PDPTR to vmcs12.
10969 */
10970 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010971 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010972 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10973 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10974 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10975 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10976 }
10977
Jim Mattson4933e9f2017-06-01 12:44:46 -070010978 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010979
Wincy Van608406e2015-02-03 23:57:51 +080010980 if (nested_cpu_has_vid(vmcs12))
10981 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10982
Jan Kiszkac18911a2013-03-13 16:06:41 +010010983 vmcs12->vm_entry_controls =
10984 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010985 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010986
Jan Kiszka2996fca2014-06-16 13:59:43 +020010987 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10988 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10989 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10990 }
10991
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010992 /* TODO: These cannot have changed unless we have MSR bitmaps and
10993 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010994 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010995 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010996 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10997 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010998 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10999 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11000 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011001 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011002 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080011003 if (nested_cpu_has_xsaves(vmcs12))
11004 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011005
11006 /* update exit information fields: */
11007
Jan Kiszka533558b2014-01-04 18:47:20 +010011008 vmcs12->vm_exit_reason = exit_reason;
11009 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011010
Jan Kiszka533558b2014-01-04 18:47:20 +010011011 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020011012 if ((vmcs12->vm_exit_intr_info &
11013 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
11014 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
11015 vmcs12->vm_exit_intr_error_code =
11016 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011017 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011018 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11019 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11020
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011021 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11022 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11023 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011024 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011025
11026 /*
11027 * Transfer the event that L0 or L1 may wanted to inject into
11028 * L2 to IDT_VECTORING_INFO_FIELD.
11029 */
11030 vmcs12_save_pending_event(vcpu, vmcs12);
11031 }
11032
11033 /*
11034 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11035 * preserved above and would only end up incorrectly in L1.
11036 */
11037 vcpu->arch.nmi_injected = false;
11038 kvm_clear_exception_queue(vcpu);
11039 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011040}
11041
11042/*
11043 * A part of what we need to when the nested L2 guest exits and we want to
11044 * run its L1 parent, is to reset L1's guest state to the host state specified
11045 * in vmcs12.
11046 * This function is to be called not only on normal nested exit, but also on
11047 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11048 * Failures During or After Loading Guest State").
11049 * This function should be called when the active VMCS is L1's (vmcs01).
11050 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011051static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11052 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011053{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011054 struct kvm_segment seg;
11055
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011056 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11057 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011058 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011059 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11060 else
11061 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11062 vmx_set_efer(vcpu, vcpu->arch.efer);
11063
11064 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11065 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011066 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011067 /*
11068 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11069 * actually changed, because it depends on the current state of
11070 * fpu_active (which may have changed).
11071 * Note that vmx_set_cr0 refers to efer set above.
11072 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011073 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011074 /*
11075 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
11076 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
11077 * but we also need to update cr0_guest_host_mask and exception_bitmap.
11078 */
11079 update_exception_bitmap(vcpu);
11080 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
11081 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11082
11083 /*
11084 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
11085 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
11086 */
11087 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080011088 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011089
Jan Kiszka29bf08f2013-12-28 16:31:52 +010011090 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011091
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011092 kvm_set_cr3(vcpu, vmcs12->host_cr3);
11093 kvm_mmu_reset_context(vcpu);
11094
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011095 if (!enable_ept)
11096 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11097
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011098 if (enable_vpid) {
11099 /*
11100 * Trivially support vpid by letting L2s share their parent
11101 * L1's vpid. TODO: move to a more elaborate solution, giving
11102 * each L2 its own vpid and exposing the vpid feature to L1.
11103 */
11104 vmx_flush_tlb(vcpu);
11105 }
11106
11107
11108 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11109 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11110 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11111 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11112 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020011113 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11114 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011115
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011116 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11117 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11118 vmcs_write64(GUEST_BNDCFGS, 0);
11119
Jan Kiszka44811c02013-08-04 17:17:27 +020011120 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011121 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011122 vcpu->arch.pat = vmcs12->host_ia32_pat;
11123 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011124 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11125 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11126 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011127
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011128 /* Set L1 segment info according to Intel SDM
11129 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11130 seg = (struct kvm_segment) {
11131 .base = 0,
11132 .limit = 0xFFFFFFFF,
11133 .selector = vmcs12->host_cs_selector,
11134 .type = 11,
11135 .present = 1,
11136 .s = 1,
11137 .g = 1
11138 };
11139 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11140 seg.l = 1;
11141 else
11142 seg.db = 1;
11143 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11144 seg = (struct kvm_segment) {
11145 .base = 0,
11146 .limit = 0xFFFFFFFF,
11147 .type = 3,
11148 .present = 1,
11149 .s = 1,
11150 .db = 1,
11151 .g = 1
11152 };
11153 seg.selector = vmcs12->host_ds_selector;
11154 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11155 seg.selector = vmcs12->host_es_selector;
11156 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11157 seg.selector = vmcs12->host_ss_selector;
11158 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11159 seg.selector = vmcs12->host_fs_selector;
11160 seg.base = vmcs12->host_fs_base;
11161 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11162 seg.selector = vmcs12->host_gs_selector;
11163 seg.base = vmcs12->host_gs_base;
11164 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11165 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011166 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011167 .limit = 0x67,
11168 .selector = vmcs12->host_tr_selector,
11169 .type = 11,
11170 .present = 1
11171 };
11172 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11173
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011174 kvm_set_dr(vcpu, 7, 0x400);
11175 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011176
Wincy Van3af18d92015-02-03 23:49:31 +080011177 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011178 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011179
Wincy Vanff651cb2014-12-11 08:52:58 +030011180 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11181 vmcs12->vm_exit_msr_load_count))
11182 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011183}
11184
11185/*
11186 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11187 * and modify vmcs12 to make it see what it would expect to see there if
11188 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11189 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011190static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11191 u32 exit_intr_info,
11192 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011193{
11194 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011195 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11196
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011197 /* trying to cancel vmlaunch/vmresume is a bug */
11198 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11199
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011200 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011201 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11202 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011203
Wincy Vanff651cb2014-12-11 08:52:58 +030011204 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11205 vmcs12->vm_exit_msr_store_count))
11206 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11207
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011208 vmx_load_vmcs01(vcpu);
11209
Bandan Das77b0f5d2014-04-19 18:17:45 -040011210 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11211 && nested_exit_intr_ack_set(vcpu)) {
11212 int irq = kvm_cpu_get_interrupt(vcpu);
11213 WARN_ON(irq < 0);
11214 vmcs12->vm_exit_intr_info = irq |
11215 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11216 }
11217
Jan Kiszka542060e2014-01-04 18:47:21 +010011218 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11219 vmcs12->exit_qualification,
11220 vmcs12->idt_vectoring_info_field,
11221 vmcs12->vm_exit_intr_info,
11222 vmcs12->vm_exit_intr_error_code,
11223 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011224
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011225 vm_entry_controls_reset_shadow(vmx);
11226 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011227 vmx_segment_cache_clear(vmx);
11228
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011229 load_vmcs12_host_state(vcpu, vmcs12);
11230
Paolo Bonzini93140062016-07-06 13:23:51 +020011231 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011232 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11233 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011234 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011235 if (vmx->hv_deadline_tsc == -1)
11236 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11237 PIN_BASED_VMX_PREEMPTION_TIMER);
11238 else
11239 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11240 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011241 if (kvm_has_tsc_control)
11242 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011243
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011244 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11245 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11246 vmx_set_virtual_x2apic_mode(vcpu,
11247 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011248 } else if (!nested_cpu_has_ept(vmcs12) &&
11249 nested_cpu_has2(vmcs12,
11250 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11251 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011252 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011253
11254 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11255 vmx->host_rsp = 0;
11256
11257 /* Unpin physical memory we referred to in vmcs02 */
11258 if (vmx->nested.apic_access_page) {
11259 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011260 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011261 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011262 if (vmx->nested.virtual_apic_page) {
11263 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011264 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011265 }
Wincy Van705699a2015-02-03 23:58:17 +080011266 if (vmx->nested.pi_desc_page) {
11267 kunmap(vmx->nested.pi_desc_page);
11268 nested_release_page(vmx->nested.pi_desc_page);
11269 vmx->nested.pi_desc_page = NULL;
11270 vmx->nested.pi_desc = NULL;
11271 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011272
11273 /*
Tang Chen38b99172014-09-24 15:57:54 +080011274 * We are now running in L2, mmu_notifier will force to reload the
11275 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11276 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011277 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011278
11279 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011280 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11281 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11282 * success or failure flag accordingly.
11283 */
11284 if (unlikely(vmx->fail)) {
11285 vmx->fail = 0;
11286 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11287 } else
11288 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011289 if (enable_shadow_vmcs)
11290 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011291
11292 /* in case we halted in L2 */
11293 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011294}
11295
Nadav Har'El7c177932011-05-25 23:12:04 +030011296/*
Jan Kiszka42124922014-01-04 18:47:19 +010011297 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11298 */
11299static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11300{
Wanpeng Lic886f282017-03-06 04:03:28 -080011301 if (is_guest_mode(vcpu)) {
11302 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011303 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011304 }
Jan Kiszka42124922014-01-04 18:47:19 +010011305 free_nested(to_vmx(vcpu));
11306}
11307
11308/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011309 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11310 * 23.7 "VM-entry failures during or after loading guest state" (this also
11311 * lists the acceptable exit-reason and exit-qualification parameters).
11312 * It should only be called before L2 actually succeeded to run, and when
11313 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11314 */
11315static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11316 struct vmcs12 *vmcs12,
11317 u32 reason, unsigned long qualification)
11318{
11319 load_vmcs12_host_state(vcpu, vmcs12);
11320 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11321 vmcs12->exit_qualification = qualification;
11322 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011323 if (enable_shadow_vmcs)
11324 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011325}
11326
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011327static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11328 struct x86_instruction_info *info,
11329 enum x86_intercept_stage stage)
11330{
11331 return X86EMUL_CONTINUE;
11332}
11333
Yunhong Jiang64672c92016-06-13 14:19:59 -070011334#ifdef CONFIG_X86_64
11335/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11336static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11337 u64 divisor, u64 *result)
11338{
11339 u64 low = a << shift, high = a >> (64 - shift);
11340
11341 /* To avoid the overflow on divq */
11342 if (high >= divisor)
11343 return 1;
11344
11345 /* Low hold the result, high hold rem which is discarded */
11346 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11347 "rm" (divisor), "0" (low), "1" (high));
11348 *result = low;
11349
11350 return 0;
11351}
11352
11353static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11354{
11355 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011356 u64 tscl = rdtsc();
11357 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11358 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011359
11360 /* Convert to host delta tsc if tsc scaling is enabled */
11361 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11362 u64_shl_div_u64(delta_tsc,
11363 kvm_tsc_scaling_ratio_frac_bits,
11364 vcpu->arch.tsc_scaling_ratio,
11365 &delta_tsc))
11366 return -ERANGE;
11367
11368 /*
11369 * If the delta tsc can't fit in the 32 bit after the multi shift,
11370 * we can't use the preemption timer.
11371 * It's possible that it fits on later vmentries, but checking
11372 * on every vmentry is costly so we just use an hrtimer.
11373 */
11374 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11375 return -ERANGE;
11376
11377 vmx->hv_deadline_tsc = tscl + delta_tsc;
11378 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11379 PIN_BASED_VMX_PREEMPTION_TIMER);
11380 return 0;
11381}
11382
11383static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11384{
11385 struct vcpu_vmx *vmx = to_vmx(vcpu);
11386 vmx->hv_deadline_tsc = -1;
11387 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11388 PIN_BASED_VMX_PREEMPTION_TIMER);
11389}
11390#endif
11391
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011392static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011393{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011394 if (ple_gap)
11395 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011396}
11397
Kai Huang843e4332015-01-28 10:54:28 +080011398static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11399 struct kvm_memory_slot *slot)
11400{
11401 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11402 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11403}
11404
11405static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11406 struct kvm_memory_slot *slot)
11407{
11408 kvm_mmu_slot_set_dirty(kvm, slot);
11409}
11410
11411static void vmx_flush_log_dirty(struct kvm *kvm)
11412{
11413 kvm_flush_pml_buffers(kvm);
11414}
11415
11416static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11417 struct kvm_memory_slot *memslot,
11418 gfn_t offset, unsigned long mask)
11419{
11420 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11421}
11422
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011423static void __pi_post_block(struct kvm_vcpu *vcpu)
11424{
11425 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11426 struct pi_desc old, new;
11427 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011428
11429 do {
11430 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011431 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11432 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011433
11434 dest = cpu_physical_id(vcpu->cpu);
11435
11436 if (x2apic_enabled())
11437 new.ndst = dest;
11438 else
11439 new.ndst = (dest << 8) & 0xFF00;
11440
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011441 /* set 'NV' to 'notification vector' */
11442 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011443 } while (cmpxchg64(&pi_desc->control, old.control,
11444 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011445
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011446 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11447 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011448 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011449 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011450 vcpu->pre_pcpu = -1;
11451 }
11452}
11453
Feng Wuefc64402015-09-18 22:29:51 +080011454/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011455 * This routine does the following things for vCPU which is going
11456 * to be blocked if VT-d PI is enabled.
11457 * - Store the vCPU to the wakeup list, so when interrupts happen
11458 * we can find the right vCPU to wake up.
11459 * - Change the Posted-interrupt descriptor as below:
11460 * 'NDST' <-- vcpu->pre_pcpu
11461 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11462 * - If 'ON' is set during this process, which means at least one
11463 * interrupt is posted for this vCPU, we cannot block it, in
11464 * this case, return 1, otherwise, return 0.
11465 *
11466 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011467static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011468{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011469 unsigned int dest;
11470 struct pi_desc old, new;
11471 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11472
11473 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011474 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11475 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011476 return 0;
11477
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011478 WARN_ON(irqs_disabled());
11479 local_irq_disable();
11480 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11481 vcpu->pre_pcpu = vcpu->cpu;
11482 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11483 list_add_tail(&vcpu->blocked_vcpu_list,
11484 &per_cpu(blocked_vcpu_on_cpu,
11485 vcpu->pre_pcpu));
11486 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11487 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011488
11489 do {
11490 old.control = new.control = pi_desc->control;
11491
Feng Wubf9f6ac2015-09-18 22:29:55 +080011492 WARN((pi_desc->sn == 1),
11493 "Warning: SN field of posted-interrupts "
11494 "is set before blocking\n");
11495
11496 /*
11497 * Since vCPU can be preempted during this process,
11498 * vcpu->cpu could be different with pre_pcpu, we
11499 * need to set pre_pcpu as the destination of wakeup
11500 * notification event, then we can find the right vCPU
11501 * to wakeup in wakeup handler if interrupts happen
11502 * when the vCPU is in blocked state.
11503 */
11504 dest = cpu_physical_id(vcpu->pre_pcpu);
11505
11506 if (x2apic_enabled())
11507 new.ndst = dest;
11508 else
11509 new.ndst = (dest << 8) & 0xFF00;
11510
11511 /* set 'NV' to 'wakeup vector' */
11512 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011513 } while (cmpxchg64(&pi_desc->control, old.control,
11514 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011515
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011516 /* We should not block the vCPU if an interrupt is posted for it. */
11517 if (pi_test_on(pi_desc) == 1)
11518 __pi_post_block(vcpu);
11519
11520 local_irq_enable();
11521 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011522}
11523
Yunhong Jiangbc225122016-06-13 14:19:58 -070011524static int vmx_pre_block(struct kvm_vcpu *vcpu)
11525{
11526 if (pi_pre_block(vcpu))
11527 return 1;
11528
Yunhong Jiang64672c92016-06-13 14:19:59 -070011529 if (kvm_lapic_hv_timer_in_use(vcpu))
11530 kvm_lapic_switch_to_sw_timer(vcpu);
11531
Yunhong Jiangbc225122016-06-13 14:19:58 -070011532 return 0;
11533}
11534
11535static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011536{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011537 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011538 return;
11539
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011540 WARN_ON(irqs_disabled());
11541 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011542 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011543 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011544}
11545
Yunhong Jiangbc225122016-06-13 14:19:58 -070011546static void vmx_post_block(struct kvm_vcpu *vcpu)
11547{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011548 if (kvm_x86_ops->set_hv_timer)
11549 kvm_lapic_switch_to_hv_timer(vcpu);
11550
Yunhong Jiangbc225122016-06-13 14:19:58 -070011551 pi_post_block(vcpu);
11552}
11553
Feng Wubf9f6ac2015-09-18 22:29:55 +080011554/*
Feng Wuefc64402015-09-18 22:29:51 +080011555 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11556 *
11557 * @kvm: kvm
11558 * @host_irq: host irq of the interrupt
11559 * @guest_irq: gsi of the interrupt
11560 * @set: set or unset PI
11561 * returns 0 on success, < 0 on failure
11562 */
11563static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11564 uint32_t guest_irq, bool set)
11565{
11566 struct kvm_kernel_irq_routing_entry *e;
11567 struct kvm_irq_routing_table *irq_rt;
11568 struct kvm_lapic_irq irq;
11569 struct kvm_vcpu *vcpu;
11570 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011571 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011572
11573 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011574 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11575 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011576 return 0;
11577
11578 idx = srcu_read_lock(&kvm->irq_srcu);
11579 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011580 if (guest_irq >= irq_rt->nr_rt_entries ||
11581 hlist_empty(&irq_rt->map[guest_irq])) {
11582 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11583 guest_irq, irq_rt->nr_rt_entries);
11584 goto out;
11585 }
Feng Wuefc64402015-09-18 22:29:51 +080011586
11587 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11588 if (e->type != KVM_IRQ_ROUTING_MSI)
11589 continue;
11590 /*
11591 * VT-d PI cannot support posting multicast/broadcast
11592 * interrupts to a vCPU, we still use interrupt remapping
11593 * for these kind of interrupts.
11594 *
11595 * For lowest-priority interrupts, we only support
11596 * those with single CPU as the destination, e.g. user
11597 * configures the interrupts via /proc/irq or uses
11598 * irqbalance to make the interrupts single-CPU.
11599 *
11600 * We will support full lowest-priority interrupt later.
11601 */
11602
Radim Krčmář371313132016-07-12 22:09:27 +020011603 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011604 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11605 /*
11606 * Make sure the IRTE is in remapped mode if
11607 * we don't handle it in posted mode.
11608 */
11609 ret = irq_set_vcpu_affinity(host_irq, NULL);
11610 if (ret < 0) {
11611 printk(KERN_INFO
11612 "failed to back to remapped mode, irq: %u\n",
11613 host_irq);
11614 goto out;
11615 }
11616
Feng Wuefc64402015-09-18 22:29:51 +080011617 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011618 }
Feng Wuefc64402015-09-18 22:29:51 +080011619
11620 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11621 vcpu_info.vector = irq.vector;
11622
Feng Wub6ce9782016-01-25 16:53:35 +080011623 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011624 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11625
11626 if (set)
11627 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011628 else
Feng Wuefc64402015-09-18 22:29:51 +080011629 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011630
11631 if (ret < 0) {
11632 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11633 __func__);
11634 goto out;
11635 }
11636 }
11637
11638 ret = 0;
11639out:
11640 srcu_read_unlock(&kvm->irq_srcu, idx);
11641 return ret;
11642}
11643
Ashok Rajc45dcc72016-06-22 14:59:56 +080011644static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11645{
11646 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11647 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11648 FEATURE_CONTROL_LMCE;
11649 else
11650 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11651 ~FEATURE_CONTROL_LMCE;
11652}
11653
Kees Cook404f6aa2016-08-08 16:29:06 -070011654static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011655 .cpu_has_kvm_support = cpu_has_kvm_support,
11656 .disabled_by_bios = vmx_disabled_by_bios,
11657 .hardware_setup = hardware_setup,
11658 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011659 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011660 .hardware_enable = hardware_enable,
11661 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011662 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011663 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011664
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011665 .vm_init = vmx_vm_init,
11666
Avi Kivity6aa8b732006-12-10 02:21:36 -080011667 .vcpu_create = vmx_create_vcpu,
11668 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011669 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011670
Avi Kivity04d2cc72007-09-10 18:10:54 +030011671 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011672 .vcpu_load = vmx_vcpu_load,
11673 .vcpu_put = vmx_vcpu_put,
11674
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011675 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky62d88fc2018-02-21 13:39:51 -060011676 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011677 .get_msr = vmx_get_msr,
11678 .set_msr = vmx_set_msr,
11679 .get_segment_base = vmx_get_segment_base,
11680 .get_segment = vmx_get_segment,
11681 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011682 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011683 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011684 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011685 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011686 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011687 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011688 .set_cr3 = vmx_set_cr3,
11689 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011690 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011691 .get_idt = vmx_get_idt,
11692 .set_idt = vmx_set_idt,
11693 .get_gdt = vmx_get_gdt,
11694 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011695 .get_dr6 = vmx_get_dr6,
11696 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011697 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011698 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011699 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011700 .get_rflags = vmx_get_rflags,
11701 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011702
11703 .get_pkru = vmx_get_pkru,
11704
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011705 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011706 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011707
11708 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011709
Avi Kivity6aa8b732006-12-10 02:21:36 -080011710 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011711 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011712 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011713 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11714 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011715 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011716 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011717 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011718 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011719 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011720 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011721 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011722 .get_nmi_mask = vmx_get_nmi_mask,
11723 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011724 .enable_nmi_window = enable_nmi_window,
11725 .enable_irq_window = enable_irq_window,
11726 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011727 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011728 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011729 .get_enable_apicv = vmx_get_enable_apicv,
11730 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011731 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11732 .hwapic_irr_update = vmx_hwapic_irr_update,
11733 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011734 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11735 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011736
Izik Eiduscbc94022007-10-25 00:29:55 +020011737 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011738 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011739 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011740
Avi Kivity586f9602010-11-18 13:09:54 +020011741 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011742
Sheng Yang17cc3932010-01-05 19:02:27 +080011743 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011744
11745 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011746
11747 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011748 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011749
11750 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011751
11752 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011753
11754 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011755
11756 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011757
11758 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011759 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011760 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011761 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011762
11763 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011764
11765 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011766
11767 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11768 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11769 .flush_log_dirty = vmx_flush_log_dirty,
11770 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011771
Feng Wubf9f6ac2015-09-18 22:29:55 +080011772 .pre_block = vmx_pre_block,
11773 .post_block = vmx_post_block,
11774
Wei Huang25462f72015-06-19 15:45:05 +020011775 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011776
11777 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011778
11779#ifdef CONFIG_X86_64
11780 .set_hv_timer = vmx_set_hv_timer,
11781 .cancel_hv_timer = vmx_cancel_hv_timer,
11782#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011783
11784 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011785};
11786
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011787static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011788{
11789 if (vmx_l1d_flush_pages) {
11790 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11791 vmx_l1d_flush_pages = NULL;
11792 }
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011793 /* Restore state so sysfs ignores VMX */
11794 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011795}
11796
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011797
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011798static void vmx_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011799{
Dave Young2965faa2015-09-09 15:38:55 -070011800#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011801 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011802 synchronize_rcu();
11803#endif
11804
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011805 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011806
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011807 vmx_cleanup_l1d_flush();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011808}
Avi Kivity6aa8b732006-12-10 02:21:36 -080011809module_exit(vmx_exit)
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011810
Avi Kivity6aa8b732006-12-10 02:21:36 -080011811static int __init vmx_init(void)
11812{
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011813 int r;
11814
11815 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11816 __alignof__(struct vcpu_vmx), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011817 if (r)
11818 return r;
11819
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011820 /*
Thomas Gleixner641a2112018-07-13 16:23:19 +020011821 * Must be called after kvm_init() so enable_ept is properly set
11822 * up. Hand the parameter mitigation value in which was stored in
11823 * the pre module init parser. If no parameter was given, it will
11824 * contain 'auto' which will be turned into the default 'cond'
11825 * mitigation mode.
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011826 */
Thomas Gleixner641a2112018-07-13 16:23:19 +020011827 if (boot_cpu_has(X86_BUG_L1TF)) {
11828 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
11829 if (r) {
11830 vmx_exit();
11831 return r;
11832 }
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011833 }
11834
Avi Kivity6aa8b732006-12-10 02:21:36 -080011835#ifdef CONFIG_KEXEC_CORE
11836 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11837 crash_vmclear_local_loaded_vmcss);
11838#endif
11839
11840 return 0;
11841}
Avi Kivity6aa8b732006-12-10 02:21:36 -080011842module_init(vmx_init)