blob: f76caa03f4f80b2b49fa6560a52294167a052357 [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;
Paolo Bonzini87294122018-08-22 16:43:39 +0200201 bool for_parse;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200202} vmentry_l1d_param[] = {
Paolo Bonzini87294122018-08-22 16:43:39 +0200203 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
204 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
205 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
206 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
207 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
208 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200209};
210
Thomas Gleixner641a2112018-07-13 16:23:19 +0200211#define L1D_CACHE_ORDER 4
212static void *vmx_l1d_flush_pages;
213
214static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
215{
216 struct page *page;
Nicolai Stange587d4992018-07-18 19:07:38 +0200217 unsigned int i;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200218
Thomas Gleixner641a2112018-07-13 16:23:19 +0200219 if (!enable_ept) {
220 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
221 return 0;
222 }
223
Paolo Bonzini383f1602018-08-05 16:07:46 +0200224 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
225 u64 msr;
226
227 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
228 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
229 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
230 return 0;
231 }
232 }
233
Jiri Kosina2decbf52018-07-13 16:23:25 +0200234 /* If set to auto use the default l1tf mitigation method */
235 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
236 switch (l1tf_mitigation) {
237 case L1TF_MITIGATION_OFF:
238 l1tf = VMENTER_L1D_FLUSH_NEVER;
239 break;
240 case L1TF_MITIGATION_FLUSH_NOWARN:
241 case L1TF_MITIGATION_FLUSH:
242 case L1TF_MITIGATION_FLUSH_NOSMT:
243 l1tf = VMENTER_L1D_FLUSH_COND;
244 break;
245 case L1TF_MITIGATION_FULL:
246 case L1TF_MITIGATION_FULL_FORCE:
247 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
248 break;
249 }
250 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
251 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
252 }
253
Thomas Gleixner641a2112018-07-13 16:23:19 +0200254 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
255 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
256 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
257 if (!page)
258 return -ENOMEM;
259 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange587d4992018-07-18 19:07:38 +0200260
261 /*
262 * Initialize each page with a different pattern in
263 * order to protect against KSM in the nested
264 * virtualization case.
265 */
266 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
267 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
268 PAGE_SIZE);
269 }
Thomas Gleixner641a2112018-07-13 16:23:19 +0200270 }
271
272 l1tf_vmx_mitigation = l1tf;
273
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200274 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
275 static_branch_enable(&vmx_l1d_should_flush);
276 else
277 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixnerdff09822018-07-13 16:23:20 +0200278
Nicolai Stange936f5662018-07-21 22:25:00 +0200279 if (l1tf == VMENTER_L1D_FLUSH_COND)
280 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200281 else
Nicolai Stange936f5662018-07-21 22:25:00 +0200282 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner641a2112018-07-13 16:23:19 +0200283 return 0;
284}
285
286static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200287{
288 unsigned int i;
289
Thomas Gleixner641a2112018-07-13 16:23:19 +0200290 if (s) {
291 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini87294122018-08-22 16:43:39 +0200292 if (vmentry_l1d_param[i].for_parse &&
293 sysfs_streq(s, vmentry_l1d_param[i].option))
294 return i;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200295 }
296 }
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200297 return -EINVAL;
298}
299
Thomas Gleixner641a2112018-07-13 16:23:19 +0200300static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
301{
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200302 int l1tf, ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200303
Thomas Gleixner641a2112018-07-13 16:23:19 +0200304 l1tf = vmentry_l1d_flush_parse(s);
305 if (l1tf < 0)
306 return l1tf;
307
Paolo Bonzini87294122018-08-22 16:43:39 +0200308 if (!boot_cpu_has(X86_BUG_L1TF))
309 return 0;
310
Thomas Gleixner641a2112018-07-13 16:23:19 +0200311 /*
312 * Has vmx_init() run already? If not then this is the pre init
313 * parameter parsing. In that case just store the value and let
314 * vmx_init() do the proper setup after enable_ept has been
315 * established.
316 */
317 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
318 vmentry_l1d_flush_param = l1tf;
319 return 0;
320 }
321
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200322 mutex_lock(&vmx_l1d_flush_mutex);
323 ret = vmx_setup_l1d_flush(l1tf);
324 mutex_unlock(&vmx_l1d_flush_mutex);
325 return ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200326}
327
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200328static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
329{
Paolo Bonzini87294122018-08-22 16:43:39 +0200330 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
331 return sprintf(s, "???\n");
332
Thomas Gleixner641a2112018-07-13 16:23:19 +0200333 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200334}
335
336static const struct kernel_param_ops vmentry_l1d_flush_ops = {
337 .set = vmentry_l1d_flush_set,
338 .get = vmentry_l1d_flush_get,
339};
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200340module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200341
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200342#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300343
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400344struct vmcs {
345 u32 revision_id;
346 u32 abort;
347 char data[0];
348};
349
Nadav Har'Eld462b812011-05-24 15:26:10 +0300350/*
351 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
352 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
353 * loaded on this CPU (so we can clear them if the CPU goes down).
354 */
355struct loaded_vmcs {
356 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700357 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300358 int cpu;
359 int launched;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100360 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300361 struct list_head loaded_vmcss_on_cpu_link;
362};
363
Avi Kivity26bb0982009-09-07 11:14:12 +0300364struct shared_msr_entry {
365 unsigned index;
366 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200367 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300368};
369
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300370/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300371 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
372 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
373 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
374 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
375 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
376 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson46e24df2017-11-27 17:22:25 -0600377 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300378 * underlying hardware which will be used to run L2.
379 * This structure is packed to ensure that its layout is identical across
380 * machines (necessary for live migration).
381 * If there are changes in this struct, VMCS12_REVISION must be changed.
382 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300383typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300384struct __packed vmcs12 {
385 /* According to the Intel spec, a VMCS region must start with the
386 * following two fields. Then follow implementation-specific data.
387 */
388 u32 revision_id;
389 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300390
Nadav Har'El27d6c862011-05-25 23:06:59 +0300391 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
392 u32 padding[7]; /* room for future expansion */
393
Nadav Har'El22bd0352011-05-25 23:05:57 +0300394 u64 io_bitmap_a;
395 u64 io_bitmap_b;
396 u64 msr_bitmap;
397 u64 vm_exit_msr_store_addr;
398 u64 vm_exit_msr_load_addr;
399 u64 vm_entry_msr_load_addr;
400 u64 tsc_offset;
401 u64 virtual_apic_page_addr;
402 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800403 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300404 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800405 u64 eoi_exit_bitmap0;
406 u64 eoi_exit_bitmap1;
407 u64 eoi_exit_bitmap2;
408 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800409 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300410 u64 guest_physical_address;
411 u64 vmcs_link_pointer;
412 u64 guest_ia32_debugctl;
413 u64 guest_ia32_pat;
414 u64 guest_ia32_efer;
415 u64 guest_ia32_perf_global_ctrl;
416 u64 guest_pdptr0;
417 u64 guest_pdptr1;
418 u64 guest_pdptr2;
419 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100420 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300421 u64 host_ia32_pat;
422 u64 host_ia32_efer;
423 u64 host_ia32_perf_global_ctrl;
424 u64 padding64[8]; /* room for future expansion */
425 /*
426 * To allow migration of L1 (complete with its L2 guests) between
427 * machines of different natural widths (32 or 64 bit), we cannot have
428 * unsigned long fields with no explict size. We use u64 (aliased
429 * natural_width) instead. Luckily, x86 is little-endian.
430 */
431 natural_width cr0_guest_host_mask;
432 natural_width cr4_guest_host_mask;
433 natural_width cr0_read_shadow;
434 natural_width cr4_read_shadow;
435 natural_width cr3_target_value0;
436 natural_width cr3_target_value1;
437 natural_width cr3_target_value2;
438 natural_width cr3_target_value3;
439 natural_width exit_qualification;
440 natural_width guest_linear_address;
441 natural_width guest_cr0;
442 natural_width guest_cr3;
443 natural_width guest_cr4;
444 natural_width guest_es_base;
445 natural_width guest_cs_base;
446 natural_width guest_ss_base;
447 natural_width guest_ds_base;
448 natural_width guest_fs_base;
449 natural_width guest_gs_base;
450 natural_width guest_ldtr_base;
451 natural_width guest_tr_base;
452 natural_width guest_gdtr_base;
453 natural_width guest_idtr_base;
454 natural_width guest_dr7;
455 natural_width guest_rsp;
456 natural_width guest_rip;
457 natural_width guest_rflags;
458 natural_width guest_pending_dbg_exceptions;
459 natural_width guest_sysenter_esp;
460 natural_width guest_sysenter_eip;
461 natural_width host_cr0;
462 natural_width host_cr3;
463 natural_width host_cr4;
464 natural_width host_fs_base;
465 natural_width host_gs_base;
466 natural_width host_tr_base;
467 natural_width host_gdtr_base;
468 natural_width host_idtr_base;
469 natural_width host_ia32_sysenter_esp;
470 natural_width host_ia32_sysenter_eip;
471 natural_width host_rsp;
472 natural_width host_rip;
473 natural_width paddingl[8]; /* room for future expansion */
474 u32 pin_based_vm_exec_control;
475 u32 cpu_based_vm_exec_control;
476 u32 exception_bitmap;
477 u32 page_fault_error_code_mask;
478 u32 page_fault_error_code_match;
479 u32 cr3_target_count;
480 u32 vm_exit_controls;
481 u32 vm_exit_msr_store_count;
482 u32 vm_exit_msr_load_count;
483 u32 vm_entry_controls;
484 u32 vm_entry_msr_load_count;
485 u32 vm_entry_intr_info_field;
486 u32 vm_entry_exception_error_code;
487 u32 vm_entry_instruction_len;
488 u32 tpr_threshold;
489 u32 secondary_vm_exec_control;
490 u32 vm_instruction_error;
491 u32 vm_exit_reason;
492 u32 vm_exit_intr_info;
493 u32 vm_exit_intr_error_code;
494 u32 idt_vectoring_info_field;
495 u32 idt_vectoring_error_code;
496 u32 vm_exit_instruction_len;
497 u32 vmx_instruction_info;
498 u32 guest_es_limit;
499 u32 guest_cs_limit;
500 u32 guest_ss_limit;
501 u32 guest_ds_limit;
502 u32 guest_fs_limit;
503 u32 guest_gs_limit;
504 u32 guest_ldtr_limit;
505 u32 guest_tr_limit;
506 u32 guest_gdtr_limit;
507 u32 guest_idtr_limit;
508 u32 guest_es_ar_bytes;
509 u32 guest_cs_ar_bytes;
510 u32 guest_ss_ar_bytes;
511 u32 guest_ds_ar_bytes;
512 u32 guest_fs_ar_bytes;
513 u32 guest_gs_ar_bytes;
514 u32 guest_ldtr_ar_bytes;
515 u32 guest_tr_ar_bytes;
516 u32 guest_interruptibility_info;
517 u32 guest_activity_state;
518 u32 guest_sysenter_cs;
519 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100520 u32 vmx_preemption_timer_value;
521 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300522 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800523 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300524 u16 guest_es_selector;
525 u16 guest_cs_selector;
526 u16 guest_ss_selector;
527 u16 guest_ds_selector;
528 u16 guest_fs_selector;
529 u16 guest_gs_selector;
530 u16 guest_ldtr_selector;
531 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800532 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300533 u16 host_es_selector;
534 u16 host_cs_selector;
535 u16 host_ss_selector;
536 u16 host_ds_selector;
537 u16 host_fs_selector;
538 u16 host_gs_selector;
539 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300540};
541
542/*
543 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
544 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
545 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
546 */
547#define VMCS12_REVISION 0x11e57ed0
548
549/*
550 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
551 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
552 * current implementation, 4K are reserved to avoid future complications.
553 */
554#define VMCS12_SIZE 0x1000
555
556/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300557 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
558 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
559 */
560struct nested_vmx {
561 /* Has the level1 guest done vmxon? */
562 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400563 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300564
565 /* The guest-physical address of the current VMCS L1 keeps for L2 */
566 gpa_t current_vmptr;
567 /* The host-usable pointer to the above */
568 struct page *current_vmcs12_page;
569 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700570 /*
571 * Cache of the guest's VMCS, existing outside of guest memory.
572 * Loaded from guest memory during VMPTRLD. Flushed to guest
573 * memory during VMXOFF, VMCLEAR, VMPTRLD.
574 */
575 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300576 /*
577 * Indicates if the shadow vmcs must be updated with the
578 * data hold by vmcs12
579 */
580 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300581
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200582 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300583 /* L2 must run next, and mustn't decide to exit to L1. */
584 bool nested_run_pending;
Jim Mattson46e24df2017-11-27 17:22:25 -0600585
586 struct loaded_vmcs vmcs02;
587
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300588 /*
Jim Mattson46e24df2017-11-27 17:22:25 -0600589 * Guest pages referred to in the vmcs02 with host-physical
590 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300591 */
592 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800593 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800594 struct page *pi_desc_page;
595 struct pi_desc *pi_desc;
596 bool pi_pending;
597 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100598
599 struct hrtimer preemption_timer;
600 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200601
602 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
603 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800604
Wanpeng Li5c614b32015-10-13 09:18:36 -0700605 u16 vpid02;
606 u16 last_vpid;
607
Wincy Vanb9c237b2015-02-03 23:56:30 +0800608 u32 nested_vmx_procbased_ctls_low;
609 u32 nested_vmx_procbased_ctls_high;
610 u32 nested_vmx_true_procbased_ctls_low;
611 u32 nested_vmx_secondary_ctls_low;
612 u32 nested_vmx_secondary_ctls_high;
613 u32 nested_vmx_pinbased_ctls_low;
614 u32 nested_vmx_pinbased_ctls_high;
615 u32 nested_vmx_exit_ctls_low;
616 u32 nested_vmx_exit_ctls_high;
617 u32 nested_vmx_true_exit_ctls_low;
618 u32 nested_vmx_entry_ctls_low;
619 u32 nested_vmx_entry_ctls_high;
620 u32 nested_vmx_true_entry_ctls_low;
621 u32 nested_vmx_misc_low;
622 u32 nested_vmx_misc_high;
623 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700624 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300625};
626
Yang Zhang01e439b2013-04-11 19:25:12 +0800627#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800628#define POSTED_INTR_SN 1
629
Yang Zhang01e439b2013-04-11 19:25:12 +0800630/* Posted-Interrupt Descriptor */
631struct pi_desc {
632 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800633 union {
634 struct {
635 /* bit 256 - Outstanding Notification */
636 u16 on : 1,
637 /* bit 257 - Suppress Notification */
638 sn : 1,
639 /* bit 271:258 - Reserved */
640 rsvd_1 : 14;
641 /* bit 279:272 - Notification Vector */
642 u8 nv;
643 /* bit 287:280 - Reserved */
644 u8 rsvd_2;
645 /* bit 319:288 - Notification Destination */
646 u32 ndst;
647 };
648 u64 control;
649 };
650 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800651} __aligned(64);
652
Yang Zhanga20ed542013-04-11 19:25:15 +0800653static bool pi_test_and_set_on(struct pi_desc *pi_desc)
654{
655 return test_and_set_bit(POSTED_INTR_ON,
656 (unsigned long *)&pi_desc->control);
657}
658
659static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
660{
661 return test_and_clear_bit(POSTED_INTR_ON,
662 (unsigned long *)&pi_desc->control);
663}
664
665static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
666{
667 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
668}
669
Feng Wuebbfc762015-09-18 22:29:46 +0800670static inline void pi_clear_sn(struct pi_desc *pi_desc)
671{
672 return clear_bit(POSTED_INTR_SN,
673 (unsigned long *)&pi_desc->control);
674}
675
676static inline void pi_set_sn(struct pi_desc *pi_desc)
677{
678 return set_bit(POSTED_INTR_SN,
679 (unsigned long *)&pi_desc->control);
680}
681
682static inline int pi_test_on(struct pi_desc *pi_desc)
683{
684 return test_bit(POSTED_INTR_ON,
685 (unsigned long *)&pi_desc->control);
686}
687
688static inline int pi_test_sn(struct pi_desc *pi_desc)
689{
690 return test_bit(POSTED_INTR_SN,
691 (unsigned long *)&pi_desc->control);
692}
693
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400694struct vmx_msrs {
695 unsigned int nr;
696 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
697};
698
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400699struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000700 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300701 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300702 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200703 bool nmi_known_unmasked;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100704 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300705 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200706 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200707 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300708 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400709 int nmsrs;
710 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800711 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400712#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300713 u64 msr_host_kernel_gs_base;
714 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400715#endif
Ashok Raj70131292018-02-01 22:59:43 +0100716
KarimAllah Ahmede5a83412018-02-01 22:59:45 +0100717 u64 spec_ctrl;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100718
Gleb Natapov2961e8762013-11-25 15:37:13 +0200719 u32 vm_entry_controls_shadow;
720 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300721 /*
722 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
723 * non-nested (L1) guest, it always points to vmcs01. For a nested
724 * guest (L2), it points to a different VMCS.
725 */
726 struct loaded_vmcs vmcs01;
727 struct loaded_vmcs *loaded_vmcs;
728 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300729 struct msr_autoload {
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400730 struct vmx_msrs guest;
731 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300732 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400733 struct {
734 int loaded;
735 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300736#ifdef CONFIG_X86_64
737 u16 ds_sel, es_sel;
738#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200739 int gs_ldt_reload_needed;
740 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000741 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700742 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400743 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200744 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300745 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300746 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300747 struct kvm_segment segs[8];
748 } rmode;
749 struct {
750 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300751 struct kvm_save_segment {
752 u16 selector;
753 unsigned long base;
754 u32 limit;
755 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300756 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300757 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800758 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300759 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200760
761 /* Support for vnmi-less CPUs */
762 int soft_vnmi_blocked;
763 ktime_t entry_time;
764 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800765 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800766
Yang Zhang01e439b2013-04-11 19:25:12 +0800767 /* Posted interrupt descriptor */
768 struct pi_desc pi_desc;
769
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300770 /* Support for a guest hypervisor (nested VMX) */
771 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200772
773 /* Dynamic PLE window. */
774 int ple_window;
775 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800776
777 /* Support for PML */
778#define PML_ENTITY_NUM 512
779 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800780
Yunhong Jiang64672c92016-06-13 14:19:59 -0700781 /* apic deadline value in host tsc */
782 u64 hv_deadline_tsc;
783
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800784 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800785
786 bool guest_pkru_valid;
787 u32 guest_pkru;
788 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800789
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800790 /*
791 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
792 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
793 * in msr_ia32_feature_control_valid_bits.
794 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800795 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800796 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400797};
798
Avi Kivity2fb92db2011-04-27 19:42:18 +0300799enum segment_cache_field {
800 SEG_FIELD_SEL = 0,
801 SEG_FIELD_BASE = 1,
802 SEG_FIELD_LIMIT = 2,
803 SEG_FIELD_AR = 3,
804
805 SEG_FIELD_NR = 4
806};
807
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400808static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
809{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000810 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400811}
812
Feng Wuefc64402015-09-18 22:29:51 +0800813static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
814{
815 return &(to_vmx(vcpu)->pi_desc);
816}
817
Nadav Har'El22bd0352011-05-25 23:05:57 +0300818#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
819#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
820#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
821 [number##_HIGH] = VMCS12_OFFSET(name)+4
822
Abel Gordon4607c2d2013-04-18 14:35:55 +0300823
Bandan Dasfe2b2012014-04-21 15:20:14 -0400824static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300825 /*
826 * We do NOT shadow fields that are modified when L0
827 * traps and emulates any vmx instruction (e.g. VMPTRLD,
828 * VMXON...) executed by L1.
829 * For example, VM_INSTRUCTION_ERROR is read
830 * by L1 if a vmx instruction fails (part of the error path).
831 * Note the code assumes this logic. If for some reason
832 * we start shadowing these fields then we need to
833 * force a shadow sync when L0 emulates vmx instructions
834 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
835 * by nested_vmx_failValid)
836 */
837 VM_EXIT_REASON,
838 VM_EXIT_INTR_INFO,
839 VM_EXIT_INSTRUCTION_LEN,
840 IDT_VECTORING_INFO_FIELD,
841 IDT_VECTORING_ERROR_CODE,
842 VM_EXIT_INTR_ERROR_CODE,
843 EXIT_QUALIFICATION,
844 GUEST_LINEAR_ADDRESS,
845 GUEST_PHYSICAL_ADDRESS
846};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400847static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300848 ARRAY_SIZE(shadow_read_only_fields);
849
Bandan Dasfe2b2012014-04-21 15:20:14 -0400850static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800851 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300852 GUEST_RIP,
853 GUEST_RSP,
854 GUEST_CR0,
855 GUEST_CR3,
856 GUEST_CR4,
857 GUEST_INTERRUPTIBILITY_INFO,
858 GUEST_RFLAGS,
859 GUEST_CS_SELECTOR,
860 GUEST_CS_AR_BYTES,
861 GUEST_CS_LIMIT,
862 GUEST_CS_BASE,
863 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100864 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300865 CR0_GUEST_HOST_MASK,
866 CR0_READ_SHADOW,
867 CR4_READ_SHADOW,
868 TSC_OFFSET,
869 EXCEPTION_BITMAP,
870 CPU_BASED_VM_EXEC_CONTROL,
871 VM_ENTRY_EXCEPTION_ERROR_CODE,
872 VM_ENTRY_INTR_INFO_FIELD,
873 VM_ENTRY_INSTRUCTION_LEN,
874 VM_ENTRY_EXCEPTION_ERROR_CODE,
875 HOST_FS_BASE,
876 HOST_GS_BASE,
877 HOST_FS_SELECTOR,
878 HOST_GS_SELECTOR
879};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400880static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300881 ARRAY_SIZE(shadow_read_write_fields);
882
Mathias Krause772e0312012-08-30 01:30:19 +0200883static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300884 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800885 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300886 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
887 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
888 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
889 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
890 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
891 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
892 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
893 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800894 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300895 FIELD(HOST_ES_SELECTOR, host_es_selector),
896 FIELD(HOST_CS_SELECTOR, host_cs_selector),
897 FIELD(HOST_SS_SELECTOR, host_ss_selector),
898 FIELD(HOST_DS_SELECTOR, host_ds_selector),
899 FIELD(HOST_FS_SELECTOR, host_fs_selector),
900 FIELD(HOST_GS_SELECTOR, host_gs_selector),
901 FIELD(HOST_TR_SELECTOR, host_tr_selector),
902 FIELD64(IO_BITMAP_A, io_bitmap_a),
903 FIELD64(IO_BITMAP_B, io_bitmap_b),
904 FIELD64(MSR_BITMAP, msr_bitmap),
905 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
906 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
907 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
908 FIELD64(TSC_OFFSET, tsc_offset),
909 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
910 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800911 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300912 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800913 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
914 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
915 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
916 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800917 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300918 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
919 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
920 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
921 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
922 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
923 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
924 FIELD64(GUEST_PDPTR0, guest_pdptr0),
925 FIELD64(GUEST_PDPTR1, guest_pdptr1),
926 FIELD64(GUEST_PDPTR2, guest_pdptr2),
927 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100928 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300929 FIELD64(HOST_IA32_PAT, host_ia32_pat),
930 FIELD64(HOST_IA32_EFER, host_ia32_efer),
931 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
932 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
933 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
934 FIELD(EXCEPTION_BITMAP, exception_bitmap),
935 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
936 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
937 FIELD(CR3_TARGET_COUNT, cr3_target_count),
938 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
939 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
940 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
941 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
942 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
943 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
944 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
945 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
946 FIELD(TPR_THRESHOLD, tpr_threshold),
947 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
948 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
949 FIELD(VM_EXIT_REASON, vm_exit_reason),
950 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
951 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
952 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
953 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
954 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
955 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
956 FIELD(GUEST_ES_LIMIT, guest_es_limit),
957 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
958 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
959 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
960 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
961 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
962 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
963 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
964 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
965 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
966 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
967 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
968 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
969 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
970 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
971 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
972 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
973 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
974 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
975 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
976 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
977 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100978 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300979 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
980 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
981 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
982 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
983 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
984 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
985 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
986 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
987 FIELD(EXIT_QUALIFICATION, exit_qualification),
988 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
989 FIELD(GUEST_CR0, guest_cr0),
990 FIELD(GUEST_CR3, guest_cr3),
991 FIELD(GUEST_CR4, guest_cr4),
992 FIELD(GUEST_ES_BASE, guest_es_base),
993 FIELD(GUEST_CS_BASE, guest_cs_base),
994 FIELD(GUEST_SS_BASE, guest_ss_base),
995 FIELD(GUEST_DS_BASE, guest_ds_base),
996 FIELD(GUEST_FS_BASE, guest_fs_base),
997 FIELD(GUEST_GS_BASE, guest_gs_base),
998 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
999 FIELD(GUEST_TR_BASE, guest_tr_base),
1000 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1001 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1002 FIELD(GUEST_DR7, guest_dr7),
1003 FIELD(GUEST_RSP, guest_rsp),
1004 FIELD(GUEST_RIP, guest_rip),
1005 FIELD(GUEST_RFLAGS, guest_rflags),
1006 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1007 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1008 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1009 FIELD(HOST_CR0, host_cr0),
1010 FIELD(HOST_CR3, host_cr3),
1011 FIELD(HOST_CR4, host_cr4),
1012 FIELD(HOST_FS_BASE, host_fs_base),
1013 FIELD(HOST_GS_BASE, host_gs_base),
1014 FIELD(HOST_TR_BASE, host_tr_base),
1015 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1016 FIELD(HOST_IDTR_BASE, host_idtr_base),
1017 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1018 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1019 FIELD(HOST_RSP, host_rsp),
1020 FIELD(HOST_RIP, host_rip),
1021};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001022
1023static inline short vmcs_field_to_offset(unsigned long field)
1024{
Dan Williamseb99bd62018-01-31 17:47:03 -08001025 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1026 unsigned short offset;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001027
Dan Williamseb99bd62018-01-31 17:47:03 -08001028 BUILD_BUG_ON(size > SHRT_MAX);
1029 if (field >= size)
Andrew Honig012df712018-01-10 10:12:03 -08001030 return -ENOENT;
1031
Dan Williamseb99bd62018-01-31 17:47:03 -08001032 field = array_index_nospec(field, size);
1033 offset = vmcs_field_to_offset_table[field];
1034 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001035 return -ENOENT;
Dan Williamseb99bd62018-01-31 17:47:03 -08001036 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001037}
1038
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001039static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1040{
David Matlack4f2777b2016-07-13 17:16:37 -07001041 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001042}
1043
1044static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
1045{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001046 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08001047 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001048 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +08001049
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001050 return page;
1051}
1052
1053static void nested_release_page(struct page *page)
1054{
1055 kvm_release_page_dirty(page);
1056}
1057
1058static void nested_release_page_clean(struct page *page)
1059{
1060 kvm_release_page_clean(page);
1061}
1062
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001063static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +08001064static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001065static void kvm_cpu_vmxon(u64 addr);
1066static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001067static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +02001068static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001069static void vmx_set_segment(struct kvm_vcpu *vcpu,
1070 struct kvm_segment *var, int seg);
1071static void vmx_get_segment(struct kvm_vcpu *vcpu,
1072 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001073static bool guest_state_valid(struct kvm_vcpu *vcpu);
1074static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +03001075static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +03001076static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +08001077static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzini6236b782018-01-16 16:51:18 +01001078static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Yi Wang0c4d57a2018-11-08 11:22:21 +08001079static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj70131292018-02-01 22:59:43 +01001080 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001081
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1083static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001084/*
1085 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1086 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1087 */
1088static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +03001089static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090
Feng Wubf9f6ac2015-09-18 22:29:55 +08001091/*
1092 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1093 * can find which vCPU should be waken up.
1094 */
1095static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1096static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1097
Avi Kivity3e7c73e2009-02-24 21:46:19 +02001098static unsigned long *vmx_io_bitmap_a;
1099static unsigned long *vmx_io_bitmap_b;
Abel Gordon4607c2d2013-04-18 14:35:55 +03001100static unsigned long *vmx_vmread_bitmap;
1101static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +03001102
Avi Kivity110312c2010-12-21 12:54:20 +02001103static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001104static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001105
Sheng Yang2384d2b2008-01-17 15:14:33 +08001106static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1107static DEFINE_SPINLOCK(vmx_vpid_lock);
1108
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001109static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 int size;
1111 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001112 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001114 u32 pin_based_exec_ctrl;
1115 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001116 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001117 u32 vmexit_ctrl;
1118 u32 vmentry_ctrl;
1119} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120
Hannes Ederefff9e52008-11-28 17:02:06 +01001121static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001122 u32 ept;
1123 u32 vpid;
1124} vmx_capability;
1125
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126#define VMX_SEGMENT_FIELD(seg) \
1127 [VCPU_SREG_##seg] = { \
1128 .selector = GUEST_##seg##_SELECTOR, \
1129 .base = GUEST_##seg##_BASE, \
1130 .limit = GUEST_##seg##_LIMIT, \
1131 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1132 }
1133
Mathias Krause772e0312012-08-30 01:30:19 +02001134static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 unsigned selector;
1136 unsigned base;
1137 unsigned limit;
1138 unsigned ar_bytes;
1139} kvm_vmx_segment_fields[] = {
1140 VMX_SEGMENT_FIELD(CS),
1141 VMX_SEGMENT_FIELD(DS),
1142 VMX_SEGMENT_FIELD(ES),
1143 VMX_SEGMENT_FIELD(FS),
1144 VMX_SEGMENT_FIELD(GS),
1145 VMX_SEGMENT_FIELD(SS),
1146 VMX_SEGMENT_FIELD(TR),
1147 VMX_SEGMENT_FIELD(LDTR),
1148};
1149
Avi Kivity26bb0982009-09-07 11:14:12 +03001150static u64 host_efer;
1151
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001152static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1153
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001154/*
Brian Gerst8c065852010-07-17 09:03:26 -04001155 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001156 * away by decrementing the array size.
1157 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001159#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001160 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001162 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164
Jan Kiszka5bb16012016-02-09 20:14:21 +01001165static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166{
1167 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1168 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001169 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1170}
1171
Jan Kiszka6f054852016-02-09 20:15:18 +01001172static inline bool is_debug(u32 intr_info)
1173{
1174 return is_exception_n(intr_info, DB_VECTOR);
1175}
1176
1177static inline bool is_breakpoint(u32 intr_info)
1178{
1179 return is_exception_n(intr_info, BP_VECTOR);
1180}
1181
Jan Kiszka5bb16012016-02-09 20:14:21 +01001182static inline bool is_page_fault(u32 intr_info)
1183{
1184 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185}
1186
Gui Jianfeng31299942010-03-15 17:29:09 +08001187static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001188{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001189 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001190}
1191
Gui Jianfeng31299942010-03-15 17:29:09 +08001192static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001193{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001194 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001195}
1196
Gui Jianfeng31299942010-03-15 17:29:09 +08001197static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198{
1199 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1200 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1201}
1202
Gui Jianfeng31299942010-03-15 17:29:09 +08001203static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001204{
1205 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1206 INTR_INFO_VALID_MASK)) ==
1207 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1208}
1209
Linus Torvalds587da2b2018-03-20 12:16:59 -07001210/* Undocumented: icebp/int1 */
1211static inline bool is_icebp(u32 intr_info)
1212{
1213 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1214 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1215}
1216
Gui Jianfeng31299942010-03-15 17:29:09 +08001217static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001218{
Sheng Yang04547152009-04-01 15:52:31 +08001219 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001220}
1221
Gui Jianfeng31299942010-03-15 17:29:09 +08001222static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001223{
Sheng Yang04547152009-04-01 15:52:31 +08001224 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001225}
1226
Paolo Bonzini35754c92015-07-29 12:05:37 +02001227static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001228{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001229 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001230}
1231
Gui Jianfeng31299942010-03-15 17:29:09 +08001232static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001233{
Sheng Yang04547152009-04-01 15:52:31 +08001234 return vmcs_config.cpu_based_exec_ctrl &
1235 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001236}
1237
Avi Kivity774ead32007-12-26 13:57:04 +02001238static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001239{
Sheng Yang04547152009-04-01 15:52:31 +08001240 return vmcs_config.cpu_based_2nd_exec_ctrl &
1241 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1242}
1243
Yang Zhang8d146952013-01-25 10:18:50 +08001244static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1245{
1246 return vmcs_config.cpu_based_2nd_exec_ctrl &
1247 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1248}
1249
Yang Zhang83d4c282013-01-25 10:18:49 +08001250static inline bool cpu_has_vmx_apic_register_virt(void)
1251{
1252 return vmcs_config.cpu_based_2nd_exec_ctrl &
1253 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1254}
1255
Yang Zhangc7c9c562013-01-25 10:18:51 +08001256static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1257{
1258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1260}
1261
Yunhong Jiang64672c92016-06-13 14:19:59 -07001262/*
1263 * Comment's format: document - errata name - stepping - processor name.
1264 * Refer from
1265 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1266 */
1267static u32 vmx_preemption_cpu_tfms[] = {
1268/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12690x000206E6,
1270/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1271/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1272/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12730x00020652,
1274/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12750x00020655,
1276/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1277/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1278/*
1279 * 320767.pdf - AAP86 - B1 -
1280 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1281 */
12820x000106E5,
1283/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
12840x000106A0,
1285/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
12860x000106A1,
1287/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
12880x000106A4,
1289 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1290 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1291 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
12920x000106A5,
1293};
1294
1295static inline bool cpu_has_broken_vmx_preemption_timer(void)
1296{
1297 u32 eax = cpuid_eax(0x00000001), i;
1298
1299 /* Clear the reserved bits */
1300 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001301 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001302 if (eax == vmx_preemption_cpu_tfms[i])
1303 return true;
1304
1305 return false;
1306}
1307
1308static inline bool cpu_has_vmx_preemption_timer(void)
1309{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001310 return vmcs_config.pin_based_exec_ctrl &
1311 PIN_BASED_VMX_PREEMPTION_TIMER;
1312}
1313
Yang Zhang01e439b2013-04-11 19:25:12 +08001314static inline bool cpu_has_vmx_posted_intr(void)
1315{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001316 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1317 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001318}
1319
1320static inline bool cpu_has_vmx_apicv(void)
1321{
1322 return cpu_has_vmx_apic_register_virt() &&
1323 cpu_has_vmx_virtual_intr_delivery() &&
1324 cpu_has_vmx_posted_intr();
1325}
1326
Sheng Yang04547152009-04-01 15:52:31 +08001327static inline bool cpu_has_vmx_flexpriority(void)
1328{
1329 return cpu_has_vmx_tpr_shadow() &&
1330 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001331}
1332
Marcelo Tosattie7997942009-06-11 12:07:40 -03001333static inline bool cpu_has_vmx_ept_execute_only(void)
1334{
Gui Jianfeng31299942010-03-15 17:29:09 +08001335 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001336}
1337
Marcelo Tosattie7997942009-06-11 12:07:40 -03001338static inline bool cpu_has_vmx_ept_2m_page(void)
1339{
Gui Jianfeng31299942010-03-15 17:29:09 +08001340 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001341}
1342
Sheng Yang878403b2010-01-05 19:02:29 +08001343static inline bool cpu_has_vmx_ept_1g_page(void)
1344{
Gui Jianfeng31299942010-03-15 17:29:09 +08001345 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001346}
1347
Sheng Yang4bc9b982010-06-02 14:05:24 +08001348static inline bool cpu_has_vmx_ept_4levels(void)
1349{
1350 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1351}
1352
Xudong Hao83c3a332012-05-28 19:33:35 +08001353static inline bool cpu_has_vmx_ept_ad_bits(void)
1354{
1355 return vmx_capability.ept & VMX_EPT_AD_BIT;
1356}
1357
Gui Jianfeng31299942010-03-15 17:29:09 +08001358static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001359{
Gui Jianfeng31299942010-03-15 17:29:09 +08001360 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001361}
1362
Gui Jianfeng31299942010-03-15 17:29:09 +08001363static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001364{
Gui Jianfeng31299942010-03-15 17:29:09 +08001365 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001366}
1367
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001368static inline bool cpu_has_vmx_invvpid_single(void)
1369{
1370 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1371}
1372
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001373static inline bool cpu_has_vmx_invvpid_global(void)
1374{
1375 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1376}
1377
Wanpeng Li2df19692017-03-23 05:30:08 -07001378static inline bool cpu_has_vmx_invvpid(void)
1379{
1380 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1381}
1382
Gui Jianfeng31299942010-03-15 17:29:09 +08001383static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001384{
Sheng Yang04547152009-04-01 15:52:31 +08001385 return vmcs_config.cpu_based_2nd_exec_ctrl &
1386 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001387}
1388
Gui Jianfeng31299942010-03-15 17:29:09 +08001389static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001390{
1391 return vmcs_config.cpu_based_2nd_exec_ctrl &
1392 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1393}
1394
Gui Jianfeng31299942010-03-15 17:29:09 +08001395static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001396{
1397 return vmcs_config.cpu_based_2nd_exec_ctrl &
1398 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1399}
1400
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001401static inline bool cpu_has_vmx_basic_inout(void)
1402{
1403 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1404}
1405
Paolo Bonzini35754c92015-07-29 12:05:37 +02001406static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001407{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001408 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001409}
1410
Gui Jianfeng31299942010-03-15 17:29:09 +08001411static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001412{
Sheng Yang04547152009-04-01 15:52:31 +08001413 return vmcs_config.cpu_based_2nd_exec_ctrl &
1414 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001415}
1416
Gui Jianfeng31299942010-03-15 17:29:09 +08001417static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001418{
1419 return vmcs_config.cpu_based_2nd_exec_ctrl &
1420 SECONDARY_EXEC_RDTSCP;
1421}
1422
Mao, Junjiead756a12012-07-02 01:18:48 +00001423static inline bool cpu_has_vmx_invpcid(void)
1424{
1425 return vmcs_config.cpu_based_2nd_exec_ctrl &
1426 SECONDARY_EXEC_ENABLE_INVPCID;
1427}
1428
Gui Jianfeng31299942010-03-15 17:29:09 +08001429static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001430{
1431 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1432}
1433
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001434static inline bool cpu_has_vmx_wbinvd_exit(void)
1435{
1436 return vmcs_config.cpu_based_2nd_exec_ctrl &
1437 SECONDARY_EXEC_WBINVD_EXITING;
1438}
1439
Abel Gordonabc4fc52013-04-18 14:35:25 +03001440static inline bool cpu_has_vmx_shadow_vmcs(void)
1441{
1442 u64 vmx_msr;
1443 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1444 /* check if the cpu supports writing r/o exit information fields */
1445 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1446 return false;
1447
1448 return vmcs_config.cpu_based_2nd_exec_ctrl &
1449 SECONDARY_EXEC_SHADOW_VMCS;
1450}
1451
Kai Huang843e4332015-01-28 10:54:28 +08001452static inline bool cpu_has_vmx_pml(void)
1453{
1454 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1455}
1456
Haozhong Zhang64903d62015-10-20 15:39:09 +08001457static inline bool cpu_has_vmx_tsc_scaling(void)
1458{
1459 return vmcs_config.cpu_based_2nd_exec_ctrl &
1460 SECONDARY_EXEC_TSC_SCALING;
1461}
1462
Sheng Yang04547152009-04-01 15:52:31 +08001463static inline bool report_flexpriority(void)
1464{
1465 return flexpriority_enabled;
1466}
1467
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001468static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1469{
1470 return vmcs12->cpu_based_vm_exec_control & bit;
1471}
1472
1473static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1474{
1475 return (vmcs12->cpu_based_vm_exec_control &
1476 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1477 (vmcs12->secondary_vm_exec_control & bit);
1478}
1479
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001480static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001481{
1482 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1483}
1484
Jan Kiszkaf4124502014-03-07 20:03:13 +01001485static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1486{
1487 return vmcs12->pin_based_vm_exec_control &
1488 PIN_BASED_VMX_PREEMPTION_TIMER;
1489}
1490
Nadav Har'El155a97a2013-08-05 11:07:16 +03001491static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1492{
1493 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1494}
1495
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001496static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1497{
1498 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1499 vmx_xsaves_supported();
1500}
1501
Wincy Vanf2b93282015-02-03 23:56:03 +08001502static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1503{
1504 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1505}
1506
Wanpeng Li5c614b32015-10-13 09:18:36 -07001507static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1508{
1509 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1510}
1511
Wincy Van82f0dd42015-02-03 23:57:18 +08001512static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1513{
1514 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1515}
1516
Wincy Van608406e2015-02-03 23:57:51 +08001517static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1518{
1519 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1520}
1521
Wincy Van705699a2015-02-03 23:58:17 +08001522static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1523{
1524 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1525}
1526
Jim Mattson3f618a02016-12-12 11:01:37 -08001527static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001528{
1529 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001530 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001531}
1532
Jan Kiszka533558b2014-01-04 18:47:20 +01001533static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1534 u32 exit_intr_info,
1535 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001536static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1537 struct vmcs12 *vmcs12,
1538 u32 reason, unsigned long qualification);
1539
Rusty Russell8b9cf982007-07-30 16:31:43 +10001540static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001541{
1542 int i;
1543
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001544 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001545 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001546 return i;
1547 return -1;
1548}
1549
Uros Bizjak3489f082018-10-11 19:40:43 +02001550static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001551{
1552 struct {
1553 u64 vpid : 16;
1554 u64 rsvd : 48;
1555 u64 gva;
1556 } operand = { vpid, 0, gva };
1557
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001558 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001559 /* CF==1 or ZF==1 --> rc = -1 */
1560 "; ja 1f ; ud2 ; 1:"
1561 : : "a"(&operand), "c"(ext) : "cc", "memory");
1562}
1563
Uros Bizjak3489f082018-10-11 19:40:43 +02001564static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08001565{
1566 struct {
1567 u64 eptp, gpa;
1568 } operand = {eptp, gpa};
1569
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001570 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001571 /* CF==1 or ZF==1 --> rc = -1 */
1572 "; ja 1f ; ud2 ; 1:\n"
1573 : : "a" (&operand), "c" (ext) : "cc", "memory");
1574}
1575
Avi Kivity26bb0982009-09-07 11:14:12 +03001576static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001577{
1578 int i;
1579
Rusty Russell8b9cf982007-07-30 16:31:43 +10001580 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001581 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001582 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001583 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001584}
1585
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586static void vmcs_clear(struct vmcs *vmcs)
1587{
1588 u64 phys_addr = __pa(vmcs);
1589 u8 error;
1590
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001591 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001592 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 : "cc", "memory");
1594 if (error)
1595 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1596 vmcs, phys_addr);
1597}
1598
Nadav Har'Eld462b812011-05-24 15:26:10 +03001599static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1600{
1601 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001602 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1603 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001604 loaded_vmcs->cpu = -1;
1605 loaded_vmcs->launched = 0;
1606}
1607
Dongxiao Xu7725b892010-05-11 18:29:38 +08001608static void vmcs_load(struct vmcs *vmcs)
1609{
1610 u64 phys_addr = __pa(vmcs);
1611 u8 error;
1612
1613 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001614 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001615 : "cc", "memory");
1616 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001617 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001618 vmcs, phys_addr);
1619}
1620
Dave Young2965faa2015-09-09 15:38:55 -07001621#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001622/*
1623 * This bitmap is used to indicate whether the vmclear
1624 * operation is enabled on all cpus. All disabled by
1625 * default.
1626 */
1627static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1628
1629static inline void crash_enable_local_vmclear(int cpu)
1630{
1631 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1632}
1633
1634static inline void crash_disable_local_vmclear(int cpu)
1635{
1636 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1637}
1638
1639static inline int crash_local_vmclear_enabled(int cpu)
1640{
1641 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1642}
1643
1644static void crash_vmclear_local_loaded_vmcss(void)
1645{
1646 int cpu = raw_smp_processor_id();
1647 struct loaded_vmcs *v;
1648
1649 if (!crash_local_vmclear_enabled(cpu))
1650 return;
1651
1652 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1653 loaded_vmcss_on_cpu_link)
1654 vmcs_clear(v->vmcs);
1655}
1656#else
1657static inline void crash_enable_local_vmclear(int cpu) { }
1658static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001659#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001660
Nadav Har'Eld462b812011-05-24 15:26:10 +03001661static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001663 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001664 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665
Nadav Har'Eld462b812011-05-24 15:26:10 +03001666 if (loaded_vmcs->cpu != cpu)
1667 return; /* vcpu migration can race with cpu offline */
1668 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001670 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001671 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001672
1673 /*
1674 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1675 * is before setting loaded_vmcs->vcpu to -1 which is done in
1676 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1677 * then adds the vmcs into percpu list before it is deleted.
1678 */
1679 smp_wmb();
1680
Nadav Har'Eld462b812011-05-24 15:26:10 +03001681 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001682 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683}
1684
Nadav Har'Eld462b812011-05-24 15:26:10 +03001685static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001686{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001687 int cpu = loaded_vmcs->cpu;
1688
1689 if (cpu != -1)
1690 smp_call_function_single(cpu,
1691 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001692}
1693
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001694static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001695{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001696 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001697 return;
1698
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001699 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001700 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001701}
1702
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001703static inline void vpid_sync_vcpu_global(void)
1704{
1705 if (cpu_has_vmx_invvpid_global())
1706 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1707}
1708
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001709static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001710{
1711 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001712 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001713 else
1714 vpid_sync_vcpu_global();
1715}
1716
Sheng Yang14394422008-04-28 12:24:45 +08001717static inline void ept_sync_global(void)
1718{
1719 if (cpu_has_vmx_invept_global())
1720 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1721}
1722
1723static inline void ept_sync_context(u64 eptp)
1724{
Avi Kivity089d0342009-03-23 18:26:32 +02001725 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001726 if (cpu_has_vmx_invept_context())
1727 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1728 else
1729 ept_sync_global();
1730 }
1731}
1732
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001733static __always_inline void vmcs_check16(unsigned long field)
1734{
1735 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1736 "16-bit accessor invalid for 64-bit field");
1737 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1738 "16-bit accessor invalid for 64-bit high field");
1739 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1740 "16-bit accessor invalid for 32-bit high field");
1741 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1742 "16-bit accessor invalid for natural width field");
1743}
1744
1745static __always_inline void vmcs_check32(unsigned long field)
1746{
1747 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1748 "32-bit accessor invalid for 16-bit field");
1749 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1750 "32-bit accessor invalid for natural width field");
1751}
1752
1753static __always_inline void vmcs_check64(unsigned long field)
1754{
1755 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1756 "64-bit accessor invalid for 16-bit field");
1757 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1758 "64-bit accessor invalid for 64-bit high field");
1759 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1760 "64-bit accessor invalid for 32-bit field");
1761 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1762 "64-bit accessor invalid for natural width field");
1763}
1764
1765static __always_inline void vmcs_checkl(unsigned long field)
1766{
1767 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1768 "Natural width accessor invalid for 16-bit field");
1769 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1770 "Natural width accessor invalid for 64-bit field");
1771 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1772 "Natural width accessor invalid for 64-bit high field");
1773 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1774 "Natural width accessor invalid for 32-bit field");
1775}
1776
1777static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778{
Avi Kivity5e520e62011-05-15 10:13:12 -04001779 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780
Avi Kivity5e520e62011-05-15 10:13:12 -04001781 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1782 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783 return value;
1784}
1785
Avi Kivity96304212011-05-15 10:13:13 -04001786static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001788 vmcs_check16(field);
1789 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790}
1791
Avi Kivity96304212011-05-15 10:13:13 -04001792static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001794 vmcs_check32(field);
1795 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796}
1797
Avi Kivity96304212011-05-15 10:13:13 -04001798static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001800 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001801#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001802 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001804 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805#endif
1806}
1807
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001808static __always_inline unsigned long vmcs_readl(unsigned long field)
1809{
1810 vmcs_checkl(field);
1811 return __vmcs_readl(field);
1812}
1813
Avi Kivitye52de1b2007-01-05 16:36:56 -08001814static noinline void vmwrite_error(unsigned long field, unsigned long value)
1815{
1816 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1817 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1818 dump_stack();
1819}
1820
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001821static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822{
1823 u8 error;
1824
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001825 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001826 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001827 if (unlikely(error))
1828 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829}
1830
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001831static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001833 vmcs_check16(field);
1834 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835}
1836
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001837static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001839 vmcs_check32(field);
1840 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841}
1842
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001843static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001845 vmcs_check64(field);
1846 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001847#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001849 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850#endif
1851}
1852
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001853static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001854{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001855 vmcs_checkl(field);
1856 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001857}
1858
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001859static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001860{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001861 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1862 "vmcs_clear_bits does not support 64-bit fields");
1863 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1864}
1865
1866static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1867{
1868 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1869 "vmcs_set_bits does not support 64-bit fields");
1870 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001871}
1872
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001873static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1874{
1875 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1876}
1877
Gleb Natapov2961e8762013-11-25 15:37:13 +02001878static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1879{
1880 vmcs_write32(VM_ENTRY_CONTROLS, val);
1881 vmx->vm_entry_controls_shadow = val;
1882}
1883
1884static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1885{
1886 if (vmx->vm_entry_controls_shadow != val)
1887 vm_entry_controls_init(vmx, val);
1888}
1889
1890static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1891{
1892 return vmx->vm_entry_controls_shadow;
1893}
1894
1895
1896static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1897{
1898 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1899}
1900
1901static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1902{
1903 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1904}
1905
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001906static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1907{
1908 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1909}
1910
Gleb Natapov2961e8762013-11-25 15:37:13 +02001911static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1912{
1913 vmcs_write32(VM_EXIT_CONTROLS, val);
1914 vmx->vm_exit_controls_shadow = val;
1915}
1916
1917static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1918{
1919 if (vmx->vm_exit_controls_shadow != val)
1920 vm_exit_controls_init(vmx, val);
1921}
1922
1923static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1924{
1925 return vmx->vm_exit_controls_shadow;
1926}
1927
1928
1929static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1930{
1931 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1932}
1933
1934static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1935{
1936 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1937}
1938
Avi Kivity2fb92db2011-04-27 19:42:18 +03001939static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1940{
1941 vmx->segment_cache.bitmask = 0;
1942}
1943
1944static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1945 unsigned field)
1946{
1947 bool ret;
1948 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1949
1950 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1951 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1952 vmx->segment_cache.bitmask = 0;
1953 }
1954 ret = vmx->segment_cache.bitmask & mask;
1955 vmx->segment_cache.bitmask |= mask;
1956 return ret;
1957}
1958
1959static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1960{
1961 u16 *p = &vmx->segment_cache.seg[seg].selector;
1962
1963 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1964 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1965 return *p;
1966}
1967
1968static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1969{
1970 ulong *p = &vmx->segment_cache.seg[seg].base;
1971
1972 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1973 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1974 return *p;
1975}
1976
1977static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1978{
1979 u32 *p = &vmx->segment_cache.seg[seg].limit;
1980
1981 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1982 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1983 return *p;
1984}
1985
1986static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1987{
1988 u32 *p = &vmx->segment_cache.seg[seg].ar;
1989
1990 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1991 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1992 return *p;
1993}
1994
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001995static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1996{
1997 u32 eb;
1998
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001999 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01002000 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002001 if ((vcpu->guest_debug &
2002 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2003 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2004 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002005 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002006 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002007 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002008 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02002009 if (vcpu->fpu_active)
2010 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002011
2012 /* When we are running a nested L2 guest and L1 specified for it a
2013 * certain exception bitmap, we must trap the same exceptions and pass
2014 * them to L1. When running L2, we will only handle the exceptions
2015 * specified above if L1 did not want them.
2016 */
2017 if (is_guest_mode(vcpu))
2018 eb |= get_vmcs12(vcpu)->exception_bitmap;
2019
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002020 vmcs_write32(EXCEPTION_BITMAP, eb);
2021}
2022
Ashok Raj70131292018-02-01 22:59:43 +01002023/*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01002024 * Check if MSR is intercepted for currently loaded MSR bitmap.
2025 */
2026static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2027{
2028 unsigned long *msr_bitmap;
2029 int f = sizeof(unsigned long);
2030
2031 if (!cpu_has_vmx_msr_bitmap())
2032 return true;
2033
2034 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2035
2036 if (msr <= 0x1fff) {
2037 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2038 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2039 msr &= 0x1fff;
2040 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2041 }
2042
2043 return true;
2044}
2045
2046/*
Ashok Raj70131292018-02-01 22:59:43 +01002047 * Check if MSR is intercepted for L01 MSR bitmap.
2048 */
2049static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2050{
2051 unsigned long *msr_bitmap;
2052 int f = sizeof(unsigned long);
2053
2054 if (!cpu_has_vmx_msr_bitmap())
2055 return true;
2056
2057 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2058
2059 if (msr <= 0x1fff) {
2060 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2061 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2062 msr &= 0x1fff;
2063 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2064 }
2065
2066 return true;
2067}
2068
Gleb Natapov2961e8762013-11-25 15:37:13 +02002069static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2070 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002071{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002072 vm_entry_controls_clearbit(vmx, entry);
2073 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002074}
2075
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002076static int find_msr(struct vmx_msrs *m, unsigned int msr)
2077{
2078 unsigned int i;
2079
2080 for (i = 0; i < m->nr; ++i) {
2081 if (m->val[i].index == msr)
2082 return i;
2083 }
2084 return -ENOENT;
2085}
2086
Avi Kivity61d2ef22010-04-28 16:40:38 +03002087static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2088{
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002089 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002090 struct msr_autoload *m = &vmx->msr_autoload;
2091
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002092 switch (msr) {
2093 case MSR_EFER:
2094 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002095 clear_atomic_switch_msr_special(vmx,
2096 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002097 VM_EXIT_LOAD_IA32_EFER);
2098 return;
2099 }
2100 break;
2101 case MSR_CORE_PERF_GLOBAL_CTRL:
2102 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002103 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002104 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2105 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2106 return;
2107 }
2108 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002109 }
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002110 i = find_msr(&m->guest, msr);
2111 if (i < 0)
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002112 goto skip_guest;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002113 --m->guest.nr;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002114 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002115 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002116
2117skip_guest:
2118 i = find_msr(&m->host, msr);
2119 if (i < 0)
2120 return;
2121
2122 --m->host.nr;
2123 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002124 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002125}
2126
Gleb Natapov2961e8762013-11-25 15:37:13 +02002127static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2128 unsigned long entry, unsigned long exit,
2129 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2130 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002131{
2132 vmcs_write64(guest_val_vmcs, guest_val);
2133 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002134 vm_entry_controls_setbit(vmx, entry);
2135 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002136}
2137
Avi Kivity61d2ef22010-04-28 16:40:38 +03002138static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002139 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002140{
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002141 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002142 struct msr_autoload *m = &vmx->msr_autoload;
2143
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002144 switch (msr) {
2145 case MSR_EFER:
2146 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002147 add_atomic_switch_msr_special(vmx,
2148 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002149 VM_EXIT_LOAD_IA32_EFER,
2150 GUEST_IA32_EFER,
2151 HOST_IA32_EFER,
2152 guest_val, host_val);
2153 return;
2154 }
2155 break;
2156 case MSR_CORE_PERF_GLOBAL_CTRL:
2157 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002158 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002159 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2160 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2161 GUEST_IA32_PERF_GLOBAL_CTRL,
2162 HOST_IA32_PERF_GLOBAL_CTRL,
2163 guest_val, host_val);
2164 return;
2165 }
2166 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002167 case MSR_IA32_PEBS_ENABLE:
2168 /* PEBS needs a quiescent period after being disabled (to write
2169 * a record). Disabling PEBS through VMX MSR swapping doesn't
2170 * provide that period, so a CPU could write host's record into
2171 * guest's memory.
2172 */
2173 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002174 }
2175
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002176 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002177 if (!entry_only)
2178 j = find_msr(&m->host, msr);
2179
Xiaoyao Liacd744b2019-02-14 12:08:58 +08002180 if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) ||
2181 (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002182 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002183 "Can't add msr %x\n", msr);
2184 return;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002185 }
2186 if (i < 0) {
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002187 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002188 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002189 }
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002190 m->guest.val[i].index = msr;
2191 m->guest.val[i].value = guest_val;
2192
2193 if (entry_only)
2194 return;
2195
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002196 if (j < 0) {
2197 j = m->host.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002198 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002199 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002200 m->host.val[j].index = msr;
2201 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002202}
2203
Avi Kivity33ed6322007-05-02 16:54:03 +03002204static void reload_tss(void)
2205{
Avi Kivity33ed6322007-05-02 16:54:03 +03002206 /*
2207 * VT restores TR but not its size. Useless.
2208 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002209 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002210 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002211
Avi Kivityd3591922010-07-26 18:32:39 +03002212 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002213 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2214 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002215}
2216
Avi Kivity92c0d902009-10-29 11:00:16 +02002217static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002218{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002219 u64 guest_efer = vmx->vcpu.arch.efer;
2220 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002221
Paolo Bonzini1b08d2a2019-10-27 16:23:23 +01002222 /* Shadow paging assumes NX to be available. */
2223 if (!enable_ept)
2224 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -07002225
Avi Kivity51c6cf62007-08-29 03:48:05 +03002226 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002227 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002228 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002229 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002230#ifdef CONFIG_X86_64
2231 ignore_bits |= EFER_LMA | EFER_LME;
2232 /* SCE is meaningful only in long mode on Intel */
2233 if (guest_efer & EFER_LMA)
2234 ignore_bits &= ~(u64)EFER_SCE;
2235#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002236
2237 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002238
2239 /*
2240 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2241 * On CPUs that support "load IA32_EFER", always switch EFER
2242 * atomically, since it's faster than switching it manually.
2243 */
2244 if (cpu_has_load_ia32_efer ||
2245 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002246 if (!(guest_efer & EFER_LMA))
2247 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002248 if (guest_efer != host_efer)
2249 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002250 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002251 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002252 } else {
2253 guest_efer &= ~ignore_bits;
2254 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002255
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002256 vmx->guest_msrs[efer_offset].data = guest_efer;
2257 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2258
2259 return true;
2260 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002261}
2262
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002263static unsigned long segment_base(u16 selector)
2264{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002265 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002266 struct desc_struct *d;
2267 unsigned long table_base;
2268 unsigned long v;
2269
2270 if (!(selector & ~3))
2271 return 0;
2272
Avi Kivityd3591922010-07-26 18:32:39 +03002273 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002274
2275 if (selector & 4) { /* from ldt */
2276 u16 ldt_selector = kvm_read_ldt();
2277
2278 if (!(ldt_selector & ~3))
2279 return 0;
2280
2281 table_base = segment_base(ldt_selector);
2282 }
2283 d = (struct desc_struct *)(table_base + (selector & ~7));
2284 v = get_desc_base(d);
2285#ifdef CONFIG_X86_64
2286 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2287 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2288#endif
2289 return v;
2290}
2291
2292static inline unsigned long kvm_read_tr_base(void)
2293{
2294 u16 tr;
2295 asm("str %0" : "=g"(tr));
2296 return segment_base(tr);
2297}
2298
Avi Kivity04d2cc72007-09-10 18:10:54 +03002299static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002300{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002301 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002302 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002303
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002304 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002305 return;
2306
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002307 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002308 /*
2309 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2310 * allow segment selectors with cpl > 0 or ti == 1.
2311 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002312 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002313 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002314 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002315 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002316 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002317 vmx->host_state.fs_reload_needed = 0;
2318 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002319 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002320 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002321 }
Avi Kivity9581d442010-10-19 16:46:55 +02002322 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002323 if (!(vmx->host_state.gs_sel & 7))
2324 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002325 else {
2326 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002327 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002328 }
2329
2330#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002331 savesegment(ds, vmx->host_state.ds_sel);
2332 savesegment(es, vmx->host_state.es_sel);
2333#endif
2334
2335#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002336 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2337 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2338#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002339 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2340 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002341#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002342
2343#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002344 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2345 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002346 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002347#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002348 if (boot_cpu_has(X86_FEATURE_MPX))
2349 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002350 for (i = 0; i < vmx->save_nmsrs; ++i)
2351 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002352 vmx->guest_msrs[i].data,
2353 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002354}
2355
Avi Kivitya9b21b62008-06-24 11:48:49 +03002356static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002357{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002358 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002359 return;
2360
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002361 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002362 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002363#ifdef CONFIG_X86_64
2364 if (is_long_mode(&vmx->vcpu))
2365 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2366#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002367 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002368 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002369#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002370 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002371#else
2372 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002373#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002374 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002375 if (vmx->host_state.fs_reload_needed)
2376 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002377#ifdef CONFIG_X86_64
2378 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2379 loadsegment(ds, vmx->host_state.ds_sel);
2380 loadsegment(es, vmx->host_state.es_sel);
2381 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002382#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002383 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002384#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002385 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002386#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002387 if (vmx->host_state.msr_host_bndcfgs)
2388 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002389 /*
2390 * If the FPU is not active (through the host task or
2391 * the guest vcpu), then restore the cr0.TS bit.
2392 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002393 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002394 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002395 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002396}
2397
Avi Kivitya9b21b62008-06-24 11:48:49 +03002398static void vmx_load_host_state(struct vcpu_vmx *vmx)
2399{
2400 preempt_disable();
2401 __vmx_load_host_state(vmx);
2402 preempt_enable();
2403}
2404
Feng Wu28b835d2015-09-18 22:29:54 +08002405static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2406{
2407 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2408 struct pi_desc old, new;
2409 unsigned int dest;
2410
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002411 /*
2412 * In case of hot-plug or hot-unplug, we may have to undo
2413 * vmx_vcpu_pi_put even if there is no assigned device. And we
2414 * always keep PI.NDST up to date for simplicity: it makes the
2415 * code easier, and CPU migration is not a fast path.
2416 */
2417 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002418 return;
2419
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002420 /*
2421 * First handle the simple case where no cmpxchg is necessary; just
2422 * allow posting non-urgent interrupts.
2423 *
2424 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2425 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2426 * expects the VCPU to be on the blocked_vcpu_list that matches
2427 * PI.NDST.
2428 */
2429 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2430 vcpu->cpu == cpu) {
2431 pi_clear_sn(pi_desc);
2432 return;
2433 }
2434
2435 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002436 do {
2437 old.control = new.control = pi_desc->control;
2438
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002439 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002440
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002441 if (x2apic_enabled())
2442 new.ndst = dest;
2443 else
2444 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002445
Feng Wu28b835d2015-09-18 22:29:54 +08002446 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002447 } while (cmpxchg64(&pi_desc->control, old.control,
2448 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002449}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002450
Peter Feinerc95ba922016-08-17 09:36:47 -07002451static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2452{
2453 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2454 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2455}
2456
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457/*
2458 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2459 * vcpu mutex is already taken.
2460 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002461static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002463 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002464 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002465 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002467 if (!vmm_exclusive)
2468 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002469 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002470 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002472 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002473 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002474 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002475
2476 /*
2477 * Read loaded_vmcs->cpu should be before fetching
2478 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2479 * See the comments in __loaded_vmcs_clear().
2480 */
2481 smp_rmb();
2482
Nadav Har'Eld462b812011-05-24 15:26:10 +03002483 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2484 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002485 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002486 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002487 }
2488
2489 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2490 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2491 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002492 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002493 }
2494
2495 if (!already_loaded) {
2496 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2497 unsigned long sysenter_esp;
2498
2499 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002500
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 /*
2502 * Linux uses per-cpu TSS and GDT, so set these when switching
2503 * processors.
2504 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002505 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002506 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507
2508 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2509 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002510
Nadav Har'Eld462b812011-05-24 15:26:10 +03002511 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512 }
Feng Wu28b835d2015-09-18 22:29:54 +08002513
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002514 /* Setup TSC multiplier */
2515 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002516 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2517 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002518
Feng Wu28b835d2015-09-18 22:29:54 +08002519 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002520 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002521}
2522
2523static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2524{
2525 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2526
2527 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002528 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2529 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002530 return;
2531
2532 /* Set SN when the vCPU is preempted */
2533 if (vcpu->preempted)
2534 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535}
2536
2537static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2538{
Feng Wu28b835d2015-09-18 22:29:54 +08002539 vmx_vcpu_pi_put(vcpu);
2540
Avi Kivitya9b21b62008-06-24 11:48:49 +03002541 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002542 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002543 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2544 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002545 kvm_cpu_vmxoff();
2546 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547}
2548
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002549static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2550{
Avi Kivity81231c62010-01-24 16:26:40 +02002551 ulong cr0;
2552
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002553 if (vcpu->fpu_active)
2554 return;
2555 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002556 cr0 = vmcs_readl(GUEST_CR0);
2557 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2558 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2559 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002560 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002561 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002562 if (is_guest_mode(vcpu))
2563 vcpu->arch.cr0_guest_owned_bits &=
2564 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002565 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002566}
2567
Avi Kivityedcafe32009-12-30 18:07:40 +02002568static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2569
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002570/*
2571 * Return the cr0 value that a nested guest would read. This is a combination
2572 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2573 * its hypervisor (cr0_read_shadow).
2574 */
2575static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2576{
2577 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2578 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2579}
2580static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2581{
2582 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2583 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2584}
2585
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002586static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2587{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002588 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2589 * set this *before* calling this function.
2590 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002591 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002592 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002593 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002594 vcpu->arch.cr0_guest_owned_bits = 0;
2595 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002596 if (is_guest_mode(vcpu)) {
2597 /*
2598 * L1's specified read shadow might not contain the TS bit,
2599 * so now that we turned on shadowing of this bit, we need to
2600 * set this bit of the shadow. Like in nested_vmx_run we need
2601 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2602 * up-to-date here because we just decached cr0.TS (and we'll
2603 * only update vmcs12->guest_cr0 on nested exit).
2604 */
2605 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2606 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2607 (vcpu->arch.cr0 & X86_CR0_TS);
2608 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2609 } else
2610 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002611}
2612
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2614{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002615 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002616
Avi Kivity6de12732011-03-07 12:51:22 +02002617 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2618 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2619 rflags = vmcs_readl(GUEST_RFLAGS);
2620 if (to_vmx(vcpu)->rmode.vm86_active) {
2621 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2622 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2623 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2624 }
2625 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002626 }
Avi Kivity6de12732011-03-07 12:51:22 +02002627 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628}
2629
2630static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2631{
Avi Kivity6de12732011-03-07 12:51:22 +02002632 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2633 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002634 if (to_vmx(vcpu)->rmode.vm86_active) {
2635 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002636 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002637 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 vmcs_writel(GUEST_RFLAGS, rflags);
2639}
2640
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002641static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2642{
2643 return to_vmx(vcpu)->guest_pkru;
2644}
2645
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002646static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002647{
2648 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2649 int ret = 0;
2650
2651 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002652 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002653 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002654 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002655
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002656 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002657}
2658
2659static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2660{
2661 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2662 u32 interruptibility = interruptibility_old;
2663
2664 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2665
Jan Kiszka48005f62010-02-19 19:38:07 +01002666 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002667 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002668 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002669 interruptibility |= GUEST_INTR_STATE_STI;
2670
2671 if ((interruptibility != interruptibility_old))
2672 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2673}
2674
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2676{
2677 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002679 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002681 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682
Glauber Costa2809f5d2009-05-12 16:21:05 -04002683 /* skipping an emulated instruction also counts */
2684 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685}
2686
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002687/*
2688 * KVM wants to inject page-faults which it got to the guest. This function
2689 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002690 */
Gleb Natapove011c662013-09-25 12:51:35 +03002691static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002692{
2693 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2694
Gleb Natapove011c662013-09-25 12:51:35 +03002695 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002696 return 0;
2697
Wanpeng Lia29fd272017-06-05 05:19:09 -07002698 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002699 vmcs_read32(VM_EXIT_INTR_INFO),
2700 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002701 return 1;
2702}
2703
Avi Kivity298101d2007-11-25 13:41:11 +02002704static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002705 bool has_error_code, u32 error_code,
2706 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002707{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002708 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002709 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002710
Gleb Natapove011c662013-09-25 12:51:35 +03002711 if (!reinject && is_guest_mode(vcpu) &&
2712 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002713 return;
2714
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002715 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002716 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002717 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2718 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002719
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002720 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002721 int inc_eip = 0;
2722 if (kvm_exception_is_soft(nr))
2723 inc_eip = vcpu->arch.event_exit_inst_len;
2724 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002725 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002726 return;
2727 }
2728
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002729 WARN_ON_ONCE(vmx->emulation_required);
2730
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002731 if (kvm_exception_is_soft(nr)) {
2732 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2733 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002734 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2735 } else
2736 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2737
2738 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002739}
2740
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002741static bool vmx_rdtscp_supported(void)
2742{
2743 return cpu_has_vmx_rdtscp();
2744}
2745
Mao, Junjiead756a12012-07-02 01:18:48 +00002746static bool vmx_invpcid_supported(void)
2747{
2748 return cpu_has_vmx_invpcid() && enable_ept;
2749}
2750
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751/*
Eddie Donga75beee2007-05-17 18:55:15 +03002752 * Swap MSR entry in host/guest MSR entry array.
2753 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002754static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002755{
Avi Kivity26bb0982009-09-07 11:14:12 +03002756 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002757
2758 tmp = vmx->guest_msrs[to];
2759 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2760 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002761}
2762
2763/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002764 * Set up the vmcs to automatically save and restore system
2765 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2766 * mode, as fiddling with msrs is very expensive.
2767 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002768static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002769{
Avi Kivity26bb0982009-09-07 11:14:12 +03002770 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002771
Eddie Donga75beee2007-05-17 18:55:15 +03002772 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002773#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002774 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002775 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002776 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002777 move_msr_up(vmx, index, save_nmsrs++);
2778 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002779 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002780 move_msr_up(vmx, index, save_nmsrs++);
2781 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002782 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002783 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002784 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002785 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002786 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002787 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002788 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002789 * if efer.sce is enabled.
2790 */
Brian Gerst8c065852010-07-17 09:03:26 -04002791 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002792 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002793 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002794 }
Eddie Donga75beee2007-05-17 18:55:15 +03002795#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002796 index = __find_msr_index(vmx, MSR_EFER);
2797 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002798 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002799
Avi Kivity26bb0982009-09-07 11:14:12 +03002800 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002801
Yang Zhang8d146952013-01-25 10:18:50 +08002802 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002803 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002804}
2805
2806/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002808 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2809 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002811static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812{
2813 u64 host_tsc, tsc_offset;
2814
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002815 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002817 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818}
2819
2820/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002821 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002823static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002825 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002826 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002827 * We're here if L1 chose not to trap WRMSR to TSC. According
2828 * to the spec, this should set L1's TSC; The offset that L1
2829 * set for L2 remains unchanged, and still needs to be added
2830 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002831 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002832 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002833 /* recalculate vmcs02.TSC_OFFSET: */
2834 vmcs12 = get_vmcs12(vcpu);
2835 vmcs_write64(TSC_OFFSET, offset +
2836 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2837 vmcs12->tsc_offset : 0));
2838 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002839 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2840 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002841 vmcs_write64(TSC_OFFSET, offset);
2842 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843}
2844
Nadav Har'El801d3422011-05-25 23:02:23 +03002845static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2846{
2847 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2848 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2849}
2850
2851/*
2852 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2853 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2854 * all guests if the "nested" module option is off, and can also be disabled
2855 * for a single guest by disabling its VMX cpuid bit.
2856 */
2857static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2858{
2859 return nested && guest_cpuid_has_vmx(vcpu);
2860}
2861
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002863 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2864 * returned for the various VMX controls MSRs when nested VMX is enabled.
2865 * The same values should also be used to verify that vmcs12 control fields are
2866 * valid during nested entry from L1 to L2.
2867 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2868 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2869 * bit in the high half is on if the corresponding bit in the control field
2870 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002871 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002872static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002873{
2874 /*
2875 * Note that as a general rule, the high half of the MSRs (bits in
2876 * the control fields which may be 1) should be initialized by the
2877 * intersection of the underlying hardware's MSR (i.e., features which
2878 * can be supported) and the list of features we want to expose -
2879 * because they are known to be properly supported in our code.
2880 * Also, usually, the low half of the MSRs (bits which must be 1) can
2881 * be set to 0, meaning that L1 may turn off any of these bits. The
2882 * reason is that if one of these bits is necessary, it will appear
2883 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2884 * fields of vmcs01 and vmcs02, will turn these bits off - and
2885 * nested_vmx_exit_handled() will not pass related exits to L1.
2886 * These rules have exceptions below.
2887 */
2888
2889 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002890 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002891 vmx->nested.nested_vmx_pinbased_ctls_low,
2892 vmx->nested.nested_vmx_pinbased_ctls_high);
2893 vmx->nested.nested_vmx_pinbased_ctls_low |=
2894 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2895 vmx->nested.nested_vmx_pinbased_ctls_high &=
2896 PIN_BASED_EXT_INTR_MASK |
2897 PIN_BASED_NMI_EXITING |
2898 PIN_BASED_VIRTUAL_NMIS;
2899 vmx->nested.nested_vmx_pinbased_ctls_high |=
2900 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002901 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002902 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002903 vmx->nested.nested_vmx_pinbased_ctls_high |=
2904 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002905
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002906 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002907 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002908 vmx->nested.nested_vmx_exit_ctls_low,
2909 vmx->nested.nested_vmx_exit_ctls_high);
2910 vmx->nested.nested_vmx_exit_ctls_low =
2911 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002912
Wincy Vanb9c237b2015-02-03 23:56:30 +08002913 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002914#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002915 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002916#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002917 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002918 vmx->nested.nested_vmx_exit_ctls_high |=
2919 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002920 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002921 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2922
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002923 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002924 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002925
Jan Kiszka2996fca2014-06-16 13:59:43 +02002926 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002927 vmx->nested.nested_vmx_true_exit_ctls_low =
2928 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002929 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2930
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002931 /* entry controls */
2932 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002933 vmx->nested.nested_vmx_entry_ctls_low,
2934 vmx->nested.nested_vmx_entry_ctls_high);
2935 vmx->nested.nested_vmx_entry_ctls_low =
2936 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2937 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002938#ifdef CONFIG_X86_64
2939 VM_ENTRY_IA32E_MODE |
2940#endif
2941 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002942 vmx->nested.nested_vmx_entry_ctls_high |=
2943 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002944 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002945 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002946
Jan Kiszka2996fca2014-06-16 13:59:43 +02002947 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002948 vmx->nested.nested_vmx_true_entry_ctls_low =
2949 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002950 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2951
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002952 /* cpu-based controls */
2953 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002954 vmx->nested.nested_vmx_procbased_ctls_low,
2955 vmx->nested.nested_vmx_procbased_ctls_high);
2956 vmx->nested.nested_vmx_procbased_ctls_low =
2957 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2958 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002959 CPU_BASED_VIRTUAL_INTR_PENDING |
2960 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002961 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2962 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2963 CPU_BASED_CR3_STORE_EXITING |
2964#ifdef CONFIG_X86_64
2965 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2966#endif
2967 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002968 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2969 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2970 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2971 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002972 /*
2973 * We can allow some features even when not supported by the
2974 * hardware. For example, L1 can specify an MSR bitmap - and we
2975 * can use it to avoid exits to L1 - even when L0 runs L2
2976 * without MSR bitmaps.
2977 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002978 vmx->nested.nested_vmx_procbased_ctls_high |=
2979 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002980 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002981
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002982 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002983 vmx->nested.nested_vmx_true_procbased_ctls_low =
2984 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002985 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2986
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002987 /* secondary cpu-based controls */
2988 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002989 vmx->nested.nested_vmx_secondary_ctls_low,
2990 vmx->nested.nested_vmx_secondary_ctls_high);
2991 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2992 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002993 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002994 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002995 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002996 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002997 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002998 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002999 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07003000 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003001
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003002 if (enable_ept) {
3003 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003004 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003005 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003006 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01003007 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
3008 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003009 if (cpu_has_vmx_ept_execute_only())
3010 vmx->nested.nested_vmx_ept_caps |=
3011 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003012 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04003013 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3014 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003015 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08003016 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003017
Paolo Bonzinief697a72016-03-18 16:58:38 +01003018 /*
3019 * Old versions of KVM use the single-context version without
3020 * checking for support, so declare that it is supported even
3021 * though it is treated as global context. The alternative is
3022 * not failing the single-context invvpid, and it is worse.
3023 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07003024 if (enable_vpid)
3025 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01003026 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07003027 else
3028 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003029
Radim Krčmář0790ec12015-03-17 14:02:32 +01003030 if (enable_unrestricted_guest)
3031 vmx->nested.nested_vmx_secondary_ctls_high |=
3032 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3033
Jan Kiszkac18911a2013-03-13 16:06:41 +01003034 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003035 rdmsr(MSR_IA32_VMX_MISC,
3036 vmx->nested.nested_vmx_misc_low,
3037 vmx->nested.nested_vmx_misc_high);
3038 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
3039 vmx->nested.nested_vmx_misc_low |=
3040 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003041 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003042 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003043}
3044
3045static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3046{
3047 /*
3048 * Bits 0 in high must be 0, and bits 1 in low must be 1.
3049 */
3050 return ((control & high) | low) == control;
3051}
3052
3053static inline u64 vmx_control_msr(u32 low, u32 high)
3054{
3055 return low | ((u64)high << 32);
3056}
3057
Jan Kiszkacae50132014-01-04 18:47:22 +01003058/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003059static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3060{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003061 struct vcpu_vmx *vmx = to_vmx(vcpu);
3062
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003063 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003064 case MSR_IA32_VMX_BASIC:
3065 /*
3066 * This MSR reports some information about VMX support. We
3067 * should return information about the VMX we emulate for the
3068 * guest, and the VMCS structure we give it - not about the
3069 * VMX support of the underlying hardware.
3070 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003071 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003072 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3073 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003074 if (cpu_has_vmx_basic_inout())
3075 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003076 break;
3077 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3078 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003079 *pdata = vmx_control_msr(
3080 vmx->nested.nested_vmx_pinbased_ctls_low,
3081 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003082 break;
3083 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003084 *pdata = vmx_control_msr(
3085 vmx->nested.nested_vmx_true_procbased_ctls_low,
3086 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02003087 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003088 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003089 *pdata = vmx_control_msr(
3090 vmx->nested.nested_vmx_procbased_ctls_low,
3091 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003092 break;
3093 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003094 *pdata = vmx_control_msr(
3095 vmx->nested.nested_vmx_true_exit_ctls_low,
3096 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003097 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003098 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003099 *pdata = vmx_control_msr(
3100 vmx->nested.nested_vmx_exit_ctls_low,
3101 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003102 break;
3103 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003104 *pdata = vmx_control_msr(
3105 vmx->nested.nested_vmx_true_entry_ctls_low,
3106 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003107 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003108 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003109 *pdata = vmx_control_msr(
3110 vmx->nested.nested_vmx_entry_ctls_low,
3111 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003112 break;
3113 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003114 *pdata = vmx_control_msr(
3115 vmx->nested.nested_vmx_misc_low,
3116 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003117 break;
3118 /*
3119 * These MSRs specify bits which the guest must keep fixed (on or off)
3120 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3121 * We picked the standard core2 setting.
3122 */
3123#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3124#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3125 case MSR_IA32_VMX_CR0_FIXED0:
3126 *pdata = VMXON_CR0_ALWAYSON;
3127 break;
3128 case MSR_IA32_VMX_CR0_FIXED1:
3129 *pdata = -1ULL;
3130 break;
3131 case MSR_IA32_VMX_CR4_FIXED0:
3132 *pdata = VMXON_CR4_ALWAYSON;
3133 break;
3134 case MSR_IA32_VMX_CR4_FIXED1:
3135 *pdata = -1ULL;
3136 break;
3137 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003138 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003139 break;
3140 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003141 *pdata = vmx_control_msr(
3142 vmx->nested.nested_vmx_secondary_ctls_low,
3143 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003144 break;
3145 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003146 *pdata = vmx->nested.nested_vmx_ept_caps |
3147 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003148 break;
3149 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003150 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003151 }
3152
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003153 return 0;
3154}
3155
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003156static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3157 uint64_t val)
3158{
3159 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3160
3161 return !(val & ~valid_bits);
3162}
3163
Tom Lendacky62d88fc2018-02-21 13:39:51 -06003164static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3165{
3166 return 1;
3167}
3168
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003169/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 * Reads an msr value (of 'msr_index') into 'pdata'.
3171 * Returns 0 on success, non-0 otherwise.
3172 * Assumes vcpu_load() was already called.
3173 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003174static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175{
Avi Kivity26bb0982009-09-07 11:14:12 +03003176 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003178 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003179#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003181 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 break;
3183 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003184 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003186 case MSR_KERNEL_GS_BASE:
3187 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003188 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003189 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003190#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003192 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303193 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003194 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003196 case MSR_IA32_SPEC_CTRL:
3197 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003198 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003199 return 1;
3200
3201 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3202 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003204 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 break;
3206 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003207 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 break;
3209 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003210 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003212 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003213 if (!kvm_mpx_supported() ||
3214 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003215 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003216 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003217 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003218 case MSR_IA32_MCG_EXT_CTL:
3219 if (!msr_info->host_initiated &&
3220 !(to_vmx(vcpu)->msr_ia32_feature_control &
3221 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003222 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003223 msr_info->data = vcpu->arch.mcg_ext_ctl;
3224 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003225 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003226 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003227 break;
3228 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3229 if (!nested_vmx_allowed(vcpu))
3230 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003231 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003232 case MSR_IA32_XSS:
3233 if (!vmx_xsaves_supported())
3234 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003235 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003236 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003237 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003238 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003239 return 1;
3240 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003242 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003243 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003244 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003245 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003247 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 }
3249
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 return 0;
3251}
3252
Jan Kiszkacae50132014-01-04 18:47:22 +01003253static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3254
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255/*
3256 * Writes msr value into into the appropriate "register".
3257 * Returns 0 on success, non-0 otherwise.
3258 * Assumes vcpu_load() was already called.
3259 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003260static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003262 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003263 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003264 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003265 u32 msr_index = msr_info->index;
3266 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003267
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003269 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003270 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003271 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003272#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003274 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 vmcs_writel(GUEST_FS_BASE, data);
3276 break;
3277 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003278 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279 vmcs_writel(GUEST_GS_BASE, data);
3280 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003281 case MSR_KERNEL_GS_BASE:
3282 vmx_load_host_state(vmx);
3283 vmx->msr_guest_kernel_gs_base = data;
3284 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285#endif
3286 case MSR_IA32_SYSENTER_CS:
3287 vmcs_write32(GUEST_SYSENTER_CS, data);
3288 break;
3289 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003290 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003291 break;
3292 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003293 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003295 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003296 if (!kvm_mpx_supported() ||
3297 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003298 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003299 if (is_noncanonical_address(data & PAGE_MASK) ||
3300 (data & MSR_IA32_BNDCFGS_RSVD))
3301 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003302 vmcs_write64(GUEST_BNDCFGS, data);
3303 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303304 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003305 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003307 case MSR_IA32_SPEC_CTRL:
3308 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003309 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003310 return 1;
3311
3312 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003313 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003314 return 1;
3315
3316 vmx->spec_ctrl = data;
3317
3318 if (!data)
3319 break;
3320
3321 /*
3322 * For non-nested:
3323 * When it's written (to non-zero) for the first time, pass
3324 * it through.
3325 *
3326 * For nested:
3327 * The handling of the MSR bitmap for L2 guests is done in
3328 * nested_vmx_merge_msr_bitmap. We should not touch the
3329 * vmcs02.msr_bitmap here since it gets completely overwritten
3330 * in the merging. We update the vmcs01 here for L1 as well
3331 * since it will end up touching the MSR anyway now.
3332 */
3333 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3334 MSR_IA32_SPEC_CTRL,
3335 MSR_TYPE_RW);
3336 break;
Ashok Raj70131292018-02-01 22:59:43 +01003337 case MSR_IA32_PRED_CMD:
3338 if (!msr_info->host_initiated &&
3339 !guest_cpuid_has_ibpb(vcpu))
3340 return 1;
3341
3342 if (data & ~PRED_CMD_IBPB)
3343 return 1;
3344
3345 if (!data)
3346 break;
3347
3348 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3349
3350 /*
3351 * For non-nested:
3352 * When it's written (to non-zero) for the first time, pass
3353 * it through.
3354 *
3355 * For nested:
3356 * The handling of the MSR bitmap for L2 guests is done in
3357 * nested_vmx_merge_msr_bitmap. We should not touch the
3358 * vmcs02.msr_bitmap here since it gets completely overwritten
3359 * in the merging.
3360 */
3361 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3362 MSR_TYPE_W);
3363 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003364 case MSR_IA32_CR_PAT:
3365 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003366 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3367 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003368 vmcs_write64(GUEST_IA32_PAT, data);
3369 vcpu->arch.pat = data;
3370 break;
3371 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003372 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003373 break;
Will Auldba904632012-11-29 12:42:50 -08003374 case MSR_IA32_TSC_ADJUST:
3375 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003376 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003377 case MSR_IA32_MCG_EXT_CTL:
3378 if ((!msr_info->host_initiated &&
3379 !(to_vmx(vcpu)->msr_ia32_feature_control &
3380 FEATURE_CONTROL_LMCE)) ||
3381 (data & ~MCG_EXT_CTL_LMCE_EN))
3382 return 1;
3383 vcpu->arch.mcg_ext_ctl = data;
3384 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003385 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003386 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003387 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003388 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3389 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003390 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003391 if (msr_info->host_initiated && data == 0)
3392 vmx_leave_nested(vcpu);
3393 break;
3394 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3395 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003396 case MSR_IA32_XSS:
3397 if (!vmx_xsaves_supported())
3398 return 1;
3399 /*
3400 * The only supported bit as of Skylake is bit 8, but
3401 * it is not supported on KVM.
3402 */
3403 if (data != 0)
3404 return 1;
3405 vcpu->arch.ia32_xss = data;
3406 if (vcpu->arch.ia32_xss != host_xss)
3407 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04003408 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08003409 else
3410 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3411 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003412 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003413 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003414 return 1;
3415 /* Check reserved bit, higher 32 bits should be zero */
3416 if ((data >> 32) != 0)
3417 return 1;
3418 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003420 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003421 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003422 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003423 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003424 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3425 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003426 ret = kvm_set_shared_msr(msr->index, msr->data,
3427 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003428 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003429 if (ret)
3430 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003431 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003432 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003434 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435 }
3436
Eddie Dong2cc51562007-05-21 07:28:09 +03003437 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438}
3439
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003440static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003442 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3443 switch (reg) {
3444 case VCPU_REGS_RSP:
3445 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3446 break;
3447 case VCPU_REGS_RIP:
3448 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3449 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003450 case VCPU_EXREG_PDPTR:
3451 if (enable_ept)
3452 ept_save_pdptrs(vcpu);
3453 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003454 default:
3455 break;
3456 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457}
3458
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459static __init int cpu_has_kvm_support(void)
3460{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003461 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462}
3463
3464static __init int vmx_disabled_by_bios(void)
3465{
3466 u64 msr;
3467
3468 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003469 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003470 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003471 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3472 && tboot_enabled())
3473 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003474 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003475 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003476 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003477 && !tboot_enabled()) {
3478 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003479 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003480 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003481 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003482 /* launched w/o TXT and VMX disabled */
3483 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3484 && !tboot_enabled())
3485 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003486 }
3487
3488 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489}
3490
Dongxiao Xu7725b892010-05-11 18:29:38 +08003491static void kvm_cpu_vmxon(u64 addr)
3492{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003493 intel_pt_handle_vmx(1);
3494
Dongxiao Xu7725b892010-05-11 18:29:38 +08003495 asm volatile (ASM_VMX_VMXON_RAX
3496 : : "a"(&addr), "m"(addr)
3497 : "memory", "cc");
3498}
3499
Radim Krčmář13a34e02014-08-28 15:13:03 +02003500static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501{
3502 int cpu = raw_smp_processor_id();
3503 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003504 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003506 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003507 return -EBUSY;
3508
Nadav Har'Eld462b812011-05-24 15:26:10 +03003509 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003510 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3511 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003512
3513 /*
3514 * Now we can enable the vmclear operation in kdump
3515 * since the loaded_vmcss_on_cpu list on this cpu
3516 * has been initialized.
3517 *
3518 * Though the cpu is not in VMX operation now, there
3519 * is no problem to enable the vmclear operation
3520 * for the loaded_vmcss_on_cpu list is empty!
3521 */
3522 crash_enable_local_vmclear(cpu);
3523
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003525
3526 test_bits = FEATURE_CONTROL_LOCKED;
3527 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3528 if (tboot_enabled())
3529 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3530
3531 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003533 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3534 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003535 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003536
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003537 if (vmm_exclusive) {
3538 kvm_cpu_vmxon(phys_addr);
3539 ept_sync_global();
3540 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003541
Christoph Lameter89cbc762014-08-17 12:30:40 -05003542 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003543
Alexander Graf10474ae2009-09-15 11:37:46 +02003544 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545}
3546
Nadav Har'Eld462b812011-05-24 15:26:10 +03003547static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003548{
3549 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003550 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003551
Nadav Har'Eld462b812011-05-24 15:26:10 +03003552 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3553 loaded_vmcss_on_cpu_link)
3554 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003555}
3556
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003557
3558/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3559 * tricks.
3560 */
3561static void kvm_cpu_vmxoff(void)
3562{
3563 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003564
3565 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003566}
3567
Radim Krčmář13a34e02014-08-28 15:13:03 +02003568static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003570 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003571 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003572 kvm_cpu_vmxoff();
3573 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003574 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575}
3576
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003577static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003578 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003579{
3580 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003581 u32 ctl = ctl_min | ctl_opt;
3582
3583 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3584
3585 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3586 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3587
3588 /* Ensure minimum (required) set of control bits are supported. */
3589 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003590 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003591
3592 *result = ctl;
3593 return 0;
3594}
3595
Avi Kivity110312c2010-12-21 12:54:20 +02003596static __init bool allow_1_setting(u32 msr, u32 ctl)
3597{
3598 u32 vmx_msr_low, vmx_msr_high;
3599
3600 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3601 return vmx_msr_high & ctl;
3602}
3603
Yang, Sheng002c7f72007-07-31 14:23:01 +03003604static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003605{
3606 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003607 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003608 u32 _pin_based_exec_control = 0;
3609 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003610 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003611 u32 _vmexit_control = 0;
3612 u32 _vmentry_control = 0;
3613
Raghavendra K T10166742012-02-07 23:19:20 +05303614 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003615#ifdef CONFIG_X86_64
3616 CPU_BASED_CR8_LOAD_EXITING |
3617 CPU_BASED_CR8_STORE_EXITING |
3618#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003619 CPU_BASED_CR3_LOAD_EXITING |
3620 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003621 CPU_BASED_USE_IO_BITMAPS |
3622 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003623 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003624 CPU_BASED_MWAIT_EXITING |
3625 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003626 CPU_BASED_INVLPG_EXITING |
3627 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003628
Sheng Yangf78e0e22007-10-29 09:40:42 +08003629 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003630 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003631 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003632 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3633 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003634 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003635#ifdef CONFIG_X86_64
3636 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3637 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3638 ~CPU_BASED_CR8_STORE_EXITING;
3639#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003640 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003641 min2 = 0;
3642 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003643 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003644 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003645 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003646 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003647 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003648 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003649 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003650 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003651 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003652 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003653 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003654 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003655 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003656 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003657 if (adjust_vmx_controls(min2, opt2,
3658 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003659 &_cpu_based_2nd_exec_control) < 0)
3660 return -EIO;
3661 }
3662#ifndef CONFIG_X86_64
3663 if (!(_cpu_based_2nd_exec_control &
3664 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3665 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3666#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003667
3668 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3669 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003670 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003671 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3672 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003673
Sheng Yangd56f5462008-04-25 10:13:16 +08003674 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003675 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3676 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003677 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3678 CPU_BASED_CR3_STORE_EXITING |
3679 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003680 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3681 vmx_capability.ept, vmx_capability.vpid);
3682 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003683
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003684 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003685#ifdef CONFIG_X86_64
3686 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3687#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003688 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003689 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003690 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3691 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003692 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003693
Yang Zhang01e439b2013-04-11 19:25:12 +08003694 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003695 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3696 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003697 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3698 &_pin_based_exec_control) < 0)
3699 return -EIO;
3700
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003701 if (cpu_has_broken_vmx_preemption_timer())
3702 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003703 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003704 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003705 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3706
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003707 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003708 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003709 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3710 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003711 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003713 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003714
3715 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3716 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003717 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003718
3719#ifdef CONFIG_X86_64
3720 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3721 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003722 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003723#endif
3724
3725 /* Require Write-Back (WB) memory type for VMCS accesses. */
3726 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003727 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003728
Yang, Sheng002c7f72007-07-31 14:23:01 +03003729 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003730 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003731 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003732 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003733
Yang, Sheng002c7f72007-07-31 14:23:01 +03003734 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3735 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003736 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003737 vmcs_conf->vmexit_ctrl = _vmexit_control;
3738 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003739
Avi Kivity110312c2010-12-21 12:54:20 +02003740 cpu_has_load_ia32_efer =
3741 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3742 VM_ENTRY_LOAD_IA32_EFER)
3743 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3744 VM_EXIT_LOAD_IA32_EFER);
3745
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003746 cpu_has_load_perf_global_ctrl =
3747 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3748 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3749 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3750 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3751
3752 /*
3753 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003754 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003755 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3756 *
3757 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3758 *
3759 * AAK155 (model 26)
3760 * AAP115 (model 30)
3761 * AAT100 (model 37)
3762 * BC86,AAY89,BD102 (model 44)
3763 * BA97 (model 46)
3764 *
3765 */
3766 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3767 switch (boot_cpu_data.x86_model) {
3768 case 26:
3769 case 30:
3770 case 37:
3771 case 44:
3772 case 46:
3773 cpu_has_load_perf_global_ctrl = false;
3774 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3775 "does not work properly. Using workaround\n");
3776 break;
3777 default:
3778 break;
3779 }
3780 }
3781
Borislav Petkov782511b2016-04-04 22:25:03 +02003782 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003783 rdmsrl(MSR_IA32_XSS, host_xss);
3784
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003785 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003786}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787
3788static struct vmcs *alloc_vmcs_cpu(int cpu)
3789{
3790 int node = cpu_to_node(cpu);
3791 struct page *pages;
3792 struct vmcs *vmcs;
3793
Vlastimil Babka96db8002015-09-08 15:03:50 -07003794 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795 if (!pages)
3796 return NULL;
3797 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003798 memset(vmcs, 0, vmcs_config.size);
3799 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800 return vmcs;
3801}
3802
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803static void free_vmcs(struct vmcs *vmcs)
3804{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003805 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806}
3807
Nadav Har'Eld462b812011-05-24 15:26:10 +03003808/*
3809 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3810 */
3811static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3812{
3813 if (!loaded_vmcs->vmcs)
3814 return;
3815 loaded_vmcs_clear(loaded_vmcs);
3816 free_vmcs(loaded_vmcs->vmcs);
3817 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003818 if (loaded_vmcs->msr_bitmap)
3819 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003820 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003821}
3822
Paolo Bonziniff546f92018-01-11 12:16:15 +01003823static struct vmcs *alloc_vmcs(void)
3824{
3825 return alloc_vmcs_cpu(raw_smp_processor_id());
3826}
3827
3828static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3829{
3830 loaded_vmcs->vmcs = alloc_vmcs();
3831 if (!loaded_vmcs->vmcs)
3832 return -ENOMEM;
3833
3834 loaded_vmcs->shadow_vmcs = NULL;
3835 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003836
3837 if (cpu_has_vmx_msr_bitmap()) {
3838 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3839 if (!loaded_vmcs->msr_bitmap)
3840 goto out_vmcs;
3841 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3842 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003843 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003844
3845out_vmcs:
3846 free_loaded_vmcs(loaded_vmcs);
3847 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003848}
3849
Sam Ravnborg39959582007-06-01 00:47:13 -07003850static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851{
3852 int cpu;
3853
Zachary Amsden3230bb42009-09-29 11:38:37 -10003854 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003856 per_cpu(vmxarea, cpu) = NULL;
3857 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858}
3859
Bandan Dasfe2b2012014-04-21 15:20:14 -04003860static void init_vmcs_shadow_fields(void)
3861{
3862 int i, j;
3863
3864 /* No checks for read only fields yet */
3865
3866 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3867 switch (shadow_read_write_fields[i]) {
3868 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003869 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003870 continue;
3871 break;
3872 default:
3873 break;
3874 }
3875
3876 if (j < i)
3877 shadow_read_write_fields[j] =
3878 shadow_read_write_fields[i];
3879 j++;
3880 }
3881 max_shadow_read_write_fields = j;
3882
3883 /* shadowed fields guest access without vmexit */
3884 for (i = 0; i < max_shadow_read_write_fields; i++) {
3885 clear_bit(shadow_read_write_fields[i],
3886 vmx_vmwrite_bitmap);
3887 clear_bit(shadow_read_write_fields[i],
3888 vmx_vmread_bitmap);
3889 }
3890 for (i = 0; i < max_shadow_read_only_fields; i++)
3891 clear_bit(shadow_read_only_fields[i],
3892 vmx_vmread_bitmap);
3893}
3894
Avi Kivity6aa8b732006-12-10 02:21:36 -08003895static __init int alloc_kvm_area(void)
3896{
3897 int cpu;
3898
Zachary Amsden3230bb42009-09-29 11:38:37 -10003899 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900 struct vmcs *vmcs;
3901
3902 vmcs = alloc_vmcs_cpu(cpu);
3903 if (!vmcs) {
3904 free_kvm_area();
3905 return -ENOMEM;
3906 }
3907
3908 per_cpu(vmxarea, cpu) = vmcs;
3909 }
3910 return 0;
3911}
3912
Gleb Natapov14168782013-01-21 15:36:49 +02003913static bool emulation_required(struct kvm_vcpu *vcpu)
3914{
3915 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3916}
3917
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003918static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003919 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003921 if (!emulate_invalid_guest_state) {
3922 /*
3923 * CS and SS RPL should be equal during guest entry according
3924 * to VMX spec, but in reality it is not always so. Since vcpu
3925 * is in the middle of the transition from real mode to
3926 * protected mode it is safe to assume that RPL 0 is a good
3927 * default value.
3928 */
3929 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003930 save->selector &= ~SEGMENT_RPL_MASK;
3931 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003932 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003934 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935}
3936
3937static void enter_pmode(struct kvm_vcpu *vcpu)
3938{
3939 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003940 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941
Gleb Natapovd99e4152012-12-20 16:57:45 +02003942 /*
3943 * Update real mode segment cache. It may be not up-to-date if sement
3944 * register was written while vcpu was in a guest mode.
3945 */
3946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3948 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3949 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3951 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3952
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003953 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954
Avi Kivity2fb92db2011-04-27 19:42:18 +03003955 vmx_segment_cache_clear(vmx);
3956
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003957 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958
3959 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003960 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3961 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 vmcs_writel(GUEST_RFLAGS, flags);
3963
Rusty Russell66aee912007-07-17 23:34:16 +10003964 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3965 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966
3967 update_exception_bitmap(vcpu);
3968
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003969 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3970 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3971 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3972 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3973 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3974 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975}
3976
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003977static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978{
Mathias Krause772e0312012-08-30 01:30:19 +02003979 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003980 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981
Gleb Natapovd99e4152012-12-20 16:57:45 +02003982 var.dpl = 0x3;
3983 if (seg == VCPU_SREG_CS)
3984 var.type = 0x3;
3985
3986 if (!emulate_invalid_guest_state) {
3987 var.selector = var.base >> 4;
3988 var.base = var.base & 0xffff0;
3989 var.limit = 0xffff;
3990 var.g = 0;
3991 var.db = 0;
3992 var.present = 1;
3993 var.s = 1;
3994 var.l = 0;
3995 var.unusable = 0;
3996 var.type = 0x3;
3997 var.avl = 0;
3998 if (save->base & 0xf)
3999 printk_once(KERN_WARNING "kvm: segment base is not "
4000 "paragraph aligned when entering "
4001 "protected mode (seg=%d)", seg);
4002 }
4003
4004 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05004005 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004006 vmcs_write32(sf->limit, var.limit);
4007 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008}
4009
4010static void enter_rmode(struct kvm_vcpu *vcpu)
4011{
4012 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004013 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004014
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4019 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004022
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004023 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024
Gleb Natapov776e58e2011-03-13 12:34:27 +02004025 /*
4026 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004027 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004028 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004029 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004030 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4031 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004032
Avi Kivity2fb92db2011-04-27 19:42:18 +03004033 vmx_segment_cache_clear(vmx);
4034
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004035 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004036 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004037 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4038
4039 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004040 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004041
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004042 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043
4044 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004045 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046 update_exception_bitmap(vcpu);
4047
Gleb Natapovd99e4152012-12-20 16:57:45 +02004048 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4049 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4050 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4051 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4052 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4053 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004054
Eddie Dong8668a3c2007-10-10 14:26:45 +08004055 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056}
4057
Amit Shah401d10d2009-02-20 22:53:37 +05304058static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4059{
4060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004061 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4062
4063 if (!msr)
4064 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304065
Avi Kivity44ea2b12009-09-06 15:55:37 +03004066 /*
4067 * Force kernel_gs_base reloading before EFER changes, as control
4068 * of this msr depends on is_long_mode().
4069 */
4070 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004071 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304072 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004073 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304074 msr->data = efer;
4075 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004076 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304077
4078 msr->data = efer & ~EFER_LME;
4079 }
4080 setup_msrs(vmx);
4081}
4082
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004083#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084
4085static void enter_lmode(struct kvm_vcpu *vcpu)
4086{
4087 u32 guest_tr_ar;
4088
Avi Kivity2fb92db2011-04-27 19:42:18 +03004089 vmx_segment_cache_clear(to_vmx(vcpu));
4090
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004092 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004093 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4094 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004096 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4097 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 }
Avi Kivityda38f432010-07-06 11:30:49 +03004099 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004100}
4101
4102static void exit_lmode(struct kvm_vcpu *vcpu)
4103{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004104 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004105 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106}
4107
4108#endif
4109
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004110static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004111{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004112 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004113 if (enable_ept) {
4114 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4115 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004116 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004117 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004118}
4119
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004120static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4121{
4122 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4123}
4124
Jim Mattson8386ff52017-03-16 13:53:59 -07004125static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4126{
4127 if (enable_ept)
4128 vmx_flush_tlb(vcpu);
4129}
4130
Avi Kivitye8467fd2009-12-29 18:43:06 +02004131static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4132{
4133 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4134
4135 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4136 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4137}
4138
Avi Kivityaff48ba2010-12-05 18:56:11 +02004139static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4140{
4141 if (enable_ept && is_paging(vcpu))
4142 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4143 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4144}
4145
Anthony Liguori25c4c272007-04-27 09:29:21 +03004146static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004147{
Avi Kivityfc78f512009-12-07 12:16:48 +02004148 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4149
4150 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4151 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004152}
4153
Sheng Yang14394422008-04-28 12:24:45 +08004154static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4155{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004156 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4157
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004158 if (!test_bit(VCPU_EXREG_PDPTR,
4159 (unsigned long *)&vcpu->arch.regs_dirty))
4160 return;
4161
Sheng Yang14394422008-04-28 12:24:45 +08004162 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004163 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4164 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4165 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4166 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004167 }
4168}
4169
Avi Kivity8f5d5492009-05-31 18:41:29 +03004170static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4171{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004172 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4173
Avi Kivity8f5d5492009-05-31 18:41:29 +03004174 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004175 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4176 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4177 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4178 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004179 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004180
4181 __set_bit(VCPU_EXREG_PDPTR,
4182 (unsigned long *)&vcpu->arch.regs_avail);
4183 __set_bit(VCPU_EXREG_PDPTR,
4184 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004185}
4186
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004187static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004188
4189static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4190 unsigned long cr0,
4191 struct kvm_vcpu *vcpu)
4192{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004193 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4194 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004195 if (!(cr0 & X86_CR0_PG)) {
4196 /* From paging/starting to nonpaging */
4197 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004198 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004199 (CPU_BASED_CR3_LOAD_EXITING |
4200 CPU_BASED_CR3_STORE_EXITING));
4201 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004202 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004203 } else if (!is_paging(vcpu)) {
4204 /* From nonpaging to paging */
4205 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004206 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004207 ~(CPU_BASED_CR3_LOAD_EXITING |
4208 CPU_BASED_CR3_STORE_EXITING));
4209 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004210 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004211 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004212
4213 if (!(cr0 & X86_CR0_WP))
4214 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004215}
4216
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4218{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004220 unsigned long hw_cr0;
4221
Gleb Natapov50378782013-02-04 16:00:28 +02004222 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004223 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004224 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004225 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004226 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004227
Gleb Natapov218e7632013-01-21 15:36:45 +02004228 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4229 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230
Gleb Natapov218e7632013-01-21 15:36:45 +02004231 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4232 enter_rmode(vcpu);
4233 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004235#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004236 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004237 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004238 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004239 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240 exit_lmode(vcpu);
4241 }
4242#endif
4243
Avi Kivity089d0342009-03-23 18:26:32 +02004244 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004245 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4246
Avi Kivity02daab22009-12-30 12:40:26 +02004247 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004248 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004249
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004251 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004252 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004253
4254 /* depends on vcpu->arch.cr0 to be set to a new value */
4255 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004256}
4257
Sheng Yang14394422008-04-28 12:24:45 +08004258static u64 construct_eptp(unsigned long root_hpa)
4259{
4260 u64 eptp;
4261
4262 /* TODO write the value reading from MSR */
4263 eptp = VMX_EPT_DEFAULT_MT |
4264 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004265 if (enable_ept_ad_bits)
4266 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004267 eptp |= (root_hpa & PAGE_MASK);
4268
4269 return eptp;
4270}
4271
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4273{
Sheng Yang14394422008-04-28 12:24:45 +08004274 unsigned long guest_cr3;
4275 u64 eptp;
4276
4277 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004278 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004279 eptp = construct_eptp(cr3);
4280 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004281 if (is_paging(vcpu) || is_guest_mode(vcpu))
4282 guest_cr3 = kvm_read_cr3(vcpu);
4283 else
4284 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004285 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004286 }
4287
Sheng Yang2384d2b2008-01-17 15:14:33 +08004288 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004289 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004290}
4291
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004292static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004294 /*
4295 * Pass through host's Machine Check Enable value to hw_cr4, which
4296 * is in force while we are in guest mode. Do not let guests control
4297 * this bit, even if host CR4.MCE == 0.
4298 */
4299 unsigned long hw_cr4 =
4300 (cr4_read_shadow() & X86_CR4_MCE) |
4301 (cr4 & ~X86_CR4_MCE) |
4302 (to_vmx(vcpu)->rmode.vm86_active ?
4303 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004304
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004305 if (cr4 & X86_CR4_VMXE) {
4306 /*
4307 * To use VMXON (and later other VMX instructions), a guest
4308 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4309 * So basically the check on whether to allow nested VMX
4310 * is here.
4311 */
4312 if (!nested_vmx_allowed(vcpu))
4313 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004314 }
4315 if (to_vmx(vcpu)->nested.vmxon &&
4316 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004317 return 1;
4318
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004319 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004320 if (enable_ept) {
4321 if (!is_paging(vcpu)) {
4322 hw_cr4 &= ~X86_CR4_PAE;
4323 hw_cr4 |= X86_CR4_PSE;
4324 } else if (!(cr4 & X86_CR4_PAE)) {
4325 hw_cr4 &= ~X86_CR4_PAE;
4326 }
4327 }
Sheng Yang14394422008-04-28 12:24:45 +08004328
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004329 if (!enable_unrestricted_guest && !is_paging(vcpu))
4330 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004331 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4332 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4333 * to be manually disabled when guest switches to non-paging
4334 * mode.
4335 *
4336 * If !enable_unrestricted_guest, the CPU is always running
4337 * with CR0.PG=1 and CR4 needs to be modified.
4338 * If enable_unrestricted_guest, the CPU automatically
4339 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004340 */
Huaitong Handdba2622016-03-22 16:51:15 +08004341 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004342
Sheng Yang14394422008-04-28 12:24:45 +08004343 vmcs_writel(CR4_READ_SHADOW, cr4);
4344 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004345 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346}
4347
Avi Kivity6aa8b732006-12-10 02:21:36 -08004348static void vmx_get_segment(struct kvm_vcpu *vcpu,
4349 struct kvm_segment *var, int seg)
4350{
Avi Kivitya9179492011-01-03 14:28:52 +02004351 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004352 u32 ar;
4353
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004354 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004355 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004356 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004357 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004358 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004359 var->base = vmx_read_guest_seg_base(vmx, seg);
4360 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4361 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004362 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004363 var->base = vmx_read_guest_seg_base(vmx, seg);
4364 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4365 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4366 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004367 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368 var->type = ar & 15;
4369 var->s = (ar >> 4) & 1;
4370 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004371 /*
4372 * Some userspaces do not preserve unusable property. Since usable
4373 * segment has to be present according to VMX spec we can use present
4374 * property to amend userspace bug by making unusable segment always
4375 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4376 * segment as unusable.
4377 */
4378 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004379 var->avl = (ar >> 12) & 1;
4380 var->l = (ar >> 13) & 1;
4381 var->db = (ar >> 14) & 1;
4382 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383}
4384
Avi Kivitya9179492011-01-03 14:28:52 +02004385static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4386{
Avi Kivitya9179492011-01-03 14:28:52 +02004387 struct kvm_segment s;
4388
4389 if (to_vmx(vcpu)->rmode.vm86_active) {
4390 vmx_get_segment(vcpu, &s, seg);
4391 return s.base;
4392 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004393 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004394}
4395
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004396static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004397{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004398 struct vcpu_vmx *vmx = to_vmx(vcpu);
4399
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004400 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004401 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004402 else {
4403 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004404 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004405 }
Avi Kivity69c73022011-03-07 15:26:44 +02004406}
4407
Avi Kivity653e3102007-05-07 10:55:37 +03004408static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410 u32 ar;
4411
Avi Kivityf0495f92012-06-07 17:06:10 +03004412 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413 ar = 1 << 16;
4414 else {
4415 ar = var->type & 15;
4416 ar |= (var->s & 1) << 4;
4417 ar |= (var->dpl & 3) << 5;
4418 ar |= (var->present & 1) << 7;
4419 ar |= (var->avl & 1) << 12;
4420 ar |= (var->l & 1) << 13;
4421 ar |= (var->db & 1) << 14;
4422 ar |= (var->g & 1) << 15;
4423 }
Avi Kivity653e3102007-05-07 10:55:37 +03004424
4425 return ar;
4426}
4427
4428static void vmx_set_segment(struct kvm_vcpu *vcpu,
4429 struct kvm_segment *var, int seg)
4430{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004431 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004432 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004433
Avi Kivity2fb92db2011-04-27 19:42:18 +03004434 vmx_segment_cache_clear(vmx);
4435
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004436 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4437 vmx->rmode.segs[seg] = *var;
4438 if (seg == VCPU_SREG_TR)
4439 vmcs_write16(sf->selector, var->selector);
4440 else if (var->s)
4441 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004442 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004443 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004444
Avi Kivity653e3102007-05-07 10:55:37 +03004445 vmcs_writel(sf->base, var->base);
4446 vmcs_write32(sf->limit, var->limit);
4447 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004448
4449 /*
4450 * Fix the "Accessed" bit in AR field of segment registers for older
4451 * qemu binaries.
4452 * IA32 arch specifies that at the time of processor reset the
4453 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004454 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004455 * state vmexit when "unrestricted guest" mode is turned on.
4456 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4457 * tree. Newer qemu binaries with that qemu fix would not need this
4458 * kvm hack.
4459 */
4460 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004461 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004462
Gleb Natapovf924d662012-12-12 19:10:55 +02004463 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004464
4465out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004466 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004467}
4468
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4470{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004471 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472
4473 *db = (ar >> 14) & 1;
4474 *l = (ar >> 13) & 1;
4475}
4476
Gleb Natapov89a27f42010-02-16 10:51:48 +02004477static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004479 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4480 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004481}
4482
Gleb Natapov89a27f42010-02-16 10:51:48 +02004483static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004485 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4486 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487}
4488
Gleb Natapov89a27f42010-02-16 10:51:48 +02004489static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004490{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004491 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4492 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493}
4494
Gleb Natapov89a27f42010-02-16 10:51:48 +02004495static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004497 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4498 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499}
4500
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004501static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4502{
4503 struct kvm_segment var;
4504 u32 ar;
4505
4506 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004507 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004508 if (seg == VCPU_SREG_CS)
4509 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004510 ar = vmx_segment_access_rights(&var);
4511
4512 if (var.base != (var.selector << 4))
4513 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004514 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004515 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004516 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004517 return false;
4518
4519 return true;
4520}
4521
4522static bool code_segment_valid(struct kvm_vcpu *vcpu)
4523{
4524 struct kvm_segment cs;
4525 unsigned int cs_rpl;
4526
4527 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004528 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004529
Avi Kivity1872a3f2009-01-04 23:26:52 +02004530 if (cs.unusable)
4531 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004532 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004533 return false;
4534 if (!cs.s)
4535 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004536 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004537 if (cs.dpl > cs_rpl)
4538 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004539 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004540 if (cs.dpl != cs_rpl)
4541 return false;
4542 }
4543 if (!cs.present)
4544 return false;
4545
4546 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4547 return true;
4548}
4549
4550static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4551{
4552 struct kvm_segment ss;
4553 unsigned int ss_rpl;
4554
4555 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004556 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004557
Avi Kivity1872a3f2009-01-04 23:26:52 +02004558 if (ss.unusable)
4559 return true;
4560 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004561 return false;
4562 if (!ss.s)
4563 return false;
4564 if (ss.dpl != ss_rpl) /* DPL != RPL */
4565 return false;
4566 if (!ss.present)
4567 return false;
4568
4569 return true;
4570}
4571
4572static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4573{
4574 struct kvm_segment var;
4575 unsigned int rpl;
4576
4577 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004578 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004579
Avi Kivity1872a3f2009-01-04 23:26:52 +02004580 if (var.unusable)
4581 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004582 if (!var.s)
4583 return false;
4584 if (!var.present)
4585 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004586 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004587 if (var.dpl < rpl) /* DPL < RPL */
4588 return false;
4589 }
4590
4591 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4592 * rights flags
4593 */
4594 return true;
4595}
4596
4597static bool tr_valid(struct kvm_vcpu *vcpu)
4598{
4599 struct kvm_segment tr;
4600
4601 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4602
Avi Kivity1872a3f2009-01-04 23:26:52 +02004603 if (tr.unusable)
4604 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004605 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004606 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004607 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004608 return false;
4609 if (!tr.present)
4610 return false;
4611
4612 return true;
4613}
4614
4615static bool ldtr_valid(struct kvm_vcpu *vcpu)
4616{
4617 struct kvm_segment ldtr;
4618
4619 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4620
Avi Kivity1872a3f2009-01-04 23:26:52 +02004621 if (ldtr.unusable)
4622 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004623 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004624 return false;
4625 if (ldtr.type != 2)
4626 return false;
4627 if (!ldtr.present)
4628 return false;
4629
4630 return true;
4631}
4632
4633static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4634{
4635 struct kvm_segment cs, ss;
4636
4637 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4638 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4639
Nadav Amitb32a9912015-03-29 16:33:04 +03004640 return ((cs.selector & SEGMENT_RPL_MASK) ==
4641 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004642}
4643
4644/*
4645 * Check if guest state is valid. Returns true if valid, false if
4646 * not.
4647 * We assume that registers are always usable
4648 */
4649static bool guest_state_valid(struct kvm_vcpu *vcpu)
4650{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004651 if (enable_unrestricted_guest)
4652 return true;
4653
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004654 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004655 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004656 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4657 return false;
4658 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4659 return false;
4660 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4661 return false;
4662 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4663 return false;
4664 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4665 return false;
4666 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4667 return false;
4668 } else {
4669 /* protected mode guest state checks */
4670 if (!cs_ss_rpl_check(vcpu))
4671 return false;
4672 if (!code_segment_valid(vcpu))
4673 return false;
4674 if (!stack_segment_valid(vcpu))
4675 return false;
4676 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4677 return false;
4678 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4679 return false;
4680 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4681 return false;
4682 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4683 return false;
4684 if (!tr_valid(vcpu))
4685 return false;
4686 if (!ldtr_valid(vcpu))
4687 return false;
4688 }
4689 /* TODO:
4690 * - Add checks on RIP
4691 * - Add checks on RFLAGS
4692 */
4693
4694 return true;
4695}
4696
Mike Dayd77c26f2007-10-08 09:02:08 -04004697static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004698{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004699 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004700 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004701 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004703 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004704 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004705 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4706 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004707 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004708 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004709 r = kvm_write_guest_page(kvm, fn++, &data,
4710 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004711 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004712 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004713 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4714 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004715 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004716 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4717 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004718 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004719 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004720 r = kvm_write_guest_page(kvm, fn, &data,
4721 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4722 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004723out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004724 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004725 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726}
4727
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004728static int init_rmode_identity_map(struct kvm *kvm)
4729{
Tang Chenf51770e2014-09-16 18:41:59 +08004730 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004731 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004732 u32 tmp;
4733
Avi Kivity089d0342009-03-23 18:26:32 +02004734 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004735 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004736
4737 /* Protect kvm->arch.ept_identity_pagetable_done. */
4738 mutex_lock(&kvm->slots_lock);
4739
Tang Chenf51770e2014-09-16 18:41:59 +08004740 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004741 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004742
Sheng Yangb927a3c2009-07-21 10:42:48 +08004743 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004744
4745 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004746 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004747 goto out2;
4748
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004749 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004750 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4751 if (r < 0)
4752 goto out;
4753 /* Set up identity-mapping pagetable for EPT in real mode */
4754 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4755 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4756 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4757 r = kvm_write_guest_page(kvm, identity_map_pfn,
4758 &tmp, i * sizeof(tmp), sizeof(tmp));
4759 if (r < 0)
4760 goto out;
4761 }
4762 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004763
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004764out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004765 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004766
4767out2:
4768 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004769 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004770}
4771
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772static void seg_setup(int seg)
4773{
Mathias Krause772e0312012-08-30 01:30:19 +02004774 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004775 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004776
4777 vmcs_write16(sf->selector, 0);
4778 vmcs_writel(sf->base, 0);
4779 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004780 ar = 0x93;
4781 if (seg == VCPU_SREG_CS)
4782 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004783
4784 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785}
4786
Sheng Yangf78e0e22007-10-29 09:40:42 +08004787static int alloc_apic_access_page(struct kvm *kvm)
4788{
Xiao Guangrong44841412012-09-07 14:14:20 +08004789 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004790 int r = 0;
4791
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004792 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004793 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004794 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004795 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4796 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004797 if (r)
4798 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004799
Tang Chen73a6d942014-09-11 13:38:00 +08004800 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004801 if (is_error_page(page)) {
4802 r = -EFAULT;
4803 goto out;
4804 }
4805
Tang Chenc24ae0d2014-09-24 15:57:58 +08004806 /*
4807 * Do not pin the page in memory, so that memory hot-unplug
4808 * is able to migrate it.
4809 */
4810 put_page(page);
4811 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004812out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004813 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004814 return r;
4815}
4816
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004817static int alloc_identity_pagetable(struct kvm *kvm)
4818{
Tang Chena255d472014-09-16 18:41:58 +08004819 /* Called with kvm->slots_lock held. */
4820
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004821 int r = 0;
4822
Tang Chena255d472014-09-16 18:41:58 +08004823 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4824
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004825 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4826 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004827
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004828 return r;
4829}
4830
Wanpeng Li991e7a02015-09-16 17:30:05 +08004831static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004832{
4833 int vpid;
4834
Avi Kivity919818a2009-03-23 18:01:29 +02004835 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004836 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004837 spin_lock(&vmx_vpid_lock);
4838 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004839 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004840 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004841 else
4842 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004843 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004844 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004845}
4846
Wanpeng Li991e7a02015-09-16 17:30:05 +08004847static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004848{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004849 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004850 return;
4851 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004852 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004853 spin_unlock(&vmx_vpid_lock);
4854}
4855
Yi Wang0c4d57a2018-11-08 11:22:21 +08004856static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004857 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004858{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004859 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004860
4861 if (!cpu_has_vmx_msr_bitmap())
4862 return;
4863
4864 /*
4865 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4866 * have the write-low and read-high bitmap offsets the wrong way round.
4867 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4868 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004869 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004870 if (type & MSR_TYPE_R)
4871 /* read-low */
4872 __clear_bit(msr, msr_bitmap + 0x000 / f);
4873
4874 if (type & MSR_TYPE_W)
4875 /* write-low */
4876 __clear_bit(msr, msr_bitmap + 0x800 / f);
4877
Sheng Yang25c5f222008-03-28 13:18:56 +08004878 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4879 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004880 if (type & MSR_TYPE_R)
4881 /* read-high */
4882 __clear_bit(msr, msr_bitmap + 0x400 / f);
4883
4884 if (type & MSR_TYPE_W)
4885 /* write-high */
4886 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4887
4888 }
4889}
4890
Yi Wang0c4d57a2018-11-08 11:22:21 +08004891static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004892 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004893{
4894 int f = sizeof(unsigned long);
4895
4896 if (!cpu_has_vmx_msr_bitmap())
4897 return;
4898
4899 /*
4900 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4901 * have the write-low and read-high bitmap offsets the wrong way round.
4902 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4903 */
4904 if (msr <= 0x1fff) {
4905 if (type & MSR_TYPE_R)
4906 /* read-low */
4907 __set_bit(msr, msr_bitmap + 0x000 / f);
4908
4909 if (type & MSR_TYPE_W)
4910 /* write-low */
4911 __set_bit(msr, msr_bitmap + 0x800 / f);
4912
4913 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4914 msr &= 0x1fff;
4915 if (type & MSR_TYPE_R)
4916 /* read-high */
4917 __set_bit(msr, msr_bitmap + 0x400 / f);
4918
4919 if (type & MSR_TYPE_W)
4920 /* write-high */
4921 __set_bit(msr, msr_bitmap + 0xc00 / f);
4922
Sheng Yang25c5f222008-03-28 13:18:56 +08004923 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004924}
4925
Yi Wang0c4d57a2018-11-08 11:22:21 +08004926static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004927 u32 msr, int type, bool value)
4928{
4929 if (value)
4930 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4931 else
4932 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4933}
4934
Wincy Vanf2b93282015-02-03 23:56:03 +08004935/*
4936 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4937 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4938 */
4939static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4940 unsigned long *msr_bitmap_nested,
4941 u32 msr, int type)
4942{
4943 int f = sizeof(unsigned long);
4944
4945 if (!cpu_has_vmx_msr_bitmap()) {
4946 WARN_ON(1);
4947 return;
4948 }
4949
4950 /*
4951 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4952 * have the write-low and read-high bitmap offsets the wrong way round.
4953 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4954 */
4955 if (msr <= 0x1fff) {
4956 if (type & MSR_TYPE_R &&
4957 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4958 /* read-low */
4959 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4960
4961 if (type & MSR_TYPE_W &&
4962 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4963 /* write-low */
4964 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4965
4966 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4967 msr &= 0x1fff;
4968 if (type & MSR_TYPE_R &&
4969 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4970 /* read-high */
4971 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4972
4973 if (type & MSR_TYPE_W &&
4974 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4975 /* write-high */
4976 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4977
4978 }
4979}
4980
Paolo Bonzini6236b782018-01-16 16:51:18 +01004981static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004982{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004983 u8 mode = 0;
4984
4985 if (cpu_has_secondary_exec_ctrls() &&
4986 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4987 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4988 mode |= MSR_BITMAP_MODE_X2APIC;
4989 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4990 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4991 }
4992
4993 if (is_long_mode(vcpu))
4994 mode |= MSR_BITMAP_MODE_LM;
4995
4996 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004997}
4998
Paolo Bonzini6236b782018-01-16 16:51:18 +01004999#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5000
5001static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5002 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005003{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005004 int msr;
5005
5006 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5007 unsigned word = msr / BITS_PER_LONG;
5008 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5009 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5010 }
5011
5012 if (mode & MSR_BITMAP_MODE_X2APIC) {
5013 /*
5014 * TPR reads and writes can be virtualized even if virtual interrupt
5015 * delivery is not in use.
5016 */
5017 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5018 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5019 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5020 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5021 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5022 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005023 }
Yang Zhang8d146952013-01-25 10:18:50 +08005024}
5025
Paolo Bonzini6236b782018-01-16 16:51:18 +01005026static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005027{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005028 struct vcpu_vmx *vmx = to_vmx(vcpu);
5029 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5030 u8 mode = vmx_msr_bitmap_mode(vcpu);
5031 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005032
Paolo Bonzini6236b782018-01-16 16:51:18 +01005033 if (!changed)
5034 return;
5035
5036 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5037 !(mode & MSR_BITMAP_MODE_LM));
5038
5039 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5040 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5041
5042 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005043}
5044
Andrey Smetanind62caab2015-11-10 15:36:33 +03005045static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005046{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005047 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005048}
5049
David Matlackb7649e12017-08-01 14:00:40 -07005050static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5051{
5052 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5053 gfn_t gfn;
5054
5055 /*
5056 * Don't need to mark the APIC access page dirty; it is never
5057 * written to by the CPU during APIC virtualization.
5058 */
5059
5060 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5061 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5062 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5063 }
5064
5065 if (nested_cpu_has_posted_intr(vmcs12)) {
5066 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5067 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5068 }
5069}
5070
5071
David Hildenbrand1edccf22017-01-25 11:58:58 +01005072static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005073{
5074 struct vcpu_vmx *vmx = to_vmx(vcpu);
5075 int max_irr;
5076 void *vapic_page;
5077 u16 status;
5078
David Matlackb7649e12017-08-01 14:00:40 -07005079 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5080 return;
Wincy Van705699a2015-02-03 23:58:17 +08005081
David Matlackb7649e12017-08-01 14:00:40 -07005082 vmx->nested.pi_pending = false;
5083 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5084 return;
Wincy Van705699a2015-02-03 23:58:17 +08005085
David Matlackb7649e12017-08-01 14:00:40 -07005086 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5087 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005088 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005089 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5090 kunmap(vmx->nested.virtual_apic_page);
5091
5092 status = vmcs_read16(GUEST_INTR_STATUS);
5093 if ((u8)max_irr > ((u8)status & 0xff)) {
5094 status &= ~0xff;
5095 status |= (u8)max_irr;
5096 vmcs_write16(GUEST_INTR_STATUS, status);
5097 }
5098 }
David Matlackb7649e12017-08-01 14:00:40 -07005099
5100 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005101}
5102
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005103static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5104{
5105#ifdef CONFIG_SMP
5106 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005107 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005108 * The vector of interrupt to be delivered to vcpu had
5109 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005110 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005111 * Following cases will be reached in this block, and
5112 * we always send a notification event in all cases as
5113 * explained below.
5114 *
5115 * Case 1: vcpu keeps in non-root mode. Sending a
5116 * notification event posts the interrupt to vcpu.
5117 *
5118 * Case 2: vcpu exits to root mode and is still
5119 * runnable. PIR will be synced to vIRR before the
5120 * next vcpu entry. Sending a notification event in
5121 * this case has no effect, as vcpu is not in root
5122 * mode.
5123 *
5124 * Case 3: vcpu exits to root mode and is blocked.
5125 * vcpu_block() has already synced PIR to vIRR and
5126 * never blocks vcpu if vIRR is not cleared. Therefore,
5127 * a blocked vcpu here does not wait for any requested
5128 * interrupts in PIR, and sending a notification event
5129 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005130 */
Feng Wu28b835d2015-09-18 22:29:54 +08005131
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005132 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5133 POSTED_INTR_VECTOR);
5134 return true;
5135 }
5136#endif
5137 return false;
5138}
5139
Wincy Van705699a2015-02-03 23:58:17 +08005140static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5141 int vector)
5142{
5143 struct vcpu_vmx *vmx = to_vmx(vcpu);
5144
5145 if (is_guest_mode(vcpu) &&
5146 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005147 /*
5148 * If a posted intr is not recognized by hardware,
5149 * we will accomplish it in the next vmentry.
5150 */
5151 vmx->nested.pi_pending = true;
5152 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005153 /* the PIR and ON have been set by L1. */
5154 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5155 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005156 return 0;
5157 }
5158 return -1;
5159}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005160/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005161 * Send interrupt to vcpu via posted interrupt way.
5162 * 1. If target vcpu is running(non-root mode), send posted interrupt
5163 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5164 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5165 * interrupt from PIR in next vmentry.
5166 */
5167static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5168{
5169 struct vcpu_vmx *vmx = to_vmx(vcpu);
5170 int r;
5171
Wincy Van705699a2015-02-03 23:58:17 +08005172 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5173 if (!r)
5174 return;
5175
Yang Zhanga20ed542013-04-11 19:25:15 +08005176 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5177 return;
5178
5179 r = pi_test_and_set_on(&vmx->pi_desc);
5180 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005181 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005182 kvm_vcpu_kick(vcpu);
5183}
5184
5185static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5186{
5187 struct vcpu_vmx *vmx = to_vmx(vcpu);
5188
5189 if (!pi_test_and_clear_on(&vmx->pi_desc))
5190 return;
5191
5192 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5193}
5194
Avi Kivity6aa8b732006-12-10 02:21:36 -08005195/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005196 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5197 * will not change in the lifetime of the guest.
5198 * Note that host-state that does change is set elsewhere. E.g., host-state
5199 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5200 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005201static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005202{
5203 u32 low32, high32;
5204 unsigned long tmpl;
5205 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005206 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005207
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005208 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005209 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5210
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005211 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005212 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005213 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5214 vmx->host_state.vmcs_host_cr4 = cr4;
5215
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005216 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005217#ifdef CONFIG_X86_64
5218 /*
5219 * Load null selectors, so we can avoid reloading them in
5220 * __vmx_load_host_state(), in case userspace uses the null selectors
5221 * too (the expected case).
5222 */
5223 vmcs_write16(HOST_DS_SELECTOR, 0);
5224 vmcs_write16(HOST_ES_SELECTOR, 0);
5225#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005226 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5227 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005228#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005229 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5230 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5231
5232 native_store_idt(&dt);
5233 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005234 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005235
Avi Kivity83287ea422012-09-16 15:10:57 +03005236 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005237
5238 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5239 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5240 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5241 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5242
5243 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5244 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5245 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5246 }
5247}
5248
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005249static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5250{
5251 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5252 if (enable_ept)
5253 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005254 if (is_guest_mode(&vmx->vcpu))
5255 vmx->vcpu.arch.cr4_guest_owned_bits &=
5256 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005257 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5258}
5259
Yang Zhang01e439b2013-04-11 19:25:12 +08005260static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5261{
5262 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5263
Andrey Smetanind62caab2015-11-10 15:36:33 +03005264 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005265 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005266 /* Enable the preemption timer dynamically */
5267 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005268 return pin_based_exec_ctrl;
5269}
5270
Andrey Smetanind62caab2015-11-10 15:36:33 +03005271static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5272{
5273 struct vcpu_vmx *vmx = to_vmx(vcpu);
5274
5275 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005276 if (cpu_has_secondary_exec_ctrls()) {
5277 if (kvm_vcpu_apicv_active(vcpu))
5278 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5279 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5280 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5281 else
5282 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5283 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5284 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5285 }
5286
5287 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005288 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005289}
5290
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005291static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5292{
5293 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005294
5295 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5296 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5297
Paolo Bonzini35754c92015-07-29 12:05:37 +02005298 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005299 exec_control &= ~CPU_BASED_TPR_SHADOW;
5300#ifdef CONFIG_X86_64
5301 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5302 CPU_BASED_CR8_LOAD_EXITING;
5303#endif
5304 }
5305 if (!enable_ept)
5306 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5307 CPU_BASED_CR3_LOAD_EXITING |
5308 CPU_BASED_INVLPG_EXITING;
5309 return exec_control;
5310}
5311
5312static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5313{
5314 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005315 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005316 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5317 if (vmx->vpid == 0)
5318 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5319 if (!enable_ept) {
5320 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5321 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005322 /* Enable INVPCID for non-ept guests may cause performance regression. */
5323 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005324 }
5325 if (!enable_unrestricted_guest)
5326 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5327 if (!ple_gap)
5328 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005329 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005330 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5331 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005332 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005333 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5334 (handle_vmptrld).
5335 We can NOT enable shadow_vmcs here because we don't have yet
5336 a current VMCS12
5337 */
5338 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005339
5340 if (!enable_pml)
5341 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005342
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005343 return exec_control;
5344}
5345
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005346static void ept_set_mmio_spte_mask(void)
5347{
5348 /*
5349 * EPT Misconfigurations can be generated if the value of bits 2:0
5350 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005351 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005352 * spte.
5353 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005354 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005355}
5356
Wanpeng Lif53cd632014-12-02 19:14:58 +08005357#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005358/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359 * Sets up the vmcs for emulated real mode.
5360 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005361static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005362{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005363#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005364 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005365#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005366 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005367
Avi Kivity6aa8b732006-12-10 02:21:36 -08005368 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005369 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5370 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005371
Abel Gordon4607c2d2013-04-18 14:35:55 +03005372 if (enable_shadow_vmcs) {
5373 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5374 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5375 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005376 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005377 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005378
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5380
Avi Kivity6aa8b732006-12-10 02:21:36 -08005381 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005382 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005383 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005384
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005385 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005386
Dan Williamsdfa169b2016-06-02 11:17:24 -07005387 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005388 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5389 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005390 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005391
Andrey Smetanind62caab2015-11-10 15:36:33 +03005392 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005393 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5394 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5395 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5396 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5397
5398 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005399
Li RongQing0bcf2612015-12-03 13:29:34 +08005400 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005401 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005402 }
5403
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005404 if (ple_gap) {
5405 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005406 vmx->ple_window = ple_window;
5407 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005408 }
5409
Xiao Guangrongc3707952011-07-12 03:28:04 +08005410 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5411 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005412 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5413
Avi Kivity9581d442010-10-19 16:46:55 +02005414 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5415 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005416 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005417#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005418 rdmsrl(MSR_FS_BASE, a);
5419 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5420 rdmsrl(MSR_GS_BASE, a);
5421 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5422#else
5423 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5424 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5425#endif
5426
Eddie Dong2cc51562007-05-21 07:28:09 +03005427 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5428 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005429 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005430 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005431 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005432
Radim Krčmář74545702015-04-27 15:11:25 +02005433 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5434 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005435
Paolo Bonzini03916db2014-07-24 14:21:57 +02005436 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005437 u32 index = vmx_msr_index[i];
5438 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005439 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005440
5441 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5442 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005443 if (wrmsr_safe(index, data_low, data_high) < 0)
5444 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005445 vmx->guest_msrs[j].index = i;
5446 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005447 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005448 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005449 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450
Gleb Natapov2961e8762013-11-25 15:37:13 +02005451 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452
5453 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005454 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005455
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005456 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005457 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005458
Wanpeng Lif53cd632014-12-02 19:14:58 +08005459 if (vmx_xsaves_supported())
5460 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5461
Peter Feiner4e595162016-07-07 14:49:58 -07005462 if (enable_pml) {
5463 ASSERT(vmx->pml_pg);
5464 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5465 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5466 }
5467
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005468 return 0;
5469}
5470
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005471static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005472{
5473 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005474 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005475 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005476
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005477 vmx->rmode.vm86_active = 0;
Wanpeng Li7a1eac82018-02-28 14:03:31 +08005478 vcpu->arch.microcode_version = 0x100000000ULL;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005479 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005480
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005481 vmx->soft_vnmi_blocked = 0;
5482
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005483 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005484 kvm_set_cr8(vcpu, 0);
5485
5486 if (!init_event) {
5487 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5488 MSR_IA32_APICBASE_ENABLE;
5489 if (kvm_vcpu_is_reset_bsp(vcpu))
5490 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5491 apic_base_msr.host_initiated = true;
5492 kvm_set_apic_base(vcpu, &apic_base_msr);
5493 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005494
Avi Kivity2fb92db2011-04-27 19:42:18 +03005495 vmx_segment_cache_clear(vmx);
5496
Avi Kivity5706be02008-08-20 15:07:31 +03005497 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005498 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005499 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005500
5501 seg_setup(VCPU_SREG_DS);
5502 seg_setup(VCPU_SREG_ES);
5503 seg_setup(VCPU_SREG_FS);
5504 seg_setup(VCPU_SREG_GS);
5505 seg_setup(VCPU_SREG_SS);
5506
5507 vmcs_write16(GUEST_TR_SELECTOR, 0);
5508 vmcs_writel(GUEST_TR_BASE, 0);
5509 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5510 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5511
5512 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5513 vmcs_writel(GUEST_LDTR_BASE, 0);
5514 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5515 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5516
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005517 if (!init_event) {
5518 vmcs_write32(GUEST_SYSENTER_CS, 0);
5519 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5520 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5521 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5522 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005523
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005524 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005525 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005526
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005527 vmcs_writel(GUEST_GDTR_BASE, 0);
5528 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5529
5530 vmcs_writel(GUEST_IDTR_BASE, 0);
5531 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5532
Anthony Liguori443381a2010-12-06 10:53:38 -06005533 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005534 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005535 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005536
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005537 setup_msrs(vmx);
5538
Avi Kivity6aa8b732006-12-10 02:21:36 -08005539 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5540
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005541 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005542 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005543 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005544 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005545 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005546 vmcs_write32(TPR_THRESHOLD, 0);
5547 }
5548
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005549 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550
Andrey Smetanind62caab2015-11-10 15:36:33 +03005551 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005552 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5553
Sheng Yang2384d2b2008-01-17 15:14:33 +08005554 if (vmx->vpid != 0)
5555 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5556
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005557 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005558 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005559 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005560 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005561 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005562 vmx_fpu_activate(vcpu);
5563 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005565 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005566}
5567
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005568/*
5569 * In nested virtualization, check if L1 asked to exit on external interrupts.
5570 * For most existing hypervisors, this will always return true.
5571 */
5572static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5573{
5574 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5575 PIN_BASED_EXT_INTR_MASK;
5576}
5577
Bandan Das77b0f5d2014-04-19 18:17:45 -04005578/*
5579 * In nested virtualization, check if L1 has set
5580 * VM_EXIT_ACK_INTR_ON_EXIT
5581 */
5582static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5583{
5584 return get_vmcs12(vcpu)->vm_exit_controls &
5585 VM_EXIT_ACK_INTR_ON_EXIT;
5586}
5587
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005588static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5589{
5590 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5591 PIN_BASED_NMI_EXITING;
5592}
5593
Jan Kiszkac9a79532014-03-07 20:03:15 +01005594static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005595{
5596 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005597
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005598 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5599 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5600 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5601}
5602
Jan Kiszkac9a79532014-03-07 20:03:15 +01005603static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005604{
5605 u32 cpu_based_vm_exec_control;
5606
Jan Kiszkac9a79532014-03-07 20:03:15 +01005607 if (!cpu_has_virtual_nmis() ||
5608 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5609 enable_irq_window(vcpu);
5610 return;
5611 }
Jan Kiszka03b28f82013-04-29 16:46:42 +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_NMI_PENDING;
5615 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5616}
5617
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005618static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005619{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005620 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005621 uint32_t intr;
5622 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005623
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005624 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005625
Avi Kivityfa89a812008-09-01 15:57:51 +03005626 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005627 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005628 int inc_eip = 0;
5629 if (vcpu->arch.interrupt.soft)
5630 inc_eip = vcpu->arch.event_exit_inst_len;
5631 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005632 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005633 return;
5634 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005635 intr = irq | INTR_INFO_VALID_MASK;
5636 if (vcpu->arch.interrupt.soft) {
5637 intr |= INTR_TYPE_SOFT_INTR;
5638 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5639 vmx->vcpu.arch.event_exit_inst_len);
5640 } else
5641 intr |= INTR_TYPE_EXT_INTR;
5642 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005643}
5644
Sheng Yangf08864b2008-05-15 18:23:25 +08005645static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5646{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005647 struct vcpu_vmx *vmx = to_vmx(vcpu);
5648
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005649 if (!is_guest_mode(vcpu)) {
5650 if (!cpu_has_virtual_nmis()) {
5651 /*
5652 * Tracking the NMI-blocked state in software is built upon
5653 * finding the next open IRQ window. This, in turn, depends on
5654 * well-behaving guests: They have to keep IRQs disabled at
5655 * least as long as the NMI handler runs. Otherwise we may
5656 * cause NMI nesting, maybe breaking the guest. But as this is
5657 * highly unlikely, we can live with the residual risk.
5658 */
5659 vmx->soft_vnmi_blocked = 1;
5660 vmx->vnmi_blocked_time = 0;
5661 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005662
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005663 ++vcpu->stat.nmi_injections;
5664 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005665 }
5666
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005667 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005668 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005669 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005670 return;
5671 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005672
Sheng Yangf08864b2008-05-15 18:23:25 +08005673 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5674 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005675}
5676
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005677static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5678{
5679 if (!cpu_has_virtual_nmis())
5680 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005681 if (to_vmx(vcpu)->nmi_known_unmasked)
5682 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005683 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005684}
5685
5686static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5687{
5688 struct vcpu_vmx *vmx = to_vmx(vcpu);
5689
5690 if (!cpu_has_virtual_nmis()) {
5691 if (vmx->soft_vnmi_blocked != masked) {
5692 vmx->soft_vnmi_blocked = masked;
5693 vmx->vnmi_blocked_time = 0;
5694 }
5695 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005696 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005697 if (masked)
5698 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5699 GUEST_INTR_STATE_NMI);
5700 else
5701 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5702 GUEST_INTR_STATE_NMI);
5703 }
5704}
5705
Jan Kiszka2505dc92013-04-14 12:12:47 +02005706static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5707{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005708 if (to_vmx(vcpu)->nested.nested_run_pending)
5709 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005710
Jan Kiszka2505dc92013-04-14 12:12:47 +02005711 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5712 return 0;
5713
5714 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5715 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5716 | GUEST_INTR_STATE_NMI));
5717}
5718
Gleb Natapov78646122009-03-23 12:12:11 +02005719static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5720{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005721 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5722 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005723 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5724 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005725}
5726
Izik Eiduscbc94022007-10-25 00:29:55 +02005727static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5728{
5729 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005730
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005731 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5732 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005733 if (ret)
5734 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005735 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005736 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005737}
5738
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005739static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005740{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005741 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005742 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005743 /*
5744 * Update instruction length as we may reinject the exception
5745 * from user space while in guest debugging mode.
5746 */
5747 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5748 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005749 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005750 return false;
5751 /* fall through */
5752 case DB_VECTOR:
5753 if (vcpu->guest_debug &
5754 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5755 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005756 /* fall through */
5757 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005758 case OF_VECTOR:
5759 case BR_VECTOR:
5760 case UD_VECTOR:
5761 case DF_VECTOR:
5762 case SS_VECTOR:
5763 case GP_VECTOR:
5764 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005765 return true;
5766 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005767 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005768 return false;
5769}
5770
5771static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5772 int vec, u32 err_code)
5773{
5774 /*
5775 * Instruction with address size override prefix opcode 0x67
5776 * Cause the #SS fault with 0 error code in VM86 mode.
5777 */
5778 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5779 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5780 if (vcpu->arch.halt_request) {
5781 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005782 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005783 }
5784 return 1;
5785 }
5786 return 0;
5787 }
5788
5789 /*
5790 * Forward all other exceptions that are valid in real mode.
5791 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5792 * the required debugging infrastructure rework.
5793 */
5794 kvm_queue_exception(vcpu, vec);
5795 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005796}
5797
Andi Kleena0861c02009-06-08 17:37:09 +08005798/*
5799 * Trigger machine check on the host. We assume all the MSRs are already set up
5800 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5801 * We pass a fake environment to the machine check handler because we want
5802 * the guest to be always treated like user space, no matter what context
5803 * it used internally.
5804 */
5805static void kvm_machine_check(void)
5806{
5807#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5808 struct pt_regs regs = {
5809 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5810 .flags = X86_EFLAGS_IF,
5811 };
5812
5813 do_machine_check(&regs, 0);
5814#endif
5815}
5816
Avi Kivity851ba692009-08-24 11:10:17 +03005817static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005818{
5819 /* already handled by vcpu_run */
5820 return 1;
5821}
5822
Avi Kivity851ba692009-08-24 11:10:17 +03005823static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005824{
Avi Kivity1155f762007-11-22 11:30:47 +02005825 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005826 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005827 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005828 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005829 u32 vect_info;
5830 enum emulation_result er;
5831
Avi Kivity1155f762007-11-22 11:30:47 +02005832 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005833 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005834
Andi Kleena0861c02009-06-08 17:37:09 +08005835 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005836 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005837
Jim Mattson3f618a02016-12-12 11:01:37 -08005838 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005839 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005840
5841 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005842 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005843 return 1;
5844 }
5845
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005846 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005847 if (is_guest_mode(vcpu)) {
5848 kvm_queue_exception(vcpu, UD_VECTOR);
5849 return 1;
5850 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005851 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005852 if (er == EMULATE_USER_EXIT)
5853 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005854 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005855 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005856 return 1;
5857 }
5858
Avi Kivity6aa8b732006-12-10 02:21:36 -08005859 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005860 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005861 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005862
5863 /*
5864 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5865 * MMIO, it is better to report an internal error.
5866 * See the comments in vmx_handle_exit.
5867 */
5868 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5869 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5870 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5871 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005872 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005873 vcpu->run->internal.data[0] = vect_info;
5874 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005875 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005876 return 0;
5877 }
5878
Avi Kivity6aa8b732006-12-10 02:21:36 -08005879 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005880 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005881 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005882 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005883 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005884 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005885
Gleb Natapov3298b752009-05-11 13:35:46 +03005886 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005887 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005888 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005889 }
5890
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005891 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005892
5893 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5894 return handle_rmode_exception(vcpu, ex_no, error_code);
5895
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005896 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005897 case AC_VECTOR:
5898 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5899 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005900 case DB_VECTOR:
5901 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5902 if (!(vcpu->guest_debug &
5903 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005904 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005905 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005906 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005907 skip_emulated_instruction(vcpu);
5908
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005909 kvm_queue_exception(vcpu, DB_VECTOR);
5910 return 1;
5911 }
5912 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5913 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5914 /* fall through */
5915 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005916 /*
5917 * Update instruction length as we may reinject #BP from
5918 * user space while in guest debugging mode. Reading it for
5919 * #DB as well causes no harm, it is not used in that case.
5920 */
5921 vmx->vcpu.arch.event_exit_inst_len =
5922 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005923 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005924 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005925 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5926 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005927 break;
5928 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005929 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5930 kvm_run->ex.exception = ex_no;
5931 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005932 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005933 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005934 return 0;
5935}
5936
Avi Kivity851ba692009-08-24 11:10:17 +03005937static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005938{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005939 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005940 return 1;
5941}
5942
Avi Kivity851ba692009-08-24 11:10:17 +03005943static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005944{
Avi Kivity851ba692009-08-24 11:10:17 +03005945 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Li5e29da02017-08-09 22:33:12 -07005946 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08005947 return 0;
5948}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005949
Avi Kivity851ba692009-08-24 11:10:17 +03005950static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005951{
He, Qingbfdaab02007-09-12 14:18:28 +08005952 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005953 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005954 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005955
He, Qingbfdaab02007-09-12 14:18:28 +08005956 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005957 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005958 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005959
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005960 ++vcpu->stat.io_exits;
5961
5962 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005963 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005964
5965 port = exit_qualification >> 16;
5966 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005967 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005968
5969 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005970}
5971
Ingo Molnar102d8322007-02-19 14:37:47 +02005972static void
5973vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5974{
5975 /*
5976 * Patch in the VMCALL instruction:
5977 */
5978 hypercall[0] = 0x0f;
5979 hypercall[1] = 0x01;
5980 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005981}
5982
Wincy Vanb9c237b2015-02-03 23:56:30 +08005983static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005984{
5985 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005986 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005987
Wincy Vanb9c237b2015-02-03 23:56:30 +08005988 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005989 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5990 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5991 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5992 return (val & always_on) == always_on;
5993}
5994
Guo Chao0fa06072012-06-28 15:16:19 +08005995/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005996static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5997{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005998 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005999 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6000 unsigned long orig_val = val;
6001
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006002 /*
6003 * We get here when L2 changed cr0 in a way that did not change
6004 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006005 * but did change L0 shadowed bits. So we first calculate the
6006 * effective cr0 value that L1 would like to write into the
6007 * hardware. It consists of the L2-owned bits from the new
6008 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006009 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006010 val = (val & ~vmcs12->cr0_guest_host_mask) |
6011 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6012
Wincy Vanb9c237b2015-02-03 23:56:30 +08006013 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006014 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006015
6016 if (kvm_set_cr0(vcpu, val))
6017 return 1;
6018 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006019 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006020 } else {
6021 if (to_vmx(vcpu)->nested.vmxon &&
6022 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
6023 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006024 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006025 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006026}
6027
6028static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6029{
6030 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006031 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6032 unsigned long orig_val = val;
6033
6034 /* analogously to handle_set_cr0 */
6035 val = (val & ~vmcs12->cr4_guest_host_mask) |
6036 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6037 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006038 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006039 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006040 return 0;
6041 } else
6042 return kvm_set_cr4(vcpu, val);
6043}
6044
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08006045/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006046static void handle_clts(struct kvm_vcpu *vcpu)
6047{
6048 if (is_guest_mode(vcpu)) {
6049 /*
6050 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
6051 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
6052 * just pretend it's off (also in arch.cr0 for fpu_activate).
6053 */
6054 vmcs_writel(CR0_READ_SHADOW,
6055 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
6056 vcpu->arch.cr0 &= ~X86_CR0_TS;
6057 } else
6058 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6059}
6060
Avi Kivity851ba692009-08-24 11:10:17 +03006061static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006062{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006063 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006064 int cr;
6065 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006066 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006067
He, Qingbfdaab02007-09-12 14:18:28 +08006068 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006069 cr = exit_qualification & 15;
6070 reg = (exit_qualification >> 8) & 15;
6071 switch ((exit_qualification >> 4) & 3) {
6072 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006073 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006074 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006075 switch (cr) {
6076 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006077 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006078 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006079 return 1;
6080 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006081 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006082 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006083 return 1;
6084 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006085 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006086 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006087 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006088 case 8: {
6089 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006090 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006091 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006092 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006093 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006094 return 1;
6095 if (cr8_prev <= cr8)
6096 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03006097 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006098 return 0;
6099 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006100 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006101 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006102 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006103 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006104 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006105 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006106 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006107 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006108 case 1: /*mov from cr*/
6109 switch (cr) {
6110 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006111 val = kvm_read_cr3(vcpu);
6112 kvm_register_write(vcpu, reg, val);
6113 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006114 skip_emulated_instruction(vcpu);
6115 return 1;
6116 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006117 val = kvm_get_cr8(vcpu);
6118 kvm_register_write(vcpu, reg, val);
6119 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006120 skip_emulated_instruction(vcpu);
6121 return 1;
6122 }
6123 break;
6124 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006125 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006126 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006127 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006128
6129 skip_emulated_instruction(vcpu);
6130 return 1;
6131 default:
6132 break;
6133 }
Avi Kivity851ba692009-08-24 11:10:17 +03006134 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006135 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006136 (int)(exit_qualification >> 4) & 3, cr);
6137 return 0;
6138}
6139
Avi Kivity851ba692009-08-24 11:10:17 +03006140static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006141{
He, Qingbfdaab02007-09-12 14:18:28 +08006142 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006143 int dr, dr7, reg;
6144
6145 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6146 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6147
6148 /* First, if DR does not exist, trigger UD */
6149 if (!kvm_require_dr(vcpu, dr))
6150 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006151
Jan Kiszkaf2483412010-01-20 18:20:20 +01006152 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006153 if (!kvm_require_cpl(vcpu, 0))
6154 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006155 dr7 = vmcs_readl(GUEST_DR7);
6156 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006157 /*
6158 * As the vm-exit takes precedence over the debug trap, we
6159 * need to emulate the latter, either for the host or the
6160 * guest debugging itself.
6161 */
6162 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006163 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006164 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006165 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006166 vcpu->run->debug.arch.exception = DB_VECTOR;
6167 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006168 return 0;
6169 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006170 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006171 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006172 kvm_queue_exception(vcpu, DB_VECTOR);
6173 return 1;
6174 }
6175 }
6176
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006177 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006178 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6179 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006180
6181 /*
6182 * No more DR vmexits; force a reload of the debug registers
6183 * and reenter on this instruction. The next vmexit will
6184 * retrieve the full state of the debug registers.
6185 */
6186 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6187 return 1;
6188 }
6189
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006190 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6191 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006192 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006193
6194 if (kvm_get_dr(vcpu, dr, &val))
6195 return 1;
6196 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006197 } else
Nadav Amit57773922014-06-18 17:19:23 +03006198 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006199 return 1;
6200
Avi Kivity6aa8b732006-12-10 02:21:36 -08006201 skip_emulated_instruction(vcpu);
6202 return 1;
6203}
6204
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006205static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6206{
6207 return vcpu->arch.dr6;
6208}
6209
6210static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6211{
6212}
6213
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006214static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6215{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006216 get_debugreg(vcpu->arch.db[0], 0);
6217 get_debugreg(vcpu->arch.db[1], 1);
6218 get_debugreg(vcpu->arch.db[2], 2);
6219 get_debugreg(vcpu->arch.db[3], 3);
6220 get_debugreg(vcpu->arch.dr6, 6);
6221 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6222
6223 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006224 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006225}
6226
Gleb Natapov020df072010-04-13 10:05:23 +03006227static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6228{
6229 vmcs_writel(GUEST_DR7, val);
6230}
6231
Avi Kivity851ba692009-08-24 11:10:17 +03006232static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006233{
Avi Kivity06465c52007-02-28 20:46:53 +02006234 kvm_emulate_cpuid(vcpu);
6235 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006236}
6237
Avi Kivity851ba692009-08-24 11:10:17 +03006238static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006240 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006241 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006242
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006243 msr_info.index = ecx;
6244 msr_info.host_initiated = false;
6245 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006246 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006247 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006248 return 1;
6249 }
6250
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006251 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006252
Avi Kivity6aa8b732006-12-10 02:21:36 -08006253 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006254 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6255 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006256 skip_emulated_instruction(vcpu);
6257 return 1;
6258}
6259
Avi Kivity851ba692009-08-24 11:10:17 +03006260static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006261{
Will Auld8fe8ab42012-11-29 12:42:12 -08006262 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006263 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6264 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6265 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006266
Will Auld8fe8ab42012-11-29 12:42:12 -08006267 msr.data = data;
6268 msr.index = ecx;
6269 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006270 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006271 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006272 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006273 return 1;
6274 }
6275
Avi Kivity59200272010-01-25 19:47:02 +02006276 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006277 skip_emulated_instruction(vcpu);
6278 return 1;
6279}
6280
Avi Kivity851ba692009-08-24 11:10:17 +03006281static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006282{
Avi Kivity3842d132010-07-27 12:30:24 +03006283 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006284 return 1;
6285}
6286
Avi Kivity851ba692009-08-24 11:10:17 +03006287static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006288{
Eddie Dong85f455f2007-07-06 12:20:49 +03006289 u32 cpu_based_vm_exec_control;
6290
6291 /* clear pending irq */
6292 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6293 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6294 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006295
Avi Kivity3842d132010-07-27 12:30:24 +03006296 kvm_make_request(KVM_REQ_EVENT, vcpu);
6297
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006298 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006299 return 1;
6300}
6301
Avi Kivity851ba692009-08-24 11:10:17 +03006302static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006303{
Avi Kivityd3bef152007-06-05 15:53:05 +03006304 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006305}
6306
Avi Kivity851ba692009-08-24 11:10:17 +03006307static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006308{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006309 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006310}
6311
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006312static int handle_invd(struct kvm_vcpu *vcpu)
6313{
Andre Przywara51d8b662010-12-21 11:12:02 +01006314 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006315}
6316
Avi Kivity851ba692009-08-24 11:10:17 +03006317static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006318{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006319 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006320
6321 kvm_mmu_invlpg(vcpu, exit_qualification);
6322 skip_emulated_instruction(vcpu);
6323 return 1;
6324}
6325
Avi Kivityfee84b02011-11-10 14:57:25 +02006326static int handle_rdpmc(struct kvm_vcpu *vcpu)
6327{
6328 int err;
6329
6330 err = kvm_rdpmc(vcpu);
6331 kvm_complete_insn_gp(vcpu, err);
6332
6333 return 1;
6334}
6335
Avi Kivity851ba692009-08-24 11:10:17 +03006336static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006337{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006338 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006339 return 1;
6340}
6341
Dexuan Cui2acf9232010-06-10 11:27:12 +08006342static int handle_xsetbv(struct kvm_vcpu *vcpu)
6343{
6344 u64 new_bv = kvm_read_edx_eax(vcpu);
6345 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6346
6347 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6348 skip_emulated_instruction(vcpu);
6349 return 1;
6350}
6351
Wanpeng Lif53cd632014-12-02 19:14:58 +08006352static int handle_xsaves(struct kvm_vcpu *vcpu)
6353{
6354 skip_emulated_instruction(vcpu);
6355 WARN(1, "this should never happen\n");
6356 return 1;
6357}
6358
6359static int handle_xrstors(struct kvm_vcpu *vcpu)
6360{
6361 skip_emulated_instruction(vcpu);
6362 WARN(1, "this should never happen\n");
6363 return 1;
6364}
6365
Avi Kivity851ba692009-08-24 11:10:17 +03006366static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006367{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006368 if (likely(fasteoi)) {
6369 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6370 int access_type, offset;
6371
6372 access_type = exit_qualification & APIC_ACCESS_TYPE;
6373 offset = exit_qualification & APIC_ACCESS_OFFSET;
6374 /*
6375 * Sane guest uses MOV to write EOI, with written value
6376 * not cared. So make a short-circuit here by avoiding
6377 * heavy instruction emulation.
6378 */
6379 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6380 (offset == APIC_EOI)) {
6381 kvm_lapic_set_eoi(vcpu);
6382 skip_emulated_instruction(vcpu);
6383 return 1;
6384 }
6385 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006386 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006387}
6388
Yang Zhangc7c9c562013-01-25 10:18:51 +08006389static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6390{
6391 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6392 int vector = exit_qualification & 0xff;
6393
6394 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6395 kvm_apic_set_eoi_accelerated(vcpu, vector);
6396 return 1;
6397}
6398
Yang Zhang83d4c282013-01-25 10:18:49 +08006399static int handle_apic_write(struct kvm_vcpu *vcpu)
6400{
6401 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6402 u32 offset = exit_qualification & 0xfff;
6403
6404 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6405 kvm_apic_write_nodecode(vcpu, offset);
6406 return 1;
6407}
6408
Avi Kivity851ba692009-08-24 11:10:17 +03006409static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006410{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006411 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006412 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006413 bool has_error_code = false;
6414 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006415 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006416 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006417
6418 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006419 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006420 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006421
6422 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6423
6424 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006425 if (reason == TASK_SWITCH_GATE && idt_v) {
6426 switch (type) {
6427 case INTR_TYPE_NMI_INTR:
6428 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006429 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006430 break;
6431 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006432 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006433 kvm_clear_interrupt_queue(vcpu);
6434 break;
6435 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006436 if (vmx->idt_vectoring_info &
6437 VECTORING_INFO_DELIVER_CODE_MASK) {
6438 has_error_code = true;
6439 error_code =
6440 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6441 }
6442 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006443 case INTR_TYPE_SOFT_EXCEPTION:
6444 kvm_clear_exception_queue(vcpu);
6445 break;
6446 default:
6447 break;
6448 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006449 }
Izik Eidus37817f22008-03-24 23:14:53 +02006450 tss_selector = exit_qualification;
6451
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006452 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6453 type != INTR_TYPE_EXT_INTR &&
6454 type != INTR_TYPE_NMI_INTR))
6455 skip_emulated_instruction(vcpu);
6456
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006457 if (kvm_task_switch(vcpu, tss_selector,
6458 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6459 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006460 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6461 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6462 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006463 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006464 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006465
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006466 /*
6467 * TODO: What about debug traps on tss switch?
6468 * Are we supposed to inject them and update dr6?
6469 */
6470
6471 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006472}
6473
Avi Kivity851ba692009-08-24 11:10:17 +03006474static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006475{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006476 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006477 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006478 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006479 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006480
Sheng Yangf9c617f2009-03-25 10:08:52 +08006481 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006482
Sheng Yang14394422008-04-28 12:24:45 +08006483 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006484 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006485 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6486 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6487 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006488 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006489 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6490 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006491 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6492 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006493 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006494 }
6495
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006496 /*
6497 * EPT violation happened while executing iret from NMI,
6498 * "blocked by NMI" bit has to be set before next VM entry.
6499 * There are errata that may cause this bit to not be set:
6500 * AAK134, BY25.
6501 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006502 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6503 cpu_has_virtual_nmis() &&
6504 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006505 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6506
Sheng Yang14394422008-04-28 12:24:45 +08006507 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006508 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006509
Bandan Dasd95c5562016-07-12 18:18:51 -04006510 /* it is a read fault? */
6511 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6512 /* it is a write fault? */
6513 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006514 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006515 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006516 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006517 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006518
Yang Zhang25d92082013-08-06 12:00:32 +03006519 vcpu->arch.exit_qualification = exit_qualification;
6520
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006521 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006522}
6523
Avi Kivity851ba692009-08-24 11:10:17 +03006524static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006525{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006526 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006527 gpa_t gpa;
6528
6529 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006530 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006531 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsov737dcb92018-01-25 16:37:07 +01006532 /*
6533 * Doing kvm_skip_emulated_instruction() depends on undefined
6534 * behavior: Intel's manual doesn't mandate
6535 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6536 * occurs and while on real hardware it was observed to be set,
6537 * other hypervisors (namely Hyper-V) don't set it, we end up
6538 * advancing IP with some random value. Disable fast mmio when
6539 * running nested and keep it for real hardware in hope that
6540 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6541 */
6542 if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) {
6543 skip_emulated_instruction(vcpu);
6544 return 1;
6545 }
6546 else
6547 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6548 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006549 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006550
Paolo Bonzini43a39a32017-08-17 18:36:56 +02006551 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6552 if (ret >= 0)
6553 return ret;
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006554
6555 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006556 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006557
Avi Kivity851ba692009-08-24 11:10:17 +03006558 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6559 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006560
6561 return 0;
6562}
6563
Avi Kivity851ba692009-08-24 11:10:17 +03006564static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006565{
6566 u32 cpu_based_vm_exec_control;
6567
6568 /* clear pending NMI */
6569 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6570 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6571 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6572 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006573 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006574
6575 return 1;
6576}
6577
Mohammed Gamal80ced182009-09-01 12:48:18 +02006578static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006579{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006580 struct vcpu_vmx *vmx = to_vmx(vcpu);
6581 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006582 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006583 u32 cpu_exec_ctrl;
6584 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006585 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006586
6587 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6588 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006589
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006590 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006591 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006592 return handle_interrupt_window(&vmx->vcpu);
6593
Avi Kivityde87dcd2012-06-12 20:21:38 +03006594 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6595 return 1;
6596
Liran Alon114de9b2017-11-05 16:56:34 +02006597 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006598
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006599 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006600 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006601 ret = 0;
6602 goto out;
6603 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006604
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006605 if (err != EMULATE_DONE)
6606 goto emulation_error;
6607
6608 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6609 vcpu->arch.exception.pending)
6610 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006611
Gleb Natapov8d76c492013-05-08 18:38:44 +03006612 if (vcpu->arch.halt_request) {
6613 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006614 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006615 goto out;
6616 }
6617
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006618 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006619 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006620 if (need_resched())
6621 schedule();
6622 }
6623
Mohammed Gamal80ced182009-09-01 12:48:18 +02006624out:
6625 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006626
6627emulation_error:
6628 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6629 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6630 vcpu->run->internal.ndata = 0;
6631 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006632}
6633
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006634static int __grow_ple_window(int val)
6635{
6636 if (ple_window_grow < 1)
6637 return ple_window;
6638
6639 val = min(val, ple_window_actual_max);
6640
6641 if (ple_window_grow < ple_window)
6642 val *= ple_window_grow;
6643 else
6644 val += ple_window_grow;
6645
6646 return val;
6647}
6648
6649static int __shrink_ple_window(int val, int modifier, int minimum)
6650{
6651 if (modifier < 1)
6652 return ple_window;
6653
6654 if (modifier < ple_window)
6655 val /= modifier;
6656 else
6657 val -= modifier;
6658
6659 return max(val, minimum);
6660}
6661
6662static void grow_ple_window(struct kvm_vcpu *vcpu)
6663{
6664 struct vcpu_vmx *vmx = to_vmx(vcpu);
6665 int old = vmx->ple_window;
6666
6667 vmx->ple_window = __grow_ple_window(old);
6668
6669 if (vmx->ple_window != old)
6670 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006671
6672 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006673}
6674
6675static void shrink_ple_window(struct kvm_vcpu *vcpu)
6676{
6677 struct vcpu_vmx *vmx = to_vmx(vcpu);
6678 int old = vmx->ple_window;
6679
6680 vmx->ple_window = __shrink_ple_window(old,
6681 ple_window_shrink, ple_window);
6682
6683 if (vmx->ple_window != old)
6684 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006685
6686 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006687}
6688
6689/*
6690 * ple_window_actual_max is computed to be one grow_ple_window() below
6691 * ple_window_max. (See __grow_ple_window for the reason.)
6692 * This prevents overflows, because ple_window_max is int.
6693 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6694 * this process.
6695 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6696 */
6697static void update_ple_window_actual_max(void)
6698{
6699 ple_window_actual_max =
6700 __shrink_ple_window(max(ple_window_max, ple_window),
6701 ple_window_grow, INT_MIN);
6702}
6703
Feng Wubf9f6ac2015-09-18 22:29:55 +08006704/*
6705 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6706 */
6707static void wakeup_handler(void)
6708{
6709 struct kvm_vcpu *vcpu;
6710 int cpu = smp_processor_id();
6711
6712 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6713 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6714 blocked_vcpu_list) {
6715 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6716
6717 if (pi_test_on(pi_desc) == 1)
6718 kvm_vcpu_kick(vcpu);
6719 }
6720 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6721}
6722
Tiejun Chenf2c76482014-10-28 10:14:47 +08006723static __init int hardware_setup(void)
6724{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006725 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006726
6727 rdmsrl_safe(MSR_EFER, &host_efer);
6728
6729 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6730 kvm_define_shared_msr(i, vmx_msr_index[i]);
6731
6732 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6733 if (!vmx_io_bitmap_a)
6734 return r;
6735
6736 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6737 if (!vmx_io_bitmap_b)
6738 goto out;
6739
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006740 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6741 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006742 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006743
6744 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6745 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006746 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006747
6748 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6749 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6750
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006751 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006752
6753 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6754
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006755 if (setup_vmcs_config(&vmcs_config) < 0) {
6756 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006757 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006758 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006759
6760 if (boot_cpu_has(X86_FEATURE_NX))
6761 kvm_enable_efer_bits(EFER_NX);
6762
Wanpeng Li2df19692017-03-23 05:30:08 -07006763 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6764 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006765 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006766
Tiejun Chenf2c76482014-10-28 10:14:47 +08006767 if (!cpu_has_vmx_shadow_vmcs())
6768 enable_shadow_vmcs = 0;
6769 if (enable_shadow_vmcs)
6770 init_vmcs_shadow_fields();
6771
6772 if (!cpu_has_vmx_ept() ||
6773 !cpu_has_vmx_ept_4levels()) {
6774 enable_ept = 0;
6775 enable_unrestricted_guest = 0;
6776 enable_ept_ad_bits = 0;
6777 }
6778
6779 if (!cpu_has_vmx_ept_ad_bits())
6780 enable_ept_ad_bits = 0;
6781
6782 if (!cpu_has_vmx_unrestricted_guest())
6783 enable_unrestricted_guest = 0;
6784
Paolo Bonziniad15a292015-01-30 16:18:49 +01006785 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006786 flexpriority_enabled = 0;
6787
Paolo Bonziniad15a292015-01-30 16:18:49 +01006788 /*
6789 * set_apic_access_page_addr() is used to reload apic access
6790 * page upon invalidation. No need to do anything if not
6791 * using the APIC_ACCESS_ADDR VMCS field.
6792 */
6793 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006794 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006795
6796 if (!cpu_has_vmx_tpr_shadow())
6797 kvm_x86_ops->update_cr8_intercept = NULL;
6798
6799 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6800 kvm_disable_largepages();
6801
6802 if (!cpu_has_vmx_ple())
6803 ple_gap = 0;
6804
6805 if (!cpu_has_vmx_apicv())
6806 enable_apicv = 0;
6807
Haozhong Zhang64903d62015-10-20 15:39:09 +08006808 if (cpu_has_vmx_tsc_scaling()) {
6809 kvm_has_tsc_control = true;
6810 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6811 kvm_tsc_scaling_ratio_frac_bits = 48;
6812 }
6813
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006814 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6815
Tiejun Chenbaa03522014-12-23 16:21:11 +08006816 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006817 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006818 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6819 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006820 0ull, VMX_EPT_EXECUTABLE_MASK,
6821 cpu_has_vmx_ept_execute_only() ?
6822 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006823 ept_set_mmio_spte_mask();
6824 kvm_enable_tdp();
6825 } else
6826 kvm_disable_tdp();
6827
6828 update_ple_window_actual_max();
6829
Kai Huang843e4332015-01-28 10:54:28 +08006830 /*
6831 * Only enable PML when hardware supports PML feature, and both EPT
6832 * and EPT A/D bit features are enabled -- PML depends on them to work.
6833 */
6834 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6835 enable_pml = 0;
6836
6837 if (!enable_pml) {
6838 kvm_x86_ops->slot_enable_log_dirty = NULL;
6839 kvm_x86_ops->slot_disable_log_dirty = NULL;
6840 kvm_x86_ops->flush_log_dirty = NULL;
6841 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6842 }
6843
Yunhong Jiang64672c92016-06-13 14:19:59 -07006844 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6845 u64 vmx_msr;
6846
6847 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6848 cpu_preemption_timer_multi =
6849 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6850 } else {
6851 kvm_x86_ops->set_hv_timer = NULL;
6852 kvm_x86_ops->cancel_hv_timer = NULL;
6853 }
6854
Feng Wubf9f6ac2015-09-18 22:29:55 +08006855 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6856
Ashok Rajc45dcc72016-06-22 14:59:56 +08006857 kvm_mce_cap_supported |= MCG_LMCE_P;
6858
Tiejun Chenf2c76482014-10-28 10:14:47 +08006859 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006860
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006861out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006862 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006863out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006864 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006865out1:
6866 free_page((unsigned long)vmx_io_bitmap_b);
6867out:
6868 free_page((unsigned long)vmx_io_bitmap_a);
6869
6870 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006871}
6872
6873static __exit void hardware_unsetup(void)
6874{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006875 free_page((unsigned long)vmx_io_bitmap_b);
6876 free_page((unsigned long)vmx_io_bitmap_a);
6877 free_page((unsigned long)vmx_vmwrite_bitmap);
6878 free_page((unsigned long)vmx_vmread_bitmap);
6879
Tiejun Chenf2c76482014-10-28 10:14:47 +08006880 free_kvm_area();
6881}
6882
Avi Kivity6aa8b732006-12-10 02:21:36 -08006883/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006884 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6885 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6886 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006887static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006888{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006889 if (ple_gap)
6890 grow_ple_window(vcpu);
6891
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006892 skip_emulated_instruction(vcpu);
6893 kvm_vcpu_on_spin(vcpu);
6894
6895 return 1;
6896}
6897
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006898static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006899{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006900 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006901 return 1;
6902}
6903
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006904static int handle_mwait(struct kvm_vcpu *vcpu)
6905{
6906 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6907 return handle_nop(vcpu);
6908}
6909
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006910static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6911{
6912 return 1;
6913}
6914
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006915static int handle_monitor(struct kvm_vcpu *vcpu)
6916{
6917 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6918 return handle_nop(vcpu);
6919}
6920
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006921/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006922 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6923 * set the success or error code of an emulated VMX instruction, as specified
6924 * by Vol 2B, VMX Instruction Reference, "Conventions".
6925 */
6926static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6927{
6928 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6929 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6930 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6931}
6932
6933static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6934{
6935 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6936 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6937 X86_EFLAGS_SF | X86_EFLAGS_OF))
6938 | X86_EFLAGS_CF);
6939}
6940
Abel Gordon145c28d2013-04-18 14:36:55 +03006941static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006942 u32 vm_instruction_error)
6943{
6944 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6945 /*
6946 * failValid writes the error number to the current VMCS, which
6947 * can't be done there isn't a current VMCS.
6948 */
6949 nested_vmx_failInvalid(vcpu);
6950 return;
6951 }
6952 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6953 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6954 X86_EFLAGS_SF | X86_EFLAGS_OF))
6955 | X86_EFLAGS_ZF);
6956 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6957 /*
6958 * We don't need to force a shadow sync because
6959 * VM_INSTRUCTION_ERROR is not shadowed
6960 */
6961}
Abel Gordon145c28d2013-04-18 14:36:55 +03006962
Wincy Vanff651cb2014-12-11 08:52:58 +03006963static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6964{
6965 /* TODO: not to reset guest simply here. */
6966 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006967 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006968}
6969
Jan Kiszkaf4124502014-03-07 20:03:13 +01006970static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6971{
6972 struct vcpu_vmx *vmx =
6973 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6974
6975 vmx->nested.preemption_timer_expired = true;
6976 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6977 kvm_vcpu_kick(&vmx->vcpu);
6978
6979 return HRTIMER_NORESTART;
6980}
6981
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006982/*
Bandan Das19677e32014-05-06 02:19:15 -04006983 * Decode the memory-address operand of a vmx instruction, as recorded on an
6984 * exit caused by such an instruction (run by a guest hypervisor).
6985 * On success, returns 0. When the operand is invalid, returns 1 and throws
6986 * #UD or #GP.
6987 */
6988static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6989 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006990 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006991{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006992 gva_t off;
6993 bool exn;
6994 struct kvm_segment s;
6995
Bandan Das19677e32014-05-06 02:19:15 -04006996 /*
6997 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6998 * Execution", on an exit, vmx_instruction_info holds most of the
6999 * addressing components of the operand. Only the displacement part
7000 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7001 * For how an actual address is calculated from all these components,
7002 * refer to Vol. 1, "Operand Addressing".
7003 */
7004 int scaling = vmx_instruction_info & 3;
7005 int addr_size = (vmx_instruction_info >> 7) & 7;
7006 bool is_reg = vmx_instruction_info & (1u << 10);
7007 int seg_reg = (vmx_instruction_info >> 15) & 7;
7008 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7009 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7010 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7011 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7012
7013 if (is_reg) {
7014 kvm_queue_exception(vcpu, UD_VECTOR);
7015 return 1;
7016 }
7017
7018 /* Addr = segment_base + offset */
7019 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007020 off = exit_qualification; /* holds the displacement */
Sean Christopherson97483542019-01-23 14:39:23 -08007021 if (addr_size == 1)
7022 off = (gva_t)sign_extend64(off, 31);
7023 else if (addr_size == 0)
7024 off = (gva_t)sign_extend64(off, 15);
Bandan Das19677e32014-05-06 02:19:15 -04007025 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007026 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007027 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007028 off += kvm_register_read(vcpu, index_reg)<<scaling;
7029 vmx_get_segment(vcpu, &s, seg_reg);
7030 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007031
7032 if (addr_size == 1) /* 32 bit */
7033 *ret &= 0xffffffff;
7034
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007035 /* Checks for #GP/#SS exceptions. */
7036 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007037 if (is_long_mode(vcpu)) {
7038 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7039 * non-canonical form. This is the only check on the memory
7040 * destination for long mode!
7041 */
7042 exn = is_noncanonical_address(*ret);
7043 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007044 /* Protected mode: apply checks for segment validity in the
7045 * following order:
7046 * - segment type check (#GP(0) may be thrown)
7047 * - usability check (#GP(0)/#SS(0))
7048 * - limit check (#GP(0)/#SS(0))
7049 */
7050 if (wr)
7051 /* #GP(0) if the destination operand is located in a
7052 * read-only data segment or any code segment.
7053 */
7054 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7055 else
7056 /* #GP(0) if the source operand is located in an
7057 * execute-only code segment
7058 */
7059 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007060 if (exn) {
7061 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7062 return 1;
7063 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007064 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7065 */
7066 exn = (s.unusable != 0);
Sean Christopherson7b3c6c42019-01-23 14:39:25 -08007067
7068 /*
7069 * Protected mode: #GP(0)/#SS(0) if the memory operand is
7070 * outside the segment limit. All CPUs that support VMX ignore
7071 * limit checks for flat segments, i.e. segments with base==0,
7072 * limit==0xffffffff and of type expand-up data or code.
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007073 */
Sean Christopherson7b3c6c42019-01-23 14:39:25 -08007074 if (!(s.base == 0 && s.limit == 0xffffffff &&
7075 ((s.type & 8) || !(s.type & 4))))
7076 exn = exn || (off + sizeof(u64) > s.limit);
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007077 }
7078 if (exn) {
7079 kvm_queue_exception_e(vcpu,
7080 seg_reg == VCPU_SREG_SS ?
7081 SS_VECTOR : GP_VECTOR,
7082 0);
7083 return 1;
7084 }
7085
Bandan Das19677e32014-05-06 02:19:15 -04007086 return 0;
7087}
7088
7089/*
Bandan Das3573e222014-05-06 02:19:16 -04007090 * This function performs the various checks including
7091 * - if it's 4KB aligned
7092 * - No bits beyond the physical address width are set
7093 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04007094 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04007095 */
Bandan Das4291b582014-05-06 02:19:18 -04007096static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
7097 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007098{
7099 gva_t gva;
7100 gpa_t vmptr;
7101 struct x86_exception e;
7102 struct page *page;
7103 struct vcpu_vmx *vmx = to_vmx(vcpu);
7104 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7105
7106 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007107 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007108 return 1;
7109
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007110 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007111 kvm_inject_page_fault(vcpu, &e);
7112 return 1;
7113 }
7114
7115 switch (exit_reason) {
7116 case EXIT_REASON_VMON:
7117 /*
7118 * SDM 3: 24.11.5
7119 * The first 4 bytes of VMXON region contain the supported
7120 * VMCS revision identifier
7121 *
7122 * Note - IA32_VMX_BASIC[48] will never be 1
7123 * for the nested case;
7124 * which replaces physical address width with 32
7125 *
7126 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007127 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007128 nested_vmx_failInvalid(vcpu);
7129 skip_emulated_instruction(vcpu);
7130 return 1;
7131 }
7132
7133 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007134 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007135 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007136 skip_emulated_instruction(vcpu);
7137 return 1;
7138 }
7139 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007140 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007141 nested_release_page_clean(page);
7142 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007143 skip_emulated_instruction(vcpu);
7144 return 1;
7145 }
7146 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007147 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007148 vmx->nested.vmxon_ptr = vmptr;
7149 break;
Bandan Das4291b582014-05-06 02:19:18 -04007150 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007151 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007152 nested_vmx_failValid(vcpu,
7153 VMXERR_VMCLEAR_INVALID_ADDRESS);
7154 skip_emulated_instruction(vcpu);
7155 return 1;
7156 }
Bandan Das3573e222014-05-06 02:19:16 -04007157
Bandan Das4291b582014-05-06 02:19:18 -04007158 if (vmptr == vmx->nested.vmxon_ptr) {
7159 nested_vmx_failValid(vcpu,
7160 VMXERR_VMCLEAR_VMXON_POINTER);
7161 skip_emulated_instruction(vcpu);
7162 return 1;
7163 }
7164 break;
7165 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007166 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007167 nested_vmx_failValid(vcpu,
7168 VMXERR_VMPTRLD_INVALID_ADDRESS);
7169 skip_emulated_instruction(vcpu);
7170 return 1;
7171 }
7172
7173 if (vmptr == vmx->nested.vmxon_ptr) {
7174 nested_vmx_failValid(vcpu,
7175 VMXERR_VMCLEAR_VMXON_POINTER);
7176 skip_emulated_instruction(vcpu);
7177 return 1;
7178 }
7179 break;
Bandan Das3573e222014-05-06 02:19:16 -04007180 default:
7181 return 1; /* shouldn't happen */
7182 }
7183
Bandan Das4291b582014-05-06 02:19:18 -04007184 if (vmpointer)
7185 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007186 return 0;
7187}
7188
7189/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007190 * Emulate the VMXON instruction.
7191 * Currently, we just remember that VMX is active, and do not save or even
7192 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7193 * do not currently need to store anything in that guest-allocated memory
7194 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7195 * argument is different from the VMXON pointer (which the spec says they do).
7196 */
7197static int handle_vmon(struct kvm_vcpu *vcpu)
7198{
7199 struct kvm_segment cs;
7200 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007201 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007202 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7203 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007204 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007205
7206 /* The Intel VMX Instruction Reference lists a bunch of bits that
7207 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7208 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7209 * Otherwise, we should fail with #UD. We test these now:
7210 */
7211 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7212 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7213 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7214 kvm_queue_exception(vcpu, UD_VECTOR);
7215 return 1;
7216 }
7217
7218 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7219 if (is_long_mode(vcpu) && !cs.l) {
7220 kvm_queue_exception(vcpu, UD_VECTOR);
7221 return 1;
7222 }
7223
7224 if (vmx_get_cpl(vcpu)) {
7225 kvm_inject_gp(vcpu, 0);
7226 return 1;
7227 }
Bandan Das3573e222014-05-06 02:19:16 -04007228
Bandan Das4291b582014-05-06 02:19:18 -04007229 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007230 return 1;
7231
Abel Gordon145c28d2013-04-18 14:36:55 +03007232 if (vmx->nested.vmxon) {
7233 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7234 skip_emulated_instruction(vcpu);
7235 return 1;
7236 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007237
Haozhong Zhang3b840802016-06-22 14:59:54 +08007238 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007239 != VMXON_NEEDED_FEATURES) {
7240 kvm_inject_gp(vcpu, 0);
7241 return 1;
7242 }
7243
Paolo Bonziniff546f92018-01-11 12:16:15 +01007244 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7245 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007246 goto out_vmcs02;
Jim Mattson46e24df2017-11-27 17:22:25 -06007247
David Matlack4f2777b2016-07-13 17:16:37 -07007248 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7249 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007250 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007251
Abel Gordon8de48832013-04-18 14:37:25 +03007252 if (enable_shadow_vmcs) {
7253 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007254 if (!shadow_vmcs)
7255 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007256 /* mark vmcs as shadow */
7257 shadow_vmcs->revision_id |= (1u << 31);
7258 /* init shadow vmcs */
7259 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007260 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007261 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007262
Jan Kiszkaf4124502014-03-07 20:03:13 +01007263 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007264 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007265 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7266
Roman Kagan020a90f2018-07-19 21:59:07 +03007267 vmx->nested.vpid02 = allocate_vpid();
7268
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007269 vmx->nested.vmxon = true;
7270
7271 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007272 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007273 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007274
7275out_shadow_vmcs:
7276 kfree(vmx->nested.cached_vmcs12);
7277
7278out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007279 free_loaded_vmcs(&vmx->nested.vmcs02);
7280
7281out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007282 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007283}
7284
7285/*
7286 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7287 * for running VMX instructions (except VMXON, whose prerequisites are
7288 * slightly different). It also specifies what exception to inject otherwise.
7289 */
7290static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7291{
7292 struct kvm_segment cs;
7293 struct vcpu_vmx *vmx = to_vmx(vcpu);
7294
7295 if (!vmx->nested.vmxon) {
7296 kvm_queue_exception(vcpu, UD_VECTOR);
7297 return 0;
7298 }
7299
7300 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7301 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7302 (is_long_mode(vcpu) && !cs.l)) {
7303 kvm_queue_exception(vcpu, UD_VECTOR);
7304 return 0;
7305 }
7306
7307 if (vmx_get_cpl(vcpu)) {
7308 kvm_inject_gp(vcpu, 0);
7309 return 0;
7310 }
7311
7312 return 1;
7313}
7314
Abel Gordone7953d72013-04-18 14:37:55 +03007315static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7316{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007317 if (vmx->nested.current_vmptr == -1ull)
7318 return;
7319
7320 /* current_vmptr and current_vmcs12 are always set/reset together */
7321 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7322 return;
7323
Abel Gordon012f83c2013-04-18 14:39:25 +03007324 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007325 /* copy to memory all shadowed fields in case
7326 they were modified */
7327 copy_shadow_to_vmcs12(vmx);
7328 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007329 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7330 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007331 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007332 }
Wincy Van705699a2015-02-03 23:58:17 +08007333 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007334
7335 /* Flush VMCS12 to guest memory */
7336 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7337 VMCS12_SIZE);
7338
Abel Gordone7953d72013-04-18 14:37:55 +03007339 kunmap(vmx->nested.current_vmcs12_page);
7340 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007341 vmx->nested.current_vmptr = -1ull;
7342 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007343}
7344
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007345/*
7346 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7347 * just stops using VMX.
7348 */
7349static void free_nested(struct vcpu_vmx *vmx)
7350{
7351 if (!vmx->nested.vmxon)
7352 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007353
Peter Shiera2c34d22018-10-11 11:46:46 -07007354 hrtimer_cancel(&vmx->nested.preemption_timer);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007355 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007356 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007357 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007358 if (enable_shadow_vmcs) {
7359 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7360 free_vmcs(vmx->vmcs01.shadow_vmcs);
7361 vmx->vmcs01.shadow_vmcs = NULL;
7362 }
David Matlack4f2777b2016-07-13 17:16:37 -07007363 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007364 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007365 if (vmx->nested.apic_access_page) {
7366 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007367 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007368 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007369 if (vmx->nested.virtual_apic_page) {
7370 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007371 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007372 }
Wincy Van705699a2015-02-03 23:58:17 +08007373 if (vmx->nested.pi_desc_page) {
7374 kunmap(vmx->nested.pi_desc_page);
7375 nested_release_page(vmx->nested.pi_desc_page);
7376 vmx->nested.pi_desc_page = NULL;
7377 vmx->nested.pi_desc = NULL;
7378 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007379
Jim Mattson46e24df2017-11-27 17:22:25 -06007380 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007381}
7382
7383/* Emulate the VMXOFF instruction */
7384static int handle_vmoff(struct kvm_vcpu *vcpu)
7385{
7386 if (!nested_vmx_check_permission(vcpu))
7387 return 1;
7388 free_nested(to_vmx(vcpu));
7389 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007390 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007391 return 1;
7392}
7393
Nadav Har'El27d6c862011-05-25 23:06:59 +03007394/* Emulate the VMCLEAR instruction */
7395static int handle_vmclear(struct kvm_vcpu *vcpu)
7396{
7397 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007398 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007399 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007400
7401 if (!nested_vmx_check_permission(vcpu))
7402 return 1;
7403
Bandan Das4291b582014-05-06 02:19:18 -04007404 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007405 return 1;
7406
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007407 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007408 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007409
Jim Mattson29deec42017-03-02 12:41:48 -08007410 kvm_vcpu_write_guest(vcpu,
7411 vmptr + offsetof(struct vmcs12, launch_state),
7412 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007413
Nadav Har'El27d6c862011-05-25 23:06:59 +03007414 skip_emulated_instruction(vcpu);
7415 nested_vmx_succeed(vcpu);
7416 return 1;
7417}
7418
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007419static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7420
7421/* Emulate the VMLAUNCH instruction */
7422static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7423{
7424 return nested_vmx_run(vcpu, true);
7425}
7426
7427/* Emulate the VMRESUME instruction */
7428static int handle_vmresume(struct kvm_vcpu *vcpu)
7429{
7430
7431 return nested_vmx_run(vcpu, false);
7432}
7433
Nadav Har'El49f705c2011-05-25 23:08:30 +03007434enum vmcs_field_type {
7435 VMCS_FIELD_TYPE_U16 = 0,
7436 VMCS_FIELD_TYPE_U64 = 1,
7437 VMCS_FIELD_TYPE_U32 = 2,
7438 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7439};
7440
7441static inline int vmcs_field_type(unsigned long field)
7442{
7443 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7444 return VMCS_FIELD_TYPE_U32;
7445 return (field >> 13) & 0x3 ;
7446}
7447
7448static inline int vmcs_field_readonly(unsigned long field)
7449{
7450 return (((field >> 10) & 0x3) == 1);
7451}
7452
7453/*
7454 * Read a vmcs12 field. Since these can have varying lengths and we return
7455 * one type, we chose the biggest type (u64) and zero-extend the return value
7456 * to that size. Note that the caller, handle_vmread, might need to use only
7457 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7458 * 64-bit fields are to be returned).
7459 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007460static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7461 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007462{
7463 short offset = vmcs_field_to_offset(field);
7464 char *p;
7465
7466 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007467 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007468
7469 p = ((char *)(get_vmcs12(vcpu))) + offset;
7470
7471 switch (vmcs_field_type(field)) {
7472 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7473 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007474 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007475 case VMCS_FIELD_TYPE_U16:
7476 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007477 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007478 case VMCS_FIELD_TYPE_U32:
7479 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007480 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007481 case VMCS_FIELD_TYPE_U64:
7482 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007483 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007484 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007485 WARN_ON(1);
7486 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007487 }
7488}
7489
Abel Gordon20b97fe2013-04-18 14:36:25 +03007490
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007491static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7492 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007493 short offset = vmcs_field_to_offset(field);
7494 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7495 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007496 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007497
7498 switch (vmcs_field_type(field)) {
7499 case VMCS_FIELD_TYPE_U16:
7500 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007501 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007502 case VMCS_FIELD_TYPE_U32:
7503 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007504 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007505 case VMCS_FIELD_TYPE_U64:
7506 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007507 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007508 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7509 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007510 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007511 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007512 WARN_ON(1);
7513 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007514 }
7515
7516}
7517
Abel Gordon16f5b902013-04-18 14:38:25 +03007518static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7519{
7520 int i;
7521 unsigned long field;
7522 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007523 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007524 const unsigned long *fields = shadow_read_write_fields;
7525 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007526
Jan Kiszka282da872014-10-08 18:05:39 +02007527 preempt_disable();
7528
Abel Gordon16f5b902013-04-18 14:38:25 +03007529 vmcs_load(shadow_vmcs);
7530
7531 for (i = 0; i < num_fields; i++) {
7532 field = fields[i];
7533 switch (vmcs_field_type(field)) {
7534 case VMCS_FIELD_TYPE_U16:
7535 field_value = vmcs_read16(field);
7536 break;
7537 case VMCS_FIELD_TYPE_U32:
7538 field_value = vmcs_read32(field);
7539 break;
7540 case VMCS_FIELD_TYPE_U64:
7541 field_value = vmcs_read64(field);
7542 break;
7543 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7544 field_value = vmcs_readl(field);
7545 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007546 default:
7547 WARN_ON(1);
7548 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007549 }
7550 vmcs12_write_any(&vmx->vcpu, field, field_value);
7551 }
7552
7553 vmcs_clear(shadow_vmcs);
7554 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007555
7556 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007557}
7558
Abel Gordonc3114422013-04-18 14:38:55 +03007559static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7560{
Mathias Krausec2bae892013-06-26 20:36:21 +02007561 const unsigned long *fields[] = {
7562 shadow_read_write_fields,
7563 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007564 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007565 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007566 max_shadow_read_write_fields,
7567 max_shadow_read_only_fields
7568 };
7569 int i, q;
7570 unsigned long field;
7571 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007572 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007573
7574 vmcs_load(shadow_vmcs);
7575
Mathias Krausec2bae892013-06-26 20:36:21 +02007576 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007577 for (i = 0; i < max_fields[q]; i++) {
7578 field = fields[q][i];
7579 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7580
7581 switch (vmcs_field_type(field)) {
7582 case VMCS_FIELD_TYPE_U16:
7583 vmcs_write16(field, (u16)field_value);
7584 break;
7585 case VMCS_FIELD_TYPE_U32:
7586 vmcs_write32(field, (u32)field_value);
7587 break;
7588 case VMCS_FIELD_TYPE_U64:
7589 vmcs_write64(field, (u64)field_value);
7590 break;
7591 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7592 vmcs_writel(field, (long)field_value);
7593 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007594 default:
7595 WARN_ON(1);
7596 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007597 }
7598 }
7599 }
7600
7601 vmcs_clear(shadow_vmcs);
7602 vmcs_load(vmx->loaded_vmcs->vmcs);
7603}
7604
Nadav Har'El49f705c2011-05-25 23:08:30 +03007605/*
7606 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7607 * used before) all generate the same failure when it is missing.
7608 */
7609static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7610{
7611 struct vcpu_vmx *vmx = to_vmx(vcpu);
7612 if (vmx->nested.current_vmptr == -1ull) {
7613 nested_vmx_failInvalid(vcpu);
7614 skip_emulated_instruction(vcpu);
7615 return 0;
7616 }
7617 return 1;
7618}
7619
7620static int handle_vmread(struct kvm_vcpu *vcpu)
7621{
7622 unsigned long field;
7623 u64 field_value;
7624 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7625 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7626 gva_t gva = 0;
Paolo Bonzinidfa37272019-09-14 00:26:27 +02007627 struct x86_exception e;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007628
7629 if (!nested_vmx_check_permission(vcpu) ||
7630 !nested_vmx_check_vmcs12(vcpu))
7631 return 1;
7632
7633 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007634 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007635 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007636 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007637 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7638 skip_emulated_instruction(vcpu);
7639 return 1;
7640 }
7641 /*
7642 * Now copy part of this value to register or memory, as requested.
7643 * Note that the number of bits actually copied is 32 or 64 depending
7644 * on the guest's mode (32 or 64 bit), not on the given field's length.
7645 */
7646 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007647 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007648 field_value);
7649 } else {
7650 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007651 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007652 return 1;
7653 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzinidfa37272019-09-14 00:26:27 +02007654 if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
7655 (is_long_mode(vcpu) ? 8 : 4),
Jack Wangdb074ca2019-10-07 14:36:53 +02007656 &e))
Paolo Bonzinidfa37272019-09-14 00:26:27 +02007657 kvm_inject_page_fault(vcpu, &e);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007658 }
7659
7660 nested_vmx_succeed(vcpu);
7661 skip_emulated_instruction(vcpu);
7662 return 1;
7663}
7664
7665
7666static int handle_vmwrite(struct kvm_vcpu *vcpu)
7667{
7668 unsigned long field;
7669 gva_t gva;
7670 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7671 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007672 /* The value to write might be 32 or 64 bits, depending on L1's long
7673 * mode, and eventually we need to write that into a field of several
7674 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007675 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007676 * bits into the vmcs12 field.
7677 */
7678 u64 field_value = 0;
7679 struct x86_exception e;
7680
7681 if (!nested_vmx_check_permission(vcpu) ||
7682 !nested_vmx_check_vmcs12(vcpu))
7683 return 1;
7684
7685 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007686 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007687 (((vmx_instruction_info) >> 3) & 0xf));
7688 else {
7689 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007690 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007691 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007692 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7693 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007694 kvm_inject_page_fault(vcpu, &e);
7695 return 1;
7696 }
7697 }
7698
7699
Nadav Amit27e6fb52014-06-18 17:19:26 +03007700 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007701 if (vmcs_field_readonly(field)) {
7702 nested_vmx_failValid(vcpu,
7703 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7704 skip_emulated_instruction(vcpu);
7705 return 1;
7706 }
7707
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007708 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007709 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7710 skip_emulated_instruction(vcpu);
7711 return 1;
7712 }
7713
7714 nested_vmx_succeed(vcpu);
7715 skip_emulated_instruction(vcpu);
7716 return 1;
7717}
7718
Nadav Har'El63846662011-05-25 23:07:29 +03007719/* Emulate the VMPTRLD instruction */
7720static int handle_vmptrld(struct kvm_vcpu *vcpu)
7721{
7722 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007723 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007724
7725 if (!nested_vmx_check_permission(vcpu))
7726 return 1;
7727
Bandan Das4291b582014-05-06 02:19:18 -04007728 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007729 return 1;
7730
Nadav Har'El63846662011-05-25 23:07:29 +03007731 if (vmx->nested.current_vmptr != vmptr) {
7732 struct vmcs12 *new_vmcs12;
7733 struct page *page;
7734 page = nested_get_page(vcpu, vmptr);
7735 if (page == NULL) {
7736 nested_vmx_failInvalid(vcpu);
7737 skip_emulated_instruction(vcpu);
7738 return 1;
7739 }
7740 new_vmcs12 = kmap(page);
7741 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7742 kunmap(page);
7743 nested_release_page_clean(page);
7744 nested_vmx_failValid(vcpu,
7745 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7746 skip_emulated_instruction(vcpu);
7747 return 1;
7748 }
Nadav Har'El63846662011-05-25 23:07:29 +03007749
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007750 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007751 vmx->nested.current_vmptr = vmptr;
7752 vmx->nested.current_vmcs12 = new_vmcs12;
7753 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007754 /*
7755 * Load VMCS12 from guest memory since it is not already
7756 * cached.
7757 */
7758 memcpy(vmx->nested.cached_vmcs12,
7759 vmx->nested.current_vmcs12, VMCS12_SIZE);
7760
Abel Gordon012f83c2013-04-18 14:39:25 +03007761 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007762 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7763 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007764 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007765 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007766 vmx->nested.sync_shadow_vmcs = true;
7767 }
Nadav Har'El63846662011-05-25 23:07:29 +03007768 }
7769
7770 nested_vmx_succeed(vcpu);
7771 skip_emulated_instruction(vcpu);
7772 return 1;
7773}
7774
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007775/* Emulate the VMPTRST instruction */
7776static int handle_vmptrst(struct kvm_vcpu *vcpu)
7777{
7778 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7779 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7780 gva_t vmcs_gva;
7781 struct x86_exception e;
7782
7783 if (!nested_vmx_check_permission(vcpu))
7784 return 1;
7785
7786 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007787 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007788 return 1;
7789 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007790 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7791 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7792 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007793 kvm_inject_page_fault(vcpu, &e);
7794 return 1;
7795 }
7796 nested_vmx_succeed(vcpu);
7797 skip_emulated_instruction(vcpu);
7798 return 1;
7799}
7800
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007801/* Emulate the INVEPT instruction */
7802static int handle_invept(struct kvm_vcpu *vcpu)
7803{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007804 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007805 u32 vmx_instruction_info, types;
7806 unsigned long type;
7807 gva_t gva;
7808 struct x86_exception e;
7809 struct {
7810 u64 eptp, gpa;
7811 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007812
Wincy Vanb9c237b2015-02-03 23:56:30 +08007813 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7814 SECONDARY_EXEC_ENABLE_EPT) ||
7815 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007816 kvm_queue_exception(vcpu, UD_VECTOR);
7817 return 1;
7818 }
7819
7820 if (!nested_vmx_check_permission(vcpu))
7821 return 1;
7822
7823 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7824 kvm_queue_exception(vcpu, UD_VECTOR);
7825 return 1;
7826 }
7827
7828 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007829 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007830
Wincy Vanb9c237b2015-02-03 23:56:30 +08007831 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007832
Jim Mattson85c856b2016-10-26 08:38:38 -07007833 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007834 nested_vmx_failValid(vcpu,
7835 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007836 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007837 return 1;
7838 }
7839
7840 /* According to the Intel VMX instruction reference, the memory
7841 * operand is read even if it isn't needed (e.g., for type==global)
7842 */
7843 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007844 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007845 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007846 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007847 kvm_inject_page_fault(vcpu, &e);
7848 return 1;
7849 }
7850
7851 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007852 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007853 /*
7854 * TODO: track mappings and invalidate
7855 * single context requests appropriately
7856 */
7857 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007858 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007859 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007860 nested_vmx_succeed(vcpu);
7861 break;
7862 default:
7863 BUG_ON(1);
7864 break;
7865 }
7866
7867 skip_emulated_instruction(vcpu);
7868 return 1;
7869}
7870
Petr Matouseka642fc32014-09-23 20:22:30 +02007871static int handle_invvpid(struct kvm_vcpu *vcpu)
7872{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007873 struct vcpu_vmx *vmx = to_vmx(vcpu);
7874 u32 vmx_instruction_info;
7875 unsigned long type, types;
7876 gva_t gva;
7877 struct x86_exception e;
7878 int vpid;
7879
7880 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7881 SECONDARY_EXEC_ENABLE_VPID) ||
7882 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7883 kvm_queue_exception(vcpu, UD_VECTOR);
7884 return 1;
7885 }
7886
7887 if (!nested_vmx_check_permission(vcpu))
7888 return 1;
7889
7890 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7891 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7892
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007893 types = (vmx->nested.nested_vmx_vpid_caps &
7894 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007895
Jim Mattson85c856b2016-10-26 08:38:38 -07007896 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007897 nested_vmx_failValid(vcpu,
7898 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007899 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007900 return 1;
7901 }
7902
7903 /* according to the intel vmx instruction reference, the memory
7904 * operand is read even if it isn't needed (e.g., for type==global)
7905 */
7906 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7907 vmx_instruction_info, false, &gva))
7908 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007909 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007910 kvm_inject_page_fault(vcpu, &e);
7911 return 1;
7912 }
7913
7914 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007915 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007916 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007917 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7918 if (!vpid) {
7919 nested_vmx_failValid(vcpu,
7920 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7921 skip_emulated_instruction(vcpu);
7922 return 1;
7923 }
7924 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007925 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007926 break;
7927 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007928 WARN_ON_ONCE(1);
7929 skip_emulated_instruction(vcpu);
7930 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007931 }
7932
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007933 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7934 nested_vmx_succeed(vcpu);
7935
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007936 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007937 return 1;
7938}
7939
Kai Huang843e4332015-01-28 10:54:28 +08007940static int handle_pml_full(struct kvm_vcpu *vcpu)
7941{
7942 unsigned long exit_qualification;
7943
7944 trace_kvm_pml_full(vcpu->vcpu_id);
7945
7946 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7947
7948 /*
7949 * PML buffer FULL happened while executing iret from NMI,
7950 * "blocked by NMI" bit has to be set before next VM entry.
7951 */
7952 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7953 cpu_has_virtual_nmis() &&
7954 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7955 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7956 GUEST_INTR_STATE_NMI);
7957
7958 /*
7959 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7960 * here.., and there's no userspace involvement needed for PML.
7961 */
7962 return 1;
7963}
7964
Yunhong Jiang64672c92016-06-13 14:19:59 -07007965static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7966{
7967 kvm_lapic_expired_hv_timer(vcpu);
7968 return 1;
7969}
7970
Nadav Har'El0140cae2011-05-25 23:06:28 +03007971/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007972 * The exit handlers return 1 if the exit was handled fully and guest execution
7973 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7974 * to be done to userspace and return 0.
7975 */
Mathias Krause772e0312012-08-30 01:30:19 +02007976static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007977 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7978 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007979 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007980 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007981 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007982 [EXIT_REASON_CR_ACCESS] = handle_cr,
7983 [EXIT_REASON_DR_ACCESS] = handle_dr,
7984 [EXIT_REASON_CPUID] = handle_cpuid,
7985 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7986 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7987 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7988 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007989 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007990 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007991 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007992 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007993 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007994 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007995 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007996 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007997 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007998 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007999 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008000 [EXIT_REASON_VMOFF] = handle_vmoff,
8001 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008002 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8003 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008004 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008005 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008006 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008007 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008008 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008009 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008010 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8011 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008012 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008013 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008014 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008015 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008016 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008017 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008018 [EXIT_REASON_XSAVES] = handle_xsaves,
8019 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008020 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008021 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008022};
8023
8024static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008025 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008026
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008027static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8028 struct vmcs12 *vmcs12)
8029{
8030 unsigned long exit_qualification;
8031 gpa_t bitmap, last_bitmap;
8032 unsigned int port;
8033 int size;
8034 u8 b;
8035
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008036 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008037 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008038
8039 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8040
8041 port = exit_qualification >> 16;
8042 size = (exit_qualification & 7) + 1;
8043
8044 last_bitmap = (gpa_t)-1;
8045 b = -1;
8046
8047 while (size > 0) {
8048 if (port < 0x8000)
8049 bitmap = vmcs12->io_bitmap_a;
8050 else if (port < 0x10000)
8051 bitmap = vmcs12->io_bitmap_b;
8052 else
Joe Perches1d804d02015-03-30 16:46:09 -07008053 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008054 bitmap += (port & 0x7fff) / 8;
8055
8056 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008057 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008058 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008059 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008060 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008061
8062 port++;
8063 size--;
8064 last_bitmap = bitmap;
8065 }
8066
Joe Perches1d804d02015-03-30 16:46:09 -07008067 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008068}
8069
Nadav Har'El644d7112011-05-25 23:12:35 +03008070/*
8071 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8072 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8073 * disinterest in the current event (read or write a specific MSR) by using an
8074 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8075 */
8076static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8077 struct vmcs12 *vmcs12, u32 exit_reason)
8078{
8079 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8080 gpa_t bitmap;
8081
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008082 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008083 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008084
8085 /*
8086 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8087 * for the four combinations of read/write and low/high MSR numbers.
8088 * First we need to figure out which of the four to use:
8089 */
8090 bitmap = vmcs12->msr_bitmap;
8091 if (exit_reason == EXIT_REASON_MSR_WRITE)
8092 bitmap += 2048;
8093 if (msr_index >= 0xc0000000) {
8094 msr_index -= 0xc0000000;
8095 bitmap += 1024;
8096 }
8097
8098 /* Then read the msr_index'th bit from this bitmap: */
8099 if (msr_index < 1024*8) {
8100 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008101 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008102 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008103 return 1 & (b >> (msr_index & 7));
8104 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008105 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008106}
8107
8108/*
8109 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8110 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8111 * intercept (via guest_host_mask etc.) the current event.
8112 */
8113static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8114 struct vmcs12 *vmcs12)
8115{
8116 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8117 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008118 int reg;
8119 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008120
8121 switch ((exit_qualification >> 4) & 3) {
8122 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008123 reg = (exit_qualification >> 8) & 15;
8124 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008125 switch (cr) {
8126 case 0:
8127 if (vmcs12->cr0_guest_host_mask &
8128 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008129 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008130 break;
8131 case 3:
8132 if ((vmcs12->cr3_target_count >= 1 &&
8133 vmcs12->cr3_target_value0 == val) ||
8134 (vmcs12->cr3_target_count >= 2 &&
8135 vmcs12->cr3_target_value1 == val) ||
8136 (vmcs12->cr3_target_count >= 3 &&
8137 vmcs12->cr3_target_value2 == val) ||
8138 (vmcs12->cr3_target_count >= 4 &&
8139 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008140 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008141 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008142 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008143 break;
8144 case 4:
8145 if (vmcs12->cr4_guest_host_mask &
8146 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008147 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008148 break;
8149 case 8:
8150 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008151 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008152 break;
8153 }
8154 break;
8155 case 2: /* clts */
8156 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8157 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008158 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008159 break;
8160 case 1: /* mov from cr */
8161 switch (cr) {
8162 case 3:
8163 if (vmcs12->cpu_based_vm_exec_control &
8164 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008165 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008166 break;
8167 case 8:
8168 if (vmcs12->cpu_based_vm_exec_control &
8169 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008170 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008171 break;
8172 }
8173 break;
8174 case 3: /* lmsw */
8175 /*
8176 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8177 * cr0. Other attempted changes are ignored, with no exit.
8178 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008179 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008180 if (vmcs12->cr0_guest_host_mask & 0xe &
8181 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008182 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008183 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8184 !(vmcs12->cr0_read_shadow & 0x1) &&
8185 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008186 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008187 break;
8188 }
Joe Perches1d804d02015-03-30 16:46:09 -07008189 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008190}
8191
8192/*
8193 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8194 * should handle it ourselves in L0 (and then continue L2). Only call this
8195 * when in is_guest_mode (L2).
8196 */
8197static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8198{
Nadav Har'El644d7112011-05-25 23:12:35 +03008199 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8200 struct vcpu_vmx *vmx = to_vmx(vcpu);
8201 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008202 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008203
Jan Kiszka542060e2014-01-04 18:47:21 +01008204 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8205 vmcs_readl(EXIT_QUALIFICATION),
8206 vmx->idt_vectoring_info,
8207 intr_info,
8208 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8209 KVM_ISA_VMX);
8210
David Matlackb7649e12017-08-01 14:00:40 -07008211 /*
8212 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008213 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8214 * Page). The CPU may write to these pages via their host
8215 * physical address while L2 is running, bypassing any
8216 * address-translation-based dirty tracking (e.g. EPT write
8217 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008218 *
8219 * Mark them dirty on every exit from L2 to prevent them from
8220 * getting out of sync with dirty tracking.
8221 */
8222 nested_mark_vmcs12_pages_dirty(vcpu);
8223
Nadav Har'El644d7112011-05-25 23:12:35 +03008224 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008225 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008226
8227 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008228 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8229 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008230 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008231 }
8232
8233 switch (exit_reason) {
8234 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008235 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008236 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008237 else if (is_page_fault(intr_info))
8238 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008239 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008240 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008241 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008242 else if (is_debug(intr_info) &&
8243 vcpu->guest_debug &
8244 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8245 return false;
8246 else if (is_breakpoint(intr_info) &&
8247 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8248 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008249 return vmcs12->exception_bitmap &
8250 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8251 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008252 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008253 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008254 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008255 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008256 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008257 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008258 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008259 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008260 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008261 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008262 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008263 case EXIT_REASON_HLT:
8264 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8265 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008266 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008267 case EXIT_REASON_INVLPG:
8268 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8269 case EXIT_REASON_RDPMC:
8270 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008271 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008272 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8273 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8274 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8275 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8276 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8277 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008278 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008279 /*
8280 * VMX instructions trap unconditionally. This allows L1 to
8281 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8282 */
Joe Perches1d804d02015-03-30 16:46:09 -07008283 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008284 case EXIT_REASON_CR_ACCESS:
8285 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8286 case EXIT_REASON_DR_ACCESS:
8287 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8288 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008289 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008290 case EXIT_REASON_MSR_READ:
8291 case EXIT_REASON_MSR_WRITE:
8292 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8293 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008294 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008295 case EXIT_REASON_MWAIT_INSTRUCTION:
8296 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008297 case EXIT_REASON_MONITOR_TRAP_FLAG:
8298 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008299 case EXIT_REASON_MONITOR_INSTRUCTION:
8300 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8301 case EXIT_REASON_PAUSE_INSTRUCTION:
8302 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8303 nested_cpu_has2(vmcs12,
8304 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8305 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008306 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008307 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008308 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008309 case EXIT_REASON_APIC_ACCESS:
8310 return nested_cpu_has2(vmcs12,
8311 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008312 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008313 case EXIT_REASON_EOI_INDUCED:
8314 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008315 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008316 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008317 /*
8318 * L0 always deals with the EPT violation. If nested EPT is
8319 * used, and the nested mmu code discovers that the address is
8320 * missing in the guest EPT table (EPT12), the EPT violation
8321 * will be injected with nested_ept_inject_page_fault()
8322 */
Joe Perches1d804d02015-03-30 16:46:09 -07008323 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008324 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008325 /*
8326 * L2 never uses directly L1's EPT, but rather L0's own EPT
8327 * table (shadow on EPT) or a merged EPT table that L0 built
8328 * (EPT on EPT). So any problems with the structure of the
8329 * table is L0's fault.
8330 */
Joe Perches1d804d02015-03-30 16:46:09 -07008331 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008332 case EXIT_REASON_WBINVD:
8333 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8334 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008335 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008336 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8337 /*
8338 * This should never happen, since it is not possible to
8339 * set XSS to a non-zero value---neither in L1 nor in L2.
8340 * If if it were, XSS would have to be checked against
8341 * the XSS exit bitmap in vmcs12.
8342 */
8343 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008344 case EXIT_REASON_PREEMPTION_TIMER:
8345 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008346 case EXIT_REASON_PML_FULL:
8347 /* We don't expose PML support to L1. */
8348 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008349 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008350 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008351 }
8352}
8353
Avi Kivity586f9602010-11-18 13:09:54 +02008354static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8355{
8356 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8357 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8358}
8359
Kai Huanga3eaa862015-11-04 13:46:05 +08008360static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008361{
Kai Huanga3eaa862015-11-04 13:46:05 +08008362 if (vmx->pml_pg) {
8363 __free_page(vmx->pml_pg);
8364 vmx->pml_pg = NULL;
8365 }
Kai Huang843e4332015-01-28 10:54:28 +08008366}
8367
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008368static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008369{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008370 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008371 u64 *pml_buf;
8372 u16 pml_idx;
8373
8374 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8375
8376 /* Do nothing if PML buffer is empty */
8377 if (pml_idx == (PML_ENTITY_NUM - 1))
8378 return;
8379
8380 /* PML index always points to next available PML buffer entity */
8381 if (pml_idx >= PML_ENTITY_NUM)
8382 pml_idx = 0;
8383 else
8384 pml_idx++;
8385
8386 pml_buf = page_address(vmx->pml_pg);
8387 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8388 u64 gpa;
8389
8390 gpa = pml_buf[pml_idx];
8391 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008392 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008393 }
8394
8395 /* reset PML index */
8396 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8397}
8398
8399/*
8400 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8401 * Called before reporting dirty_bitmap to userspace.
8402 */
8403static void kvm_flush_pml_buffers(struct kvm *kvm)
8404{
8405 int i;
8406 struct kvm_vcpu *vcpu;
8407 /*
8408 * We only need to kick vcpu out of guest mode here, as PML buffer
8409 * is flushed at beginning of all VMEXITs, and it's obvious that only
8410 * vcpus running in guest are possible to have unflushed GPAs in PML
8411 * buffer.
8412 */
8413 kvm_for_each_vcpu(i, vcpu, kvm)
8414 kvm_vcpu_kick(vcpu);
8415}
8416
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008417static void vmx_dump_sel(char *name, uint32_t sel)
8418{
8419 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008420 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008421 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8422 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8423 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8424}
8425
8426static void vmx_dump_dtsel(char *name, uint32_t limit)
8427{
8428 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8429 name, vmcs_read32(limit),
8430 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8431}
8432
8433static void dump_vmcs(void)
8434{
8435 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8436 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8437 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8438 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8439 u32 secondary_exec_control = 0;
8440 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008441 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008442 int i, n;
8443
8444 if (cpu_has_secondary_exec_ctrls())
8445 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8446
8447 pr_err("*** Guest State ***\n");
8448 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8449 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8450 vmcs_readl(CR0_GUEST_HOST_MASK));
8451 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8452 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8453 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8454 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8455 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8456 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008457 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8458 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8459 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8460 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008461 }
8462 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8463 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8464 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8465 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8466 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8467 vmcs_readl(GUEST_SYSENTER_ESP),
8468 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8469 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8470 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8471 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8472 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8473 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8474 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8475 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8476 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8477 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8478 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8479 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8480 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008481 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8482 efer, vmcs_read64(GUEST_IA32_PAT));
8483 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8484 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008485 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8486 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008487 pr_err("PerfGlobCtl = 0x%016llx\n",
8488 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008489 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008490 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008491 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8492 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8493 vmcs_read32(GUEST_ACTIVITY_STATE));
8494 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8495 pr_err("InterruptStatus = %04x\n",
8496 vmcs_read16(GUEST_INTR_STATUS));
8497
8498 pr_err("*** Host State ***\n");
8499 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8500 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8501 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8502 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8503 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8504 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8505 vmcs_read16(HOST_TR_SELECTOR));
8506 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8507 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8508 vmcs_readl(HOST_TR_BASE));
8509 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8510 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8511 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8512 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8513 vmcs_readl(HOST_CR4));
8514 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8515 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8516 vmcs_read32(HOST_IA32_SYSENTER_CS),
8517 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8518 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008519 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8520 vmcs_read64(HOST_IA32_EFER),
8521 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008522 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008523 pr_err("PerfGlobCtl = 0x%016llx\n",
8524 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008525
8526 pr_err("*** Control State ***\n");
8527 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8528 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8529 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8530 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8531 vmcs_read32(EXCEPTION_BITMAP),
8532 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8533 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8534 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8535 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8536 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8537 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8538 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8539 vmcs_read32(VM_EXIT_INTR_INFO),
8540 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8541 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8542 pr_err(" reason=%08x qualification=%016lx\n",
8543 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8544 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8545 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8546 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008547 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008548 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008549 pr_err("TSC Multiplier = 0x%016llx\n",
8550 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008551 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8552 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8553 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8554 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8555 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008556 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008557 n = vmcs_read32(CR3_TARGET_COUNT);
8558 for (i = 0; i + 1 < n; i += 4)
8559 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8560 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8561 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8562 if (i < n)
8563 pr_err("CR3 target%u=%016lx\n",
8564 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8565 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8566 pr_err("PLE Gap=%08x Window=%08x\n",
8567 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8568 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8569 pr_err("Virtual processor ID = 0x%04x\n",
8570 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8571}
8572
Avi Kivity6aa8b732006-12-10 02:21:36 -08008573/*
8574 * The guest has exited. See if we can fix it or if we need userspace
8575 * assistance.
8576 */
Avi Kivity851ba692009-08-24 11:10:17 +03008577static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008578{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008579 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008580 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008581 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008582
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008583 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8584
Kai Huang843e4332015-01-28 10:54:28 +08008585 /*
8586 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8587 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8588 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8589 * mode as if vcpus is in root mode, the PML buffer must has been
8590 * flushed already.
8591 */
8592 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008593 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008594
Mohammed Gamal80ced182009-09-01 12:48:18 +02008595 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008596 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008597 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008598
Nadav Har'El644d7112011-05-25 23:12:35 +03008599 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008600 nested_vmx_vmexit(vcpu, exit_reason,
8601 vmcs_read32(VM_EXIT_INTR_INFO),
8602 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008603 return 1;
8604 }
8605
Mohammed Gamal51207022010-05-31 22:40:54 +03008606 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008607 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008608 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8609 vcpu->run->fail_entry.hardware_entry_failure_reason
8610 = exit_reason;
8611 return 0;
8612 }
8613
Avi Kivity29bd8a72007-09-10 17:27:03 +03008614 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008615 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8616 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008617 = vmcs_read32(VM_INSTRUCTION_ERROR);
8618 return 0;
8619 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008620
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008621 /*
8622 * Note:
8623 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8624 * delivery event since it indicates guest is accessing MMIO.
8625 * The vm-exit can be triggered again after return to guest that
8626 * will cause infinite loop.
8627 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008628 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008629 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008630 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008631 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008632 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8633 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8634 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8635 vcpu->run->internal.ndata = 2;
8636 vcpu->run->internal.data[0] = vectoring_info;
8637 vcpu->run->internal.data[1] = exit_reason;
8638 return 0;
8639 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008640
Nadav Har'El644d7112011-05-25 23:12:35 +03008641 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8642 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008643 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008644 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008645 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008646 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008647 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008648 /*
8649 * This CPU don't support us in finding the end of an
8650 * NMI-blocked window if the guest runs with IRQs
8651 * disabled. So we pull the trigger after 1 s of
8652 * futile waiting, but inform the user about this.
8653 */
8654 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8655 "state on VCPU %d after 1 s timeout\n",
8656 __func__, vcpu->vcpu_id);
8657 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008658 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008659 }
8660
Avi Kivity6aa8b732006-12-10 02:21:36 -08008661 if (exit_reason < kvm_vmx_max_exit_handlers
8662 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008663 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008664 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008665 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8666 kvm_queue_exception(vcpu, UD_VECTOR);
8667 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008668 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008669}
8670
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008671/*
8672 * Software based L1D cache flush which is used when microcode providing
8673 * the cache control MSR is not loaded.
8674 *
8675 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8676 * flush it is required to read in 64 KiB because the replacement algorithm
8677 * is not exactly LRU. This could be sized at runtime via topology
8678 * information but as all relevant affected CPUs have 32KiB L1D cache size
8679 * there is no point in doing so.
8680 */
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008681static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008682{
8683 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008684
8685 /*
Thomas Gleixner31282cf2018-07-13 16:23:17 +02008686 * This code is only executed when the the flush mode is 'cond' or
8687 * 'always'
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008688 */
Nicolai Stange936f5662018-07-21 22:25:00 +02008689 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stangee371c922018-07-27 13:22:16 +02008690 bool flush_l1d;
Nicolai Stange90bc3062018-07-21 22:35:28 +02008691
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008692 /*
Nicolai Stangee371c922018-07-27 13:22:16 +02008693 * Clear the per-vcpu flush bit, it gets set again
8694 * either from vcpu_run() or from one of the unsafe
8695 * VMEXIT handlers.
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008696 */
Nicolai Stangee371c922018-07-27 13:22:16 +02008697 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixnerdff09822018-07-13 16:23:20 +02008698 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stangee371c922018-07-27 13:22:16 +02008699
8700 /*
8701 * Clear the per-cpu flush bit, it gets set again from
8702 * the interrupt handlers.
8703 */
8704 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
8705 kvm_clear_cpu_l1tf_flush_l1d();
8706
Nicolai Stange90bc3062018-07-21 22:35:28 +02008707 if (!flush_l1d)
8708 return;
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008709 }
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008710
8711 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008712
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008713 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8714 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8715 return;
8716 }
8717
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008718 asm volatile(
8719 /* First ensure the pages are in the TLB */
8720 "xorl %%eax, %%eax\n"
8721 ".Lpopulate_tlb:\n\t"
Nicolai Stange587d4992018-07-18 19:07:38 +02008722 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008723 "addl $4096, %%eax\n\t"
8724 "cmpl %%eax, %[size]\n\t"
8725 "jne .Lpopulate_tlb\n\t"
8726 "xorl %%eax, %%eax\n\t"
8727 "cpuid\n\t"
8728 /* Now fill the cache */
8729 "xorl %%eax, %%eax\n"
8730 ".Lfill_cache:\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008731 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008732 "addl $64, %%eax\n\t"
8733 "cmpl %%eax, %[size]\n\t"
8734 "jne .Lfill_cache\n\t"
8735 "lfence\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008736 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008737 [size] "r" (size)
8738 : "eax", "ebx", "ecx", "edx");
8739}
8740
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008741static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008742{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008743 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8744
8745 if (is_guest_mode(vcpu) &&
8746 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8747 return;
8748
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008749 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008750 vmcs_write32(TPR_THRESHOLD, 0);
8751 return;
8752 }
8753
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008754 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008755}
8756
Yang Zhang8d146952013-01-25 10:18:50 +08008757static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8758{
8759 u32 sec_exec_control;
8760
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008761 /* Postpone execution until vmcs01 is the current VMCS. */
8762 if (is_guest_mode(vcpu)) {
8763 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8764 return;
8765 }
8766
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008767 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008768 return;
8769
Paolo Bonzini35754c92015-07-29 12:05:37 +02008770 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008771 return;
8772
8773 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8774
8775 if (set) {
8776 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8777 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8778 } else {
8779 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8780 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008781 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008782 }
8783 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8784
Paolo Bonzini6236b782018-01-16 16:51:18 +01008785 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008786}
8787
Tang Chen38b99172014-09-24 15:57:54 +08008788static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8789{
8790 struct vcpu_vmx *vmx = to_vmx(vcpu);
8791
8792 /*
8793 * Currently we do not handle the nested case where L2 has an
8794 * APIC access page of its own; that page is still pinned.
8795 * Hence, we skip the case where the VCPU is in guest mode _and_
8796 * L1 prepared an APIC access page for L2.
8797 *
8798 * For the case where L1 and L2 share the same APIC access page
8799 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8800 * in the vmcs12), this function will only update either the vmcs01
8801 * or the vmcs02. If the former, the vmcs02 will be updated by
8802 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8803 * the next L2->L1 exit.
8804 */
8805 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008806 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008807 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008808 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008809 vmx_flush_tlb_ept_only(vcpu);
8810 }
Tang Chen38b99172014-09-24 15:57:54 +08008811}
8812
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008813static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008814{
8815 u16 status;
8816 u8 old;
8817
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008818 if (max_isr == -1)
8819 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008820
8821 status = vmcs_read16(GUEST_INTR_STATUS);
8822 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008823 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008824 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008825 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008826 vmcs_write16(GUEST_INTR_STATUS, status);
8827 }
8828}
8829
8830static void vmx_set_rvi(int vector)
8831{
8832 u16 status;
8833 u8 old;
8834
Wei Wang4114c272014-11-05 10:53:43 +08008835 if (vector == -1)
8836 vector = 0;
8837
Yang Zhangc7c9c562013-01-25 10:18:51 +08008838 status = vmcs_read16(GUEST_INTR_STATUS);
8839 old = (u8)status & 0xff;
8840 if ((u8)vector != old) {
8841 status &= ~0xff;
8842 status |= (u8)vector;
8843 vmcs_write16(GUEST_INTR_STATUS, status);
8844 }
8845}
8846
8847static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8848{
Wanpeng Li963fee12014-07-17 19:03:00 +08008849 if (!is_guest_mode(vcpu)) {
8850 vmx_set_rvi(max_irr);
8851 return;
8852 }
8853
Wei Wang4114c272014-11-05 10:53:43 +08008854 if (max_irr == -1)
8855 return;
8856
Wanpeng Li963fee12014-07-17 19:03:00 +08008857 /*
Wei Wang4114c272014-11-05 10:53:43 +08008858 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8859 * handles it.
8860 */
8861 if (nested_exit_on_intr(vcpu))
8862 return;
8863
8864 /*
8865 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008866 * is run without virtual interrupt delivery.
8867 */
8868 if (!kvm_event_needs_reinjection(vcpu) &&
8869 vmx_interrupt_allowed(vcpu)) {
8870 kvm_queue_interrupt(vcpu, max_irr, false);
8871 vmx_inject_irq(vcpu);
8872 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008873}
8874
Andrey Smetanin63086302015-11-10 15:36:32 +03008875static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008876{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008877 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008878 return;
8879
Yang Zhangc7c9c562013-01-25 10:18:51 +08008880 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8881 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8882 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8883 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8884}
8885
Avi Kivity51aa01d2010-07-20 14:31:20 +03008886static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008887{
Avi Kivity00eba012011-03-07 17:24:54 +02008888 u32 exit_intr_info;
8889
8890 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8891 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8892 return;
8893
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008894 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008895 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008896
8897 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008898 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008899 kvm_machine_check();
8900
Gleb Natapov20f65982009-05-11 13:35:55 +03008901 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008902 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008903 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008904 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008905 kvm_after_handle_nmi(&vmx->vcpu);
8906 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008907}
Gleb Natapov20f65982009-05-11 13:35:55 +03008908
Yang Zhanga547c6d2013-04-11 19:25:10 +08008909static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8910{
8911 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008912 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008913
8914 /*
8915 * If external interrupt exists, IF bit is set in rflags/eflags on the
8916 * interrupt stack frame, and interrupt will be enabled on a return
8917 * from interrupt handler.
8918 */
8919 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8920 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8921 unsigned int vector;
8922 unsigned long entry;
8923 gate_desc *desc;
8924 struct vcpu_vmx *vmx = to_vmx(vcpu);
8925#ifdef CONFIG_X86_64
8926 unsigned long tmp;
8927#endif
8928
8929 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8930 desc = (gate_desc *)vmx->host_idt_base + vector;
8931 entry = gate_offset(*desc);
8932 asm volatile(
8933#ifdef CONFIG_X86_64
8934 "mov %%" _ASM_SP ", %[sp]\n\t"
8935 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8936 "push $%c[ss]\n\t"
8937 "push %[sp]\n\t"
8938#endif
8939 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008940 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008941 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008942 :
8943#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008944 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008945#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008946 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008947 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008948 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008949 [ss]"i"(__KERNEL_DS),
8950 [cs]"i"(__KERNEL_CS)
8951 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008952 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008953}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008954STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008955
Tom Lendackyb9655922018-05-10 22:06:39 +02008956static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008957{
Tom Lendackyb9655922018-05-10 22:06:39 +02008958 switch (index) {
8959 case MSR_IA32_SMBASE:
8960 /*
8961 * We cannot do SMM unless we can run the guest in big
8962 * real mode.
8963 */
8964 return enable_unrestricted_guest || emulate_invalid_guest_state;
8965 case MSR_AMD64_VIRT_SPEC_CTRL:
8966 /* This is AMD only. */
8967 return false;
8968 default:
8969 return true;
8970 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008971}
8972
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008973static bool vmx_mpx_supported(void)
8974{
8975 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8976 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8977}
8978
Wanpeng Li55412b22014-12-02 19:21:30 +08008979static bool vmx_xsaves_supported(void)
8980{
8981 return vmcs_config.cpu_based_2nd_exec_ctrl &
8982 SECONDARY_EXEC_XSAVES;
8983}
8984
Avi Kivity51aa01d2010-07-20 14:31:20 +03008985static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8986{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008987 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008988 bool unblock_nmi;
8989 u8 vector;
8990 bool idtv_info_valid;
8991
8992 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008993
Avi Kivitycf393f72008-07-01 16:20:21 +03008994 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008995 if (vmx->nmi_known_unmasked)
8996 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008997 /*
8998 * Can't use vmx->exit_intr_info since we're not sure what
8999 * the exit reason is.
9000 */
9001 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03009002 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9003 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9004 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009005 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03009006 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9007 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009008 * SDM 3: 23.2.2 (September 2008)
9009 * Bit 12 is undefined in any of the following cases:
9010 * If the VM exit sets the valid bit in the IDT-vectoring
9011 * information field.
9012 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03009013 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009014 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9015 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03009016 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9017 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02009018 else
9019 vmx->nmi_known_unmasked =
9020 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9021 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009022 } else if (unlikely(vmx->soft_vnmi_blocked))
9023 vmx->vnmi_blocked_time +=
9024 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009025}
9026
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009027static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009028 u32 idt_vectoring_info,
9029 int instr_len_field,
9030 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009031{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009032 u8 vector;
9033 int type;
9034 bool idtv_info_valid;
9035
9036 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009037
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009038 vcpu->arch.nmi_injected = false;
9039 kvm_clear_exception_queue(vcpu);
9040 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009041
9042 if (!idtv_info_valid)
9043 return;
9044
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009045 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009046
Avi Kivity668f6122008-07-02 09:28:55 +03009047 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9048 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009049
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009050 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009051 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009052 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009053 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009054 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009055 * Clear bit "block by NMI" before VM entry if a NMI
9056 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009057 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009058 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009059 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009060 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009061 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009062 /* fall through */
9063 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009064 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009065 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009066 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009067 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009068 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009069 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009070 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009071 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009072 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009073 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009074 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009075 break;
9076 default:
9077 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009078 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009079}
9080
Avi Kivity83422e12010-07-20 14:43:23 +03009081static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9082{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009083 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009084 VM_EXIT_INSTRUCTION_LEN,
9085 IDT_VECTORING_ERROR_CODE);
9086}
9087
Avi Kivityb463a6f2010-07-20 15:06:17 +03009088static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9089{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009090 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009091 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9092 VM_ENTRY_INSTRUCTION_LEN,
9093 VM_ENTRY_EXCEPTION_ERROR_CODE);
9094
9095 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9096}
9097
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009098static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9099{
9100 int i, nr_msrs;
9101 struct perf_guest_switch_msr *msrs;
9102
9103 msrs = perf_guest_get_msrs(&nr_msrs);
9104
9105 if (!msrs)
9106 return;
9107
9108 for (i = 0; i < nr_msrs; i++)
9109 if (msrs[i].host == msrs[i].guest)
9110 clear_atomic_switch_msr(vmx, msrs[i].msr);
9111 else
9112 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04009113 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009114}
9115
Yunhong Jiang64672c92016-06-13 14:19:59 -07009116void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9117{
9118 struct vcpu_vmx *vmx = to_vmx(vcpu);
9119 u64 tscl;
9120 u32 delta_tsc;
9121
9122 if (vmx->hv_deadline_tsc == -1)
9123 return;
9124
9125 tscl = rdtsc();
9126 if (vmx->hv_deadline_tsc > tscl)
9127 /* sure to be 32 bit only because checked on set_hv_timer */
9128 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9129 cpu_preemption_timer_multi);
9130 else
9131 delta_tsc = 0;
9132
9133 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9134}
9135
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009136static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009137{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009138 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009139 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009140
9141 /* Record the guest's net vcpu time for enforced NMI injections. */
9142 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9143 vmx->entry_time = ktime_get();
9144
9145 /* Don't enter VMX if guest state is invalid, let the exit handler
9146 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009147 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009148 return;
9149
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009150 if (vmx->ple_window_dirty) {
9151 vmx->ple_window_dirty = false;
9152 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9153 }
9154
Abel Gordon012f83c2013-04-18 14:39:25 +03009155 if (vmx->nested.sync_shadow_vmcs) {
9156 copy_vmcs12_to_shadow(vmx);
9157 vmx->nested.sync_shadow_vmcs = false;
9158 }
9159
Avi Kivity104f2262010-11-18 13:12:52 +02009160 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9161 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9162 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9163 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9164
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009165 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009166 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9167 vmcs_writel(HOST_CR4, cr4);
9168 vmx->host_state.vmcs_host_cr4 = cr4;
9169 }
9170
Avi Kivity104f2262010-11-18 13:12:52 +02009171 /* When single-stepping over STI and MOV SS, we must clear the
9172 * corresponding interruptibility bits in the guest state. Otherwise
9173 * vmentry fails as it then expects bit 14 (BS) in pending debug
9174 * exceptions being set, but that's not correct for the guest debugging
9175 * case. */
9176 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9177 vmx_set_interrupt_shadow(vcpu, 0);
9178
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009179 if (vmx->guest_pkru_valid)
9180 __write_pkru(vmx->guest_pkru);
9181
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009182 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009183 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009184
Yunhong Jiang64672c92016-06-13 14:19:59 -07009185 vmx_arm_hv_timer(vcpu);
9186
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009187 /*
9188 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9189 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9190 * is no need to worry about the conditional branch over the wrmsr
9191 * being speculatively taken.
9192 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009193 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009194
Nadav Har'Eld462b812011-05-24 15:26:10 +03009195 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009196
Thomas Gleixner3a8e7f62019-02-27 12:48:14 +01009197 /* L1D Flush includes CPU buffer clear to mitigate MDS */
Nicolai Stange90bc3062018-07-21 22:35:28 +02009198 if (static_branch_unlikely(&vmx_l1d_should_flush))
9199 vmx_l1d_flush(vcpu);
Thomas Gleixner3a8e7f62019-02-27 12:48:14 +01009200 else if (static_branch_unlikely(&mds_user_clear))
9201 mds_clear_cpu_buffers();
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009202
Avi Kivity104f2262010-11-18 13:12:52 +02009203 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009204 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009205 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9206 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9207 "push %%" _ASM_CX " \n\t"
9208 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009209 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009210 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009211 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009212 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009213 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009214 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9215 "mov %%cr2, %%" _ASM_DX " \n\t"
9216 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009217 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009218 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009219 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009220 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009221 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009222 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009223 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9224 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9225 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9226 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9227 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9228 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009229#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009230 "mov %c[r8](%0), %%r8 \n\t"
9231 "mov %c[r9](%0), %%r9 \n\t"
9232 "mov %c[r10](%0), %%r10 \n\t"
9233 "mov %c[r11](%0), %%r11 \n\t"
9234 "mov %c[r12](%0), %%r12 \n\t"
9235 "mov %c[r13](%0), %%r13 \n\t"
9236 "mov %c[r14](%0), %%r14 \n\t"
9237 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009238#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009239 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009240
Avi Kivity6aa8b732006-12-10 02:21:36 -08009241 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009242 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009243 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009244 "jmp 2f \n\t"
9245 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9246 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009247 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009248 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009249 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009250 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009251 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9252 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9253 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9254 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9255 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9256 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9257 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009258#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009259 "mov %%r8, %c[r8](%0) \n\t"
9260 "mov %%r9, %c[r9](%0) \n\t"
9261 "mov %%r10, %c[r10](%0) \n\t"
9262 "mov %%r11, %c[r11](%0) \n\t"
9263 "mov %%r12, %c[r12](%0) \n\t"
9264 "mov %%r13, %c[r13](%0) \n\t"
9265 "mov %%r14, %c[r14](%0) \n\t"
9266 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009267 "xor %%r8d, %%r8d \n\t"
9268 "xor %%r9d, %%r9d \n\t"
9269 "xor %%r10d, %%r10d \n\t"
9270 "xor %%r11d, %%r11d \n\t"
9271 "xor %%r12d, %%r12d \n\t"
9272 "xor %%r13d, %%r13d \n\t"
9273 "xor %%r14d, %%r14d \n\t"
9274 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009275#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009276 "mov %%cr2, %%" _ASM_AX " \n\t"
9277 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009278
Jim Mattson491c0ca2018-01-03 14:31:38 -08009279 "xor %%eax, %%eax \n\t"
9280 "xor %%ebx, %%ebx \n\t"
9281 "xor %%esi, %%esi \n\t"
9282 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009283 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009284 ".pushsection .rodata \n\t"
9285 ".global vmx_return \n\t"
9286 "vmx_return: " _ASM_PTR " 2b \n\t"
9287 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009288 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009289 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009290 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009291 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009292 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9293 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9294 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9295 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9296 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9297 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9298 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009299#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009300 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9301 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9302 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9303 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9304 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9305 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9306 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9307 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009308#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009309 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9310 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009311 : "cc", "memory"
9312#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009313 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009314 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009315#else
9316 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009317#endif
9318 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009319
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009320 /*
9321 * We do not use IBRS in the kernel. If this vCPU has used the
9322 * SPEC_CTRL MSR it may have left it on; save the value and
9323 * turn it off. This is much more efficient than blindly adding
9324 * it to the atomic save/restore list. Especially as the former
9325 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9326 *
9327 * For non-nested case:
9328 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9329 * save it.
9330 *
9331 * For nested case:
9332 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9333 * save it.
9334 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009335 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009336 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009337
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009338 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009339
David Woodhousec1ddd992018-01-12 11:11:27 +00009340 /* Eliminate branch target predictions from guest mode */
9341 vmexit_fill_RSB();
9342
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009343 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9344 if (debugctlmsr)
9345 update_debugctlmsr(debugctlmsr);
9346
Avi Kivityaa67f602012-08-01 16:48:03 +03009347#ifndef CONFIG_X86_64
9348 /*
9349 * The sysexit path does not restore ds/es, so we must set them to
9350 * a reasonable value ourselves.
9351 *
9352 * We can't defer this to vmx_load_host_state() since that function
9353 * may be executed in interrupt context, which saves and restore segments
9354 * around it, nullifying its effect.
9355 */
9356 loadsegment(ds, __USER_DS);
9357 loadsegment(es, __USER_DS);
9358#endif
9359
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009360 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009361 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009362 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009363 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009364 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009365 vcpu->arch.regs_dirty = 0;
9366
Avi Kivity1155f762007-11-22 11:30:47 +02009367 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9368
Nadav Har'Eld462b812011-05-24 15:26:10 +03009369 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009370
Avi Kivity51aa01d2010-07-20 14:31:20 +03009371 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009372
Gleb Natapove0b890d2013-09-25 12:51:33 +03009373 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009374 * eager fpu is enabled if PKEY is supported and CR4 is switched
9375 * back on host, so it is safe to read guest PKRU from current
9376 * XSAVE.
9377 */
9378 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9379 vmx->guest_pkru = __read_pkru();
9380 if (vmx->guest_pkru != vmx->host_pkru) {
9381 vmx->guest_pkru_valid = true;
9382 __write_pkru(vmx->host_pkru);
9383 } else
9384 vmx->guest_pkru_valid = false;
9385 }
9386
9387 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009388 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9389 * we did not inject a still-pending event to L1 now because of
9390 * nested_run_pending, we need to re-enable this bit.
9391 */
9392 if (vmx->nested.nested_run_pending)
9393 kvm_make_request(KVM_REQ_EVENT, vcpu);
9394
9395 vmx->nested.nested_run_pending = 0;
9396
Avi Kivity51aa01d2010-07-20 14:31:20 +03009397 vmx_complete_atomic_exit(vmx);
9398 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009399 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009400}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009401STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009402
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009403static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9404{
9405 struct vcpu_vmx *vmx = to_vmx(vcpu);
9406 int cpu;
9407
9408 if (vmx->loaded_vmcs == &vmx->vmcs01)
9409 return;
9410
9411 cpu = get_cpu();
9412 vmx->loaded_vmcs = &vmx->vmcs01;
9413 vmx_vcpu_put(vcpu);
9414 vmx_vcpu_load(vcpu, cpu);
9415 vcpu->cpu = cpu;
9416 put_cpu();
9417}
9418
Jim Mattson2f1fe812016-07-08 15:36:06 -07009419/*
9420 * Ensure that the current vmcs of the logical processor is the
9421 * vmcs01 of the vcpu before calling free_nested().
9422 */
9423static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9424{
9425 struct vcpu_vmx *vmx = to_vmx(vcpu);
9426 int r;
9427
9428 r = vcpu_load(vcpu);
9429 BUG_ON(r);
9430 vmx_load_vmcs01(vcpu);
9431 free_nested(vmx);
9432 vcpu_put(vcpu);
9433}
9434
Avi Kivity6aa8b732006-12-10 02:21:36 -08009435static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9436{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009437 struct vcpu_vmx *vmx = to_vmx(vcpu);
9438
Kai Huang843e4332015-01-28 10:54:28 +08009439 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009440 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009441 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009442 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009443 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009444 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009445 kfree(vmx->guest_msrs);
9446 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009447 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009448}
9449
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009450static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009451{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009452 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009453 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009454 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009455 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009456
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009457 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009458 return ERR_PTR(-ENOMEM);
9459
Wanpeng Li991e7a02015-09-16 17:30:05 +08009460 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009461
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009462 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9463 if (err)
9464 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009465
Peter Feiner4e595162016-07-07 14:49:58 -07009466 err = -ENOMEM;
9467
9468 /*
9469 * If PML is turned on, failure on enabling PML just results in failure
9470 * of creating the vcpu, therefore we can simplify PML logic (by
9471 * avoiding dealing with cases, such as enabling PML partially on vcpus
9472 * for the guest, etc.
9473 */
9474 if (enable_pml) {
9475 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9476 if (!vmx->pml_pg)
9477 goto uninit_vcpu;
9478 }
9479
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009480 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009481 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9482 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009483
Peter Feiner4e595162016-07-07 14:49:58 -07009484 if (!vmx->guest_msrs)
9485 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009486
Nadav Har'Eld462b812011-05-24 15:26:10 +03009487 if (!vmm_exclusive)
9488 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009489 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009490 if (!vmm_exclusive)
9491 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009492 if (err < 0)
9493 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009494
Paolo Bonzini6236b782018-01-16 16:51:18 +01009495 msr_bitmap = vmx->vmcs01.msr_bitmap;
9496 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9497 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9498 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9499 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9500 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9501 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9502 vmx->msr_bitmap_mode = 0;
9503
Paolo Bonziniff546f92018-01-11 12:16:15 +01009504 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009505 cpu = get_cpu();
9506 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009507 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009508 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009509 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009510 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009511 if (err)
9512 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009513 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009514 err = alloc_apic_access_page(kvm);
9515 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009516 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009517 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009518
Sheng Yangb927a3c2009-07-21 10:42:48 +08009519 if (enable_ept) {
9520 if (!kvm->arch.ept_identity_map_addr)
9521 kvm->arch.ept_identity_map_addr =
9522 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009523 err = init_rmode_identity_map(kvm);
9524 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009525 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009526 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009527
Roman Kagan020a90f2018-07-19 21:59:07 +03009528 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009529 nested_vmx_setup_ctls_msrs(vmx);
9530
Wincy Van705699a2015-02-03 23:58:17 +08009531 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009532 vmx->nested.current_vmptr = -1ull;
9533 vmx->nested.current_vmcs12 = NULL;
9534
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009535 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9536
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009537 /*
9538 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9539 * or POSTED_INTR_WAKEUP_VECTOR.
9540 */
9541 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9542 vmx->pi_desc.sn = 1;
9543
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009544 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009545
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009546free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009547 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009548free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009549 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009550free_pml:
9551 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009552uninit_vcpu:
9553 kvm_vcpu_uninit(&vmx->vcpu);
9554free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009555 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009556 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009557 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009558}
9559
Thomas Gleixnercb106032019-02-19 11:10:49 +01009560#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/hw-vuln/l1tf.html for details.\n"
9561#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/hw-vuln/l1tf.html for details.\n"
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009562
9563static int vmx_vm_init(struct kvm *kvm)
9564{
Jiri Kosina2decbf52018-07-13 16:23:25 +02009565 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
9566 switch (l1tf_mitigation) {
9567 case L1TF_MITIGATION_OFF:
9568 case L1TF_MITIGATION_FLUSH_NOWARN:
9569 /* 'I explicitly don't care' is set */
9570 break;
9571 case L1TF_MITIGATION_FLUSH:
9572 case L1TF_MITIGATION_FLUSH_NOSMT:
9573 case L1TF_MITIGATION_FULL:
9574 /*
9575 * Warn upon starting the first VM in a potentially
9576 * insecure environment.
9577 */
9578 if (cpu_smt_control == CPU_SMT_ENABLED)
9579 pr_warn_once(L1TF_MSG_SMT);
9580 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
9581 pr_warn_once(L1TF_MSG_L1D);
9582 break;
9583 case L1TF_MITIGATION_FULL_FORCE:
9584 /* Flush is enforced */
9585 break;
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009586 }
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009587 }
9588 return 0;
9589}
9590
Yang, Sheng002c7f72007-07-31 14:23:01 +03009591static void __init vmx_check_processor_compat(void *rtn)
9592{
9593 struct vmcs_config vmcs_conf;
9594
9595 *(int *)rtn = 0;
9596 if (setup_vmcs_config(&vmcs_conf) < 0)
9597 *(int *)rtn = -EIO;
9598 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9599 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9600 smp_processor_id());
9601 *(int *)rtn = -EIO;
9602 }
9603}
9604
Sheng Yang67253af2008-04-25 10:20:22 +08009605static int get_ept_level(void)
9606{
9607 return VMX_EPT_DEFAULT_GAW + 1;
9608}
9609
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009610static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009611{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009612 u8 cache;
9613 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009614
Sheng Yang522c68c2009-04-27 20:35:43 +08009615 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009616 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009617 * 2. EPT with VT-d:
9618 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009619 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009620 * b. VT-d with snooping control feature: snooping control feature of
9621 * VT-d engine can guarantee the cache correctness. Just set it
9622 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009623 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009624 * consistent with host MTRR
9625 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009626 if (is_mmio) {
9627 cache = MTRR_TYPE_UNCACHABLE;
9628 goto exit;
9629 }
9630
9631 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009632 ipat = VMX_EPT_IPAT_BIT;
9633 cache = MTRR_TYPE_WRBACK;
9634 goto exit;
9635 }
9636
9637 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9638 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009639 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009640 cache = MTRR_TYPE_WRBACK;
9641 else
9642 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009643 goto exit;
9644 }
9645
Xiao Guangrongff536042015-06-15 16:55:22 +08009646 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009647
9648exit:
9649 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009650}
9651
Sheng Yang17cc3932010-01-05 19:02:27 +08009652static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009653{
Sheng Yang878403b2010-01-05 19:02:29 +08009654 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9655 return PT_DIRECTORY_LEVEL;
9656 else
9657 /* For shadow and EPT supported 1GB page */
9658 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009659}
9660
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009661static void vmcs_set_secondary_exec_control(u32 new_ctl)
9662{
9663 /*
9664 * These bits in the secondary execution controls field
9665 * are dynamic, the others are mostly based on the hypervisor
9666 * architecture and the guest's CPUID. Do not touch the
9667 * dynamic bits.
9668 */
9669 u32 mask =
9670 SECONDARY_EXEC_SHADOW_VMCS |
9671 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9672 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9673
9674 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9675
9676 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9677 (new_ctl & ~mask) | (cur_ctl & mask));
9678}
9679
Sheng Yang0e851882009-12-18 16:48:46 +08009680static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9681{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009682 struct kvm_cpuid_entry2 *best;
9683 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009684 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009685
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009686 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009687 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9688 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009689 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009690
Paolo Bonzini8b972652015-09-15 17:34:42 +02009691 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009692 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009693 vmx->nested.nested_vmx_secondary_ctls_high |=
9694 SECONDARY_EXEC_RDTSCP;
9695 else
9696 vmx->nested.nested_vmx_secondary_ctls_high &=
9697 ~SECONDARY_EXEC_RDTSCP;
9698 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009699 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009700
Mao, Junjiead756a12012-07-02 01:18:48 +00009701 /* Exposing INVPCID only when PCID is exposed */
9702 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9703 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009704 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9705 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009706 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009707
Mao, Junjiead756a12012-07-02 01:18:48 +00009708 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009709 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009710 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009711
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009712 if (cpu_has_secondary_exec_ctrls())
9713 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009714
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009715 if (nested_vmx_allowed(vcpu))
9716 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9717 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9718 else
9719 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9720 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009721}
9722
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009723static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9724{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009725 if (func == 1 && nested)
9726 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009727}
9728
Yang Zhang25d92082013-08-06 12:00:32 +03009729static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9730 struct x86_exception *fault)
9731{
Jan Kiszka533558b2014-01-04 18:47:20 +01009732 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9733 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009734
9735 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009736 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009737 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009738 exit_reason = EXIT_REASON_EPT_VIOLATION;
9739 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009740 vmcs12->guest_physical_address = fault->address;
9741}
9742
Nadav Har'El155a97a2013-08-05 11:07:16 +03009743/* Callbacks for nested_ept_init_mmu_context: */
9744
9745static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9746{
9747 /* return the page table to be shadowed - in our case, EPT12 */
9748 return get_vmcs12(vcpu)->ept_pointer;
9749}
9750
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009751static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009752{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009753 WARN_ON(mmu_is_nested(vcpu));
9754 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009755 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9756 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009757 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9758 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9759 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9760
9761 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009762}
9763
9764static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9765{
9766 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9767}
9768
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009769static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9770 u16 error_code)
9771{
9772 bool inequality, bit;
9773
9774 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9775 inequality =
9776 (error_code & vmcs12->page_fault_error_code_mask) !=
9777 vmcs12->page_fault_error_code_match;
9778 return inequality ^ bit;
9779}
9780
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009781static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9782 struct x86_exception *fault)
9783{
9784 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9785
9786 WARN_ON(!is_guest_mode(vcpu));
9787
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009788 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009789 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9790 vmcs_read32(VM_EXIT_INTR_INFO),
9791 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009792 else
9793 kvm_inject_page_fault(vcpu, fault);
9794}
9795
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009796static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9797 struct vmcs12 *vmcs12)
9798{
9799 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009800 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009801
9802 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009803 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9804 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009805 return false;
9806
9807 /*
9808 * Translate L1 physical address to host physical
9809 * address for vmcs02. Keep the page pinned, so this
9810 * physical address remains valid. We keep a reference
9811 * to it so we can release it later.
9812 */
9813 if (vmx->nested.apic_access_page) /* shouldn't happen */
9814 nested_release_page(vmx->nested.apic_access_page);
9815 vmx->nested.apic_access_page =
9816 nested_get_page(vcpu, vmcs12->apic_access_addr);
9817 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009818
9819 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009820 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9821 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009822 return false;
9823
9824 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9825 nested_release_page(vmx->nested.virtual_apic_page);
9826 vmx->nested.virtual_apic_page =
9827 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9828
9829 /*
9830 * Failing the vm entry is _not_ what the processor does
9831 * but it's basically the only possibility we have.
9832 * We could still enter the guest if CR8 load exits are
9833 * enabled, CR8 store exits are enabled, and virtualize APIC
9834 * access is disabled; in this case the processor would never
9835 * use the TPR shadow and we could simply clear the bit from
9836 * the execution control. But such a configuration is useless,
9837 * so let's keep the code simple.
9838 */
9839 if (!vmx->nested.virtual_apic_page)
9840 return false;
9841 }
9842
Wincy Van705699a2015-02-03 23:58:17 +08009843 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009844 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9845 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009846 return false;
9847
9848 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9849 kunmap(vmx->nested.pi_desc_page);
9850 nested_release_page(vmx->nested.pi_desc_page);
9851 }
9852 vmx->nested.pi_desc_page =
9853 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9854 if (!vmx->nested.pi_desc_page)
9855 return false;
9856
9857 vmx->nested.pi_desc =
9858 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9859 if (!vmx->nested.pi_desc) {
9860 nested_release_page_clean(vmx->nested.pi_desc_page);
9861 return false;
9862 }
9863 vmx->nested.pi_desc =
9864 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9865 (unsigned long)(vmcs12->posted_intr_desc_addr &
9866 (PAGE_SIZE - 1)));
9867 }
9868
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009869 return true;
9870}
9871
Jan Kiszkaf4124502014-03-07 20:03:13 +01009872static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9873{
9874 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9875 struct vcpu_vmx *vmx = to_vmx(vcpu);
9876
9877 if (vcpu->arch.virtual_tsc_khz == 0)
9878 return;
9879
9880 /* Make sure short timeouts reliably trigger an immediate vmexit.
9881 * hrtimer_start does not guarantee this. */
9882 if (preemption_timeout <= 1) {
9883 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9884 return;
9885 }
9886
9887 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9888 preemption_timeout *= 1000000;
9889 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9890 hrtimer_start(&vmx->nested.preemption_timer,
9891 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9892}
9893
Wincy Van3af18d92015-02-03 23:49:31 +08009894static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9895 struct vmcs12 *vmcs12)
9896{
9897 int maxphyaddr;
9898 u64 addr;
9899
9900 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9901 return 0;
9902
9903 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9904 WARN_ON(1);
9905 return -EINVAL;
9906 }
9907 maxphyaddr = cpuid_maxphyaddr(vcpu);
9908
9909 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9910 ((addr + PAGE_SIZE) >> maxphyaddr))
9911 return -EINVAL;
9912
9913 return 0;
9914}
9915
9916/*
9917 * Merge L0's and L1's MSR bitmap, return false to indicate that
9918 * we do not use the hardware.
9919 */
9920static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9921 struct vmcs12 *vmcs12)
9922{
Wincy Van82f0dd42015-02-03 23:57:18 +08009923 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009924 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009925 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009926 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009927 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009928 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009929 *
9930 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9931 * ensures that we do not accidentally generate an L02 MSR bitmap
9932 * from the L12 MSR bitmap that is too permissive.
9933 * 2. That L1 or L2s have actually used the MSR. This avoids
9934 * unnecessarily merging of the bitmap if the MSR is unused. This
9935 * works properly because we only update the L01 MSR bitmap lazily.
9936 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9937 * updated to reflect this when L1 (or its L2s) actually write to
9938 * the MSR.
9939 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009940 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9941 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009942
Ashok Raj70131292018-02-01 22:59:43 +01009943 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009944 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009945 return false;
9946
9947 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009948 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009949 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009950 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009951
Radim Krčmářd048c092016-08-08 20:16:22 +02009952 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9953
Wincy Vanf2b93282015-02-03 23:56:03 +08009954 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009955 if (nested_cpu_has_apic_reg_virt(vmcs12))
9956 for (msr = 0x800; msr <= 0x8ff; msr++)
9957 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009958 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009959 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009960
9961 nested_vmx_disable_intercept_for_msr(
9962 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009963 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9964 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009965
Wincy Van608406e2015-02-03 23:57:51 +08009966 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009967 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009968 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009969 APIC_BASE_MSR + (APIC_EOI >> 4),
9970 MSR_TYPE_W);
9971 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009972 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009973 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9974 MSR_TYPE_W);
9975 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009976 }
Ashok Raj70131292018-02-01 22:59:43 +01009977
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009978 if (spec_ctrl)
9979 nested_vmx_disable_intercept_for_msr(
9980 msr_bitmap_l1, msr_bitmap_l0,
9981 MSR_IA32_SPEC_CTRL,
9982 MSR_TYPE_R | MSR_TYPE_W);
9983
Ashok Raj70131292018-02-01 22:59:43 +01009984 if (pred_cmd)
9985 nested_vmx_disable_intercept_for_msr(
9986 msr_bitmap_l1, msr_bitmap_l0,
9987 MSR_IA32_PRED_CMD,
9988 MSR_TYPE_W);
9989
Wincy Vanf2b93282015-02-03 23:56:03 +08009990 kunmap(page);
9991 nested_release_page_clean(page);
9992
9993 return true;
9994}
9995
9996static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9997 struct vmcs12 *vmcs12)
9998{
Wincy Van82f0dd42015-02-03 23:57:18 +08009999 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010000 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010001 !nested_cpu_has_vid(vmcs12) &&
10002 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010003 return 0;
10004
10005 /*
10006 * If virtualize x2apic mode is enabled,
10007 * virtualize apic access must be disabled.
10008 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010009 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10010 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010011 return -EINVAL;
10012
Wincy Van608406e2015-02-03 23:57:51 +080010013 /*
10014 * If virtual interrupt delivery is enabled,
10015 * we must exit on external interrupts.
10016 */
10017 if (nested_cpu_has_vid(vmcs12) &&
10018 !nested_exit_on_intr(vcpu))
10019 return -EINVAL;
10020
Wincy Van705699a2015-02-03 23:58:17 +080010021 /*
10022 * bits 15:8 should be zero in posted_intr_nv,
10023 * the descriptor address has been already checked
10024 * in nested_get_vmcs12_pages.
10025 */
10026 if (nested_cpu_has_posted_intr(vmcs12) &&
10027 (!nested_cpu_has_vid(vmcs12) ||
10028 !nested_exit_intr_ack_set(vcpu) ||
10029 vmcs12->posted_intr_nv & 0xff00))
10030 return -EINVAL;
10031
Wincy Vanf2b93282015-02-03 23:56:03 +080010032 /* tpr shadow is needed by all apicv features. */
10033 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10034 return -EINVAL;
10035
10036 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010037}
10038
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010039static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10040 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010041 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010042{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010043 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010044 u64 count, addr;
10045
10046 if (vmcs12_read_any(vcpu, count_field, &count) ||
10047 vmcs12_read_any(vcpu, addr_field, &addr)) {
10048 WARN_ON(1);
10049 return -EINVAL;
10050 }
10051 if (count == 0)
10052 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010053 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010054 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10055 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010056 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010057 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10058 addr_field, maxphyaddr, count, addr);
10059 return -EINVAL;
10060 }
10061 return 0;
10062}
10063
10064static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10065 struct vmcs12 *vmcs12)
10066{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010067 if (vmcs12->vm_exit_msr_load_count == 0 &&
10068 vmcs12->vm_exit_msr_store_count == 0 &&
10069 vmcs12->vm_entry_msr_load_count == 0)
10070 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010071 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010072 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010073 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010074 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010075 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010076 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010077 return -EINVAL;
10078 return 0;
10079}
10080
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010081static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10082 struct vmx_msr_entry *e)
10083{
10084 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010085 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010086 return -EINVAL;
10087 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10088 e->index == MSR_IA32_UCODE_REV)
10089 return -EINVAL;
10090 if (e->reserved != 0)
10091 return -EINVAL;
10092 return 0;
10093}
10094
10095static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10096 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010097{
10098 if (e->index == MSR_FS_BASE ||
10099 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010100 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10101 nested_vmx_msr_check_common(vcpu, e))
10102 return -EINVAL;
10103 return 0;
10104}
10105
10106static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10107 struct vmx_msr_entry *e)
10108{
10109 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10110 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010111 return -EINVAL;
10112 return 0;
10113}
10114
10115/*
10116 * Load guest's/host's msr at nested entry/exit.
10117 * return 0 for success, entry index for failure.
10118 */
10119static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10120{
10121 u32 i;
10122 struct vmx_msr_entry e;
10123 struct msr_data msr;
10124
10125 msr.host_initiated = false;
10126 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010127 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10128 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010129 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010130 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10131 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010132 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010133 }
10134 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010135 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010136 "%s check failed (%u, 0x%x, 0x%x)\n",
10137 __func__, i, e.index, e.reserved);
10138 goto fail;
10139 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010140 msr.index = e.index;
10141 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010142 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010143 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010144 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10145 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010146 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010147 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010148 }
10149 return 0;
10150fail:
10151 return i + 1;
10152}
10153
10154static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10155{
10156 u32 i;
10157 struct vmx_msr_entry e;
10158
10159 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010160 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010161 if (kvm_vcpu_read_guest(vcpu,
10162 gpa + i * sizeof(e),
10163 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010164 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010165 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10166 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010167 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010168 }
10169 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010170 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010171 "%s check failed (%u, 0x%x, 0x%x)\n",
10172 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010173 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010174 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010175 msr_info.host_initiated = false;
10176 msr_info.index = e.index;
10177 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010178 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010179 "%s cannot read MSR (%u, 0x%x)\n",
10180 __func__, i, e.index);
10181 return -EINVAL;
10182 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010183 if (kvm_vcpu_write_guest(vcpu,
10184 gpa + i * sizeof(e) +
10185 offsetof(struct vmx_msr_entry, value),
10186 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010187 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010188 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010189 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010190 return -EINVAL;
10191 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010192 }
10193 return 0;
10194}
10195
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010196/*
10197 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10198 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010199 * 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 +030010200 * guest in a way that will both be appropriate to L1's requests, and our
10201 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10202 * function also has additional necessary side-effects, like setting various
10203 * vcpu->arch fields.
10204 */
10205static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10206{
10207 struct vcpu_vmx *vmx = to_vmx(vcpu);
10208 u32 exec_control;
10209
10210 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10211 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10212 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10213 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10214 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10215 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10216 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10217 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10218 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10219 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10220 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10221 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10222 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10223 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10224 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10225 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10226 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10227 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10228 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10229 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10230 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10231 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10232 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10233 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10234 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10235 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10236 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10237 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10238 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10239 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10240 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10241 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10242 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10243 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10244 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10245 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10246
Jan Kiszka2996fca2014-06-16 13:59:43 +020010247 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10248 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10249 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10250 } else {
10251 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10252 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10253 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010254 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10255 vmcs12->vm_entry_intr_info_field);
10256 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10257 vmcs12->vm_entry_exception_error_code);
10258 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10259 vmcs12->vm_entry_instruction_len);
10260 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10261 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010262 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010263 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010264 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10265 vmcs12->guest_pending_dbg_exceptions);
10266 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10267 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10268
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010269 if (nested_cpu_has_xsaves(vmcs12))
10270 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010271 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10272
Jan Kiszkaf4124502014-03-07 20:03:13 +010010273 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010274
Paolo Bonzini93140062016-07-06 13:23:51 +020010275 /* Preemption timer setting is only taken from vmcs01. */
10276 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10277 exec_control |= vmcs_config.pin_based_exec_ctrl;
10278 if (vmx->hv_deadline_tsc == -1)
10279 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10280
10281 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010282 if (nested_cpu_has_posted_intr(vmcs12)) {
10283 /*
10284 * Note that we use L0's vector here and in
10285 * vmx_deliver_nested_posted_interrupt.
10286 */
10287 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10288 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010289 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010290 vmcs_write64(POSTED_INTR_DESC_ADDR,
10291 page_to_phys(vmx->nested.pi_desc_page) +
10292 (unsigned long)(vmcs12->posted_intr_desc_addr &
10293 (PAGE_SIZE - 1)));
10294 } else
10295 exec_control &= ~PIN_BASED_POSTED_INTR;
10296
Jan Kiszkaf4124502014-03-07 20:03:13 +010010297 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010298
Jan Kiszkaf4124502014-03-07 20:03:13 +010010299 vmx->nested.preemption_timer_expired = false;
10300 if (nested_cpu_has_preemption_timer(vmcs12))
10301 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010302
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010303 /*
10304 * Whether page-faults are trapped is determined by a combination of
10305 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10306 * If enable_ept, L0 doesn't care about page faults and we should
10307 * set all of these to L1's desires. However, if !enable_ept, L0 does
10308 * care about (at least some) page faults, and because it is not easy
10309 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10310 * to exit on each and every L2 page fault. This is done by setting
10311 * MASK=MATCH=0 and (see below) EB.PF=1.
10312 * Note that below we don't need special code to set EB.PF beyond the
10313 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10314 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10315 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10316 *
10317 * A problem with this approach (when !enable_ept) is that L1 may be
10318 * injected with more page faults than it asked for. This could have
10319 * caused problems, but in practice existing hypervisors don't care.
10320 * To fix this, we will need to emulate the PFEC checking (on the L1
10321 * page tables), using walk_addr(), when injecting PFs to L1.
10322 */
10323 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10324 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10325 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10326 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10327
10328 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010329 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010330
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010331 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010332 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010333 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010334 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010335 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010336 if (nested_cpu_has(vmcs12,
10337 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10338 exec_control |= vmcs12->secondary_vm_exec_control;
10339
10340 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10341 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010342 * If translation failed, no matter: This feature asks
10343 * to exit when accessing the given address, and if it
10344 * can never be accessed, this feature won't do
10345 * anything anyway.
10346 */
10347 if (!vmx->nested.apic_access_page)
10348 exec_control &=
10349 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10350 else
10351 vmcs_write64(APIC_ACCESS_ADDR,
10352 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010353 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010354 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010355 exec_control |=
10356 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010357 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010358 }
10359
Wincy Van608406e2015-02-03 23:57:51 +080010360 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10361 vmcs_write64(EOI_EXIT_BITMAP0,
10362 vmcs12->eoi_exit_bitmap0);
10363 vmcs_write64(EOI_EXIT_BITMAP1,
10364 vmcs12->eoi_exit_bitmap1);
10365 vmcs_write64(EOI_EXIT_BITMAP2,
10366 vmcs12->eoi_exit_bitmap2);
10367 vmcs_write64(EOI_EXIT_BITMAP3,
10368 vmcs12->eoi_exit_bitmap3);
10369 vmcs_write16(GUEST_INTR_STATUS,
10370 vmcs12->guest_intr_status);
10371 }
10372
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010373 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10374 }
10375
10376
10377 /*
10378 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10379 * Some constant fields are set here by vmx_set_constant_host_state().
10380 * Other fields are different per CPU, and will be set later when
10381 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10382 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010383 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010384
10385 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010386 * Set the MSR load/store lists to match L0's settings.
10387 */
10388 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010389 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10390 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10391 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10392 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010393
10394 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010395 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10396 * entry, but only if the current (host) sp changed from the value
10397 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10398 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10399 * here we just force the write to happen on entry.
10400 */
10401 vmx->host_rsp = 0;
10402
10403 exec_control = vmx_exec_control(vmx); /* L0's desires */
10404 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10405 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10406 exec_control &= ~CPU_BASED_TPR_SHADOW;
10407 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010408
10409 if (exec_control & CPU_BASED_TPR_SHADOW) {
10410 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10411 page_to_phys(vmx->nested.virtual_apic_page));
10412 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010413 } else {
10414#ifdef CONFIG_X86_64
10415 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10416 CPU_BASED_CR8_STORE_EXITING;
10417#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010418 }
10419
Wincy Van3af18d92015-02-03 23:49:31 +080010420 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010421 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10422 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10423 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10424 else
Wincy Van3af18d92015-02-03 23:49:31 +080010425 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10426
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010427 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010428 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010429 * Rather, exit every time.
10430 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010431 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10432 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10433
10434 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10435
10436 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10437 * bitwise-or of what L1 wants to trap for L2, and what we want to
10438 * trap. Note that CR0.TS also needs updating - we do this later.
10439 */
10440 update_exception_bitmap(vcpu);
10441 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10442 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10443
Nadav Har'El8049d652013-08-05 11:07:06 +030010444 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10445 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10446 * bits are further modified by vmx_set_efer() below.
10447 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010448 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010449
10450 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10451 * emulated by vmx_set_efer(), below.
10452 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010453 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010454 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10455 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010456 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10457
Jan Kiszka44811c02013-08-04 17:17:27 +020010458 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010459 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010460 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10461 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010462 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10463
10464
10465 set_cr4_guest_host_mask(vmx);
10466
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010467 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10468 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10469
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010470 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10471 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010472 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010473 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010474 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010475 if (kvm_has_tsc_control)
10476 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010477
Paolo Bonzini6236b782018-01-16 16:51:18 +010010478 if (cpu_has_vmx_msr_bitmap())
10479 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10480
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010481 if (enable_vpid) {
10482 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010483 * There is no direct mapping between vpid02 and vpid12, the
10484 * vpid02 is per-vCPU for L0 and reused while the value of
10485 * vpid12 is changed w/ one invvpid during nested vmentry.
10486 * The vpid12 is allocated by L1 for L2, so it will not
10487 * influence global bitmap(for vpid01 and vpid02 allocation)
10488 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010489 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010490 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10491 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10492 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10493 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10494 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10495 }
10496 } else {
10497 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10498 vmx_flush_tlb(vcpu);
10499 }
10500
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010501 }
10502
Ladi Prosek560a9792017-04-04 14:18:53 +020010503 if (enable_pml) {
10504 /*
10505 * Conceptually we want to copy the PML address and index from
10506 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10507 * since we always flush the log on each vmexit, this happens
10508 * to be equivalent to simply resetting the fields in vmcs02.
10509 */
10510 ASSERT(vmx->pml_pg);
10511 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10512 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10513 }
10514
Nadav Har'El155a97a2013-08-05 11:07:16 +030010515 if (nested_cpu_has_ept(vmcs12)) {
10516 kvm_mmu_unload(vcpu);
10517 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010518 } else if (nested_cpu_has2(vmcs12,
10519 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10520 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010521 }
10522
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010523 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10524 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010525 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010526 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10527 else
10528 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10529 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10530 vmx_set_efer(vcpu, vcpu->arch.efer);
10531
10532 /*
10533 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10534 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10535 * The CR0_READ_SHADOW is what L2 should have expected to read given
10536 * the specifications by L1; It's not enough to take
10537 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10538 * have more bits than L1 expected.
10539 */
10540 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10541 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10542
10543 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10544 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10545
10546 /* shadow page tables on either EPT or shadow page tables */
10547 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10548 kvm_mmu_reset_context(vcpu);
10549
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010550 if (!enable_ept)
10551 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10552
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010553 /*
10554 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10555 */
10556 if (enable_ept) {
10557 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10558 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10559 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10560 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10561 }
10562
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010563 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10564 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10565}
10566
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010567/*
10568 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10569 * for running an L2 nested guest.
10570 */
10571static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10572{
10573 struct vmcs12 *vmcs12;
10574 struct vcpu_vmx *vmx = to_vmx(vcpu);
10575 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010576 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010577 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010578
10579 if (!nested_vmx_check_permission(vcpu) ||
10580 !nested_vmx_check_vmcs12(vcpu))
10581 return 1;
10582
10583 skip_emulated_instruction(vcpu);
10584 vmcs12 = get_vmcs12(vcpu);
10585
Abel Gordon012f83c2013-04-18 14:39:25 +030010586 if (enable_shadow_vmcs)
10587 copy_shadow_to_vmcs12(vmx);
10588
Nadav Har'El7c177932011-05-25 23:12:04 +030010589 /*
10590 * The nested entry process starts with enforcing various prerequisites
10591 * on vmcs12 as required by the Intel SDM, and act appropriately when
10592 * they fail: As the SDM explains, some conditions should cause the
10593 * instruction to fail, while others will cause the instruction to seem
10594 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10595 * To speed up the normal (success) code path, we should avoid checking
10596 * for misconfigurations which will anyway be caught by the processor
10597 * when using the merged vmcs02.
10598 */
10599 if (vmcs12->launch_state == launch) {
10600 nested_vmx_failValid(vcpu,
10601 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10602 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10603 return 1;
10604 }
10605
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010606 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10607 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010608 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10609 return 1;
10610 }
10611
Wincy Van3af18d92015-02-03 23:49:31 +080010612 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010613 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10614 return 1;
10615 }
10616
Wincy Van3af18d92015-02-03 23:49:31 +080010617 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010618 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10619 return 1;
10620 }
10621
Wincy Vanf2b93282015-02-03 23:56:03 +080010622 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10623 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10624 return 1;
10625 }
10626
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010627 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10628 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10629 return 1;
10630 }
10631
Nadav Har'El7c177932011-05-25 23:12:04 +030010632 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010633 vmx->nested.nested_vmx_true_procbased_ctls_low,
10634 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010635 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010636 vmx->nested.nested_vmx_secondary_ctls_low,
10637 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010638 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010639 vmx->nested.nested_vmx_pinbased_ctls_low,
10640 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010641 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010642 vmx->nested.nested_vmx_true_exit_ctls_low,
10643 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010644 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010645 vmx->nested.nested_vmx_true_entry_ctls_low,
10646 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010647 {
10648 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10649 return 1;
10650 }
10651
10652 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10653 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10654 nested_vmx_failValid(vcpu,
10655 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10656 return 1;
10657 }
10658
Wincy Vanb9c237b2015-02-03 23:56:30 +080010659 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010660 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10661 nested_vmx_entry_failure(vcpu, vmcs12,
10662 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10663 return 1;
10664 }
10665 if (vmcs12->vmcs_link_pointer != -1ull) {
10666 nested_vmx_entry_failure(vcpu, vmcs12,
10667 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10668 return 1;
10669 }
10670
10671 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010672 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010673 * are performed on the field for the IA32_EFER MSR:
10674 * - Bits reserved in the IA32_EFER MSR must be 0.
10675 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10676 * the IA-32e mode guest VM-exit control. It must also be identical
10677 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10678 * CR0.PG) is 1.
10679 */
10680 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10681 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10682 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10683 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10684 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10685 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10686 nested_vmx_entry_failure(vcpu, vmcs12,
10687 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10688 return 1;
10689 }
10690 }
10691
10692 /*
10693 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10694 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10695 * the values of the LMA and LME bits in the field must each be that of
10696 * the host address-space size VM-exit control.
10697 */
10698 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10699 ia32e = (vmcs12->vm_exit_controls &
10700 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10701 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10702 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10703 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10704 nested_vmx_entry_failure(vcpu, vmcs12,
10705 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10706 return 1;
10707 }
10708 }
10709
10710 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010711 * We're finally done with prerequisite checking, and can start with
10712 * the nested entry.
10713 */
10714
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010715 enter_guest_mode(vcpu);
10716
Jan Kiszka2996fca2014-06-16 13:59:43 +020010717 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10718 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10719
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010720 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010721 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010722 vmx_vcpu_put(vcpu);
10723 vmx_vcpu_load(vcpu, cpu);
10724 vcpu->cpu = cpu;
10725 put_cpu();
10726
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010727 vmx_segment_cache_clear(vmx);
10728
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010729 prepare_vmcs02(vcpu, vmcs12);
10730
Wincy Vanff651cb2014-12-11 08:52:58 +030010731 msr_entry_idx = nested_vmx_load_msr(vcpu,
10732 vmcs12->vm_entry_msr_load_addr,
10733 vmcs12->vm_entry_msr_load_count);
10734 if (msr_entry_idx) {
10735 leave_guest_mode(vcpu);
10736 vmx_load_vmcs01(vcpu);
10737 nested_vmx_entry_failure(vcpu, vmcs12,
10738 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10739 return 1;
10740 }
10741
10742 vmcs12->launch_state = 1;
10743
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010744 /* Hide L1D cache contents from the nested guest. */
10745 vmx->vcpu.arch.l1tf_flush_l1d = true;
10746
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010747 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010748 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010749
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010750 vmx->nested.nested_run_pending = 1;
10751
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010752 /*
10753 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10754 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10755 * returned as far as L1 is concerned. It will only return (and set
10756 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10757 */
10758 return 1;
10759}
10760
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010761/*
10762 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10763 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10764 * This function returns the new value we should put in vmcs12.guest_cr0.
10765 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10766 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10767 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10768 * didn't trap the bit, because if L1 did, so would L0).
10769 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10770 * been modified by L2, and L1 knows it. So just leave the old value of
10771 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10772 * isn't relevant, because if L0 traps this bit it can set it to anything.
10773 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10774 * changed these bits, and therefore they need to be updated, but L0
10775 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10776 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10777 */
10778static inline unsigned long
10779vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10780{
10781 return
10782 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10783 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10784 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10785 vcpu->arch.cr0_guest_owned_bits));
10786}
10787
10788static inline unsigned long
10789vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10790{
10791 return
10792 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10793 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10794 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10795 vcpu->arch.cr4_guest_owned_bits));
10796}
10797
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010798static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10799 struct vmcs12 *vmcs12)
10800{
10801 u32 idt_vectoring;
10802 unsigned int nr;
10803
Gleb Natapov851eb6672013-09-25 12:51:34 +030010804 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010805 nr = vcpu->arch.exception.nr;
10806 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10807
10808 if (kvm_exception_is_soft(nr)) {
10809 vmcs12->vm_exit_instruction_len =
10810 vcpu->arch.event_exit_inst_len;
10811 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10812 } else
10813 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10814
10815 if (vcpu->arch.exception.has_error_code) {
10816 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10817 vmcs12->idt_vectoring_error_code =
10818 vcpu->arch.exception.error_code;
10819 }
10820
10821 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010822 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010823 vmcs12->idt_vectoring_info_field =
10824 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10825 } else if (vcpu->arch.interrupt.pending) {
10826 nr = vcpu->arch.interrupt.nr;
10827 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10828
10829 if (vcpu->arch.interrupt.soft) {
10830 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10831 vmcs12->vm_entry_instruction_len =
10832 vcpu->arch.event_exit_inst_len;
10833 } else
10834 idt_vectoring |= INTR_TYPE_EXT_INTR;
10835
10836 vmcs12->idt_vectoring_info_field = idt_vectoring;
10837 }
10838}
10839
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010840static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10841{
10842 struct vcpu_vmx *vmx = to_vmx(vcpu);
10843
Jan Kiszkaf4124502014-03-07 20:03:13 +010010844 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10845 vmx->nested.preemption_timer_expired) {
10846 if (vmx->nested.nested_run_pending)
10847 return -EBUSY;
10848 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10849 return 0;
10850 }
10851
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010852 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010853 if (vmx->nested.nested_run_pending ||
10854 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010855 return -EBUSY;
10856 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10857 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10858 INTR_INFO_VALID_MASK, 0);
10859 /*
10860 * The NMI-triggered VM exit counts as injection:
10861 * clear this one and block further NMIs.
10862 */
10863 vcpu->arch.nmi_pending = 0;
10864 vmx_set_nmi_mask(vcpu, true);
10865 return 0;
10866 }
10867
10868 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10869 nested_exit_on_intr(vcpu)) {
10870 if (vmx->nested.nested_run_pending)
10871 return -EBUSY;
10872 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010873 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010874 }
10875
David Hildenbrand1edccf22017-01-25 11:58:58 +010010876 vmx_complete_nested_posted_interrupt(vcpu);
10877 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010878}
10879
Jan Kiszkaf4124502014-03-07 20:03:13 +010010880static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10881{
10882 ktime_t remaining =
10883 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10884 u64 value;
10885
10886 if (ktime_to_ns(remaining) <= 0)
10887 return 0;
10888
10889 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10890 do_div(value, 1000000);
10891 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10892}
10893
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010894/*
10895 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10896 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10897 * and this function updates it to reflect the changes to the guest state while
10898 * L2 was running (and perhaps made some exits which were handled directly by L0
10899 * without going back to L1), and to reflect the exit reason.
10900 * Note that we do not have to copy here all VMCS fields, just those that
10901 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10902 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10903 * which already writes to vmcs12 directly.
10904 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010905static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10906 u32 exit_reason, u32 exit_intr_info,
10907 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010908{
10909 /* update guest state fields: */
10910 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10911 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10912
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010913 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10914 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10915 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10916
10917 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10918 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10919 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10920 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10921 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10922 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10923 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10924 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10925 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10926 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10927 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10928 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10929 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10930 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10931 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10932 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10933 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10934 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10935 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10936 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10937 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10938 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10939 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10940 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10941 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10942 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10943 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10944 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10945 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10946 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10947 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10948 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10949 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10950 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10951 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10952 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10953
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010954 vmcs12->guest_interruptibility_info =
10955 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10956 vmcs12->guest_pending_dbg_exceptions =
10957 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010958 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10959 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10960 else
10961 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010962
Jan Kiszkaf4124502014-03-07 20:03:13 +010010963 if (nested_cpu_has_preemption_timer(vmcs12)) {
10964 if (vmcs12->vm_exit_controls &
10965 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10966 vmcs12->vmx_preemption_timer_value =
10967 vmx_get_preemption_timer_value(vcpu);
10968 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10969 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010970
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010971 /*
10972 * In some cases (usually, nested EPT), L2 is allowed to change its
10973 * own CR3 without exiting. If it has changed it, we must keep it.
10974 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10975 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10976 *
10977 * Additionally, restore L2's PDPTR to vmcs12.
10978 */
10979 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010980 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010981 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10982 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10983 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10984 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10985 }
10986
Jim Mattson4933e9f2017-06-01 12:44:46 -070010987 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010988
Wincy Van608406e2015-02-03 23:57:51 +080010989 if (nested_cpu_has_vid(vmcs12))
10990 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10991
Jan Kiszkac18911a2013-03-13 16:06:41 +010010992 vmcs12->vm_entry_controls =
10993 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010994 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010995
Jan Kiszka2996fca2014-06-16 13:59:43 +020010996 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10997 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10998 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10999 }
11000
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011001 /* TODO: These cannot have changed unless we have MSR bitmaps and
11002 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011003 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011004 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011005 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11006 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011007 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11008 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11009 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011010 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011011 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080011012 if (nested_cpu_has_xsaves(vmcs12))
11013 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011014
11015 /* update exit information fields: */
11016
Jan Kiszka533558b2014-01-04 18:47:20 +010011017 vmcs12->vm_exit_reason = exit_reason;
11018 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011019
Jan Kiszka533558b2014-01-04 18:47:20 +010011020 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020011021 if ((vmcs12->vm_exit_intr_info &
11022 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
11023 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
11024 vmcs12->vm_exit_intr_error_code =
11025 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011026 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011027 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11028 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11029
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011030 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11031 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11032 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011033 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011034
11035 /*
11036 * Transfer the event that L0 or L1 may wanted to inject into
11037 * L2 to IDT_VECTORING_INFO_FIELD.
11038 */
11039 vmcs12_save_pending_event(vcpu, vmcs12);
11040 }
11041
11042 /*
11043 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11044 * preserved above and would only end up incorrectly in L1.
11045 */
11046 vcpu->arch.nmi_injected = false;
11047 kvm_clear_exception_queue(vcpu);
11048 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011049}
11050
11051/*
11052 * A part of what we need to when the nested L2 guest exits and we want to
11053 * run its L1 parent, is to reset L1's guest state to the host state specified
11054 * in vmcs12.
11055 * This function is to be called not only on normal nested exit, but also on
11056 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11057 * Failures During or After Loading Guest State").
11058 * This function should be called when the active VMCS is L1's (vmcs01).
11059 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011060static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11061 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011062{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011063 struct kvm_segment seg;
11064
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011065 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11066 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011067 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011068 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11069 else
11070 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11071 vmx_set_efer(vcpu, vcpu->arch.efer);
11072
11073 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11074 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011075 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011076 /*
11077 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11078 * actually changed, because it depends on the current state of
11079 * fpu_active (which may have changed).
11080 * Note that vmx_set_cr0 refers to efer set above.
11081 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011082 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011083 /*
11084 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
11085 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
11086 * but we also need to update cr0_guest_host_mask and exception_bitmap.
11087 */
11088 update_exception_bitmap(vcpu);
11089 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
11090 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11091
11092 /*
11093 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
11094 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
11095 */
11096 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080011097 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011098
Jan Kiszka29bf08f2013-12-28 16:31:52 +010011099 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011100
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011101 kvm_set_cr3(vcpu, vmcs12->host_cr3);
11102 kvm_mmu_reset_context(vcpu);
11103
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011104 if (!enable_ept)
11105 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11106
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011107 if (enable_vpid) {
11108 /*
11109 * Trivially support vpid by letting L2s share their parent
11110 * L1's vpid. TODO: move to a more elaborate solution, giving
11111 * each L2 its own vpid and exposing the vpid feature to L1.
11112 */
11113 vmx_flush_tlb(vcpu);
11114 }
11115
11116
11117 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11118 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11119 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11120 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11121 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020011122 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11123 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011124
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011125 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11126 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11127 vmcs_write64(GUEST_BNDCFGS, 0);
11128
Jan Kiszka44811c02013-08-04 17:17:27 +020011129 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011130 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011131 vcpu->arch.pat = vmcs12->host_ia32_pat;
11132 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011133 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11134 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11135 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011136
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011137 /* Set L1 segment info according to Intel SDM
11138 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11139 seg = (struct kvm_segment) {
11140 .base = 0,
11141 .limit = 0xFFFFFFFF,
11142 .selector = vmcs12->host_cs_selector,
11143 .type = 11,
11144 .present = 1,
11145 .s = 1,
11146 .g = 1
11147 };
11148 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11149 seg.l = 1;
11150 else
11151 seg.db = 1;
11152 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11153 seg = (struct kvm_segment) {
11154 .base = 0,
11155 .limit = 0xFFFFFFFF,
11156 .type = 3,
11157 .present = 1,
11158 .s = 1,
11159 .db = 1,
11160 .g = 1
11161 };
11162 seg.selector = vmcs12->host_ds_selector;
11163 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11164 seg.selector = vmcs12->host_es_selector;
11165 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11166 seg.selector = vmcs12->host_ss_selector;
11167 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11168 seg.selector = vmcs12->host_fs_selector;
11169 seg.base = vmcs12->host_fs_base;
11170 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11171 seg.selector = vmcs12->host_gs_selector;
11172 seg.base = vmcs12->host_gs_base;
11173 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11174 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011175 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011176 .limit = 0x67,
11177 .selector = vmcs12->host_tr_selector,
11178 .type = 11,
11179 .present = 1
11180 };
11181 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11182
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011183 kvm_set_dr(vcpu, 7, 0x400);
11184 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011185
Wincy Van3af18d92015-02-03 23:49:31 +080011186 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011187 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011188
Wincy Vanff651cb2014-12-11 08:52:58 +030011189 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11190 vmcs12->vm_exit_msr_load_count))
11191 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011192}
11193
11194/*
11195 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11196 * and modify vmcs12 to make it see what it would expect to see there if
11197 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11198 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011199static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11200 u32 exit_intr_info,
11201 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011202{
11203 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011204 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11205
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011206 /* trying to cancel vmlaunch/vmresume is a bug */
11207 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11208
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011209 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011210 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11211 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011212
Wincy Vanff651cb2014-12-11 08:52:58 +030011213 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11214 vmcs12->vm_exit_msr_store_count))
11215 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11216
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011217 vmx_load_vmcs01(vcpu);
11218
Bandan Das77b0f5d2014-04-19 18:17:45 -040011219 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11220 && nested_exit_intr_ack_set(vcpu)) {
11221 int irq = kvm_cpu_get_interrupt(vcpu);
11222 WARN_ON(irq < 0);
11223 vmcs12->vm_exit_intr_info = irq |
11224 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11225 }
11226
Jan Kiszka542060e2014-01-04 18:47:21 +010011227 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11228 vmcs12->exit_qualification,
11229 vmcs12->idt_vectoring_info_field,
11230 vmcs12->vm_exit_intr_info,
11231 vmcs12->vm_exit_intr_error_code,
11232 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011233
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011234 vm_entry_controls_reset_shadow(vmx);
11235 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011236 vmx_segment_cache_clear(vmx);
11237
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011238 load_vmcs12_host_state(vcpu, vmcs12);
11239
Paolo Bonzini93140062016-07-06 13:23:51 +020011240 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011241 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11242 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011243 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011244 if (vmx->hv_deadline_tsc == -1)
11245 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11246 PIN_BASED_VMX_PREEMPTION_TIMER);
11247 else
11248 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11249 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011250 if (kvm_has_tsc_control)
11251 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011252
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011253 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11254 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11255 vmx_set_virtual_x2apic_mode(vcpu,
11256 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011257 } else if (!nested_cpu_has_ept(vmcs12) &&
11258 nested_cpu_has2(vmcs12,
11259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11260 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011261 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011262
11263 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11264 vmx->host_rsp = 0;
11265
11266 /* Unpin physical memory we referred to in vmcs02 */
11267 if (vmx->nested.apic_access_page) {
11268 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011269 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011270 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011271 if (vmx->nested.virtual_apic_page) {
11272 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011273 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011274 }
Wincy Van705699a2015-02-03 23:58:17 +080011275 if (vmx->nested.pi_desc_page) {
11276 kunmap(vmx->nested.pi_desc_page);
11277 nested_release_page(vmx->nested.pi_desc_page);
11278 vmx->nested.pi_desc_page = NULL;
11279 vmx->nested.pi_desc = NULL;
11280 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011281
11282 /*
Tang Chen38b99172014-09-24 15:57:54 +080011283 * We are now running in L2, mmu_notifier will force to reload the
11284 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11285 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011286 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011287
11288 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011289 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11290 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11291 * success or failure flag accordingly.
11292 */
11293 if (unlikely(vmx->fail)) {
11294 vmx->fail = 0;
11295 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11296 } else
11297 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011298 if (enable_shadow_vmcs)
11299 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011300
11301 /* in case we halted in L2 */
11302 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011303}
11304
Nadav Har'El7c177932011-05-25 23:12:04 +030011305/*
Jan Kiszka42124922014-01-04 18:47:19 +010011306 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11307 */
11308static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11309{
Wanpeng Lic886f282017-03-06 04:03:28 -080011310 if (is_guest_mode(vcpu)) {
11311 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011312 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011313 }
Jan Kiszka42124922014-01-04 18:47:19 +010011314 free_nested(to_vmx(vcpu));
11315}
11316
11317/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011318 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11319 * 23.7 "VM-entry failures during or after loading guest state" (this also
11320 * lists the acceptable exit-reason and exit-qualification parameters).
11321 * It should only be called before L2 actually succeeded to run, and when
11322 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11323 */
11324static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11325 struct vmcs12 *vmcs12,
11326 u32 reason, unsigned long qualification)
11327{
11328 load_vmcs12_host_state(vcpu, vmcs12);
11329 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11330 vmcs12->exit_qualification = qualification;
11331 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011332 if (enable_shadow_vmcs)
11333 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011334}
11335
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011336static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11337 struct x86_instruction_info *info,
11338 enum x86_intercept_stage stage)
11339{
11340 return X86EMUL_CONTINUE;
11341}
11342
Yunhong Jiang64672c92016-06-13 14:19:59 -070011343#ifdef CONFIG_X86_64
11344/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11345static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11346 u64 divisor, u64 *result)
11347{
11348 u64 low = a << shift, high = a >> (64 - shift);
11349
11350 /* To avoid the overflow on divq */
11351 if (high >= divisor)
11352 return 1;
11353
11354 /* Low hold the result, high hold rem which is discarded */
11355 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11356 "rm" (divisor), "0" (low), "1" (high));
11357 *result = low;
11358
11359 return 0;
11360}
11361
11362static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11363{
11364 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011365 u64 tscl = rdtsc();
11366 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11367 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011368
11369 /* Convert to host delta tsc if tsc scaling is enabled */
11370 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11371 u64_shl_div_u64(delta_tsc,
11372 kvm_tsc_scaling_ratio_frac_bits,
11373 vcpu->arch.tsc_scaling_ratio,
11374 &delta_tsc))
11375 return -ERANGE;
11376
11377 /*
11378 * If the delta tsc can't fit in the 32 bit after the multi shift,
11379 * we can't use the preemption timer.
11380 * It's possible that it fits on later vmentries, but checking
11381 * on every vmentry is costly so we just use an hrtimer.
11382 */
11383 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11384 return -ERANGE;
11385
11386 vmx->hv_deadline_tsc = tscl + delta_tsc;
11387 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11388 PIN_BASED_VMX_PREEMPTION_TIMER);
11389 return 0;
11390}
11391
11392static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11393{
11394 struct vcpu_vmx *vmx = to_vmx(vcpu);
11395 vmx->hv_deadline_tsc = -1;
11396 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11397 PIN_BASED_VMX_PREEMPTION_TIMER);
11398}
11399#endif
11400
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011401static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011402{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011403 if (ple_gap)
11404 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011405}
11406
Kai Huang843e4332015-01-28 10:54:28 +080011407static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11408 struct kvm_memory_slot *slot)
11409{
11410 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11411 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11412}
11413
11414static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11415 struct kvm_memory_slot *slot)
11416{
11417 kvm_mmu_slot_set_dirty(kvm, slot);
11418}
11419
11420static void vmx_flush_log_dirty(struct kvm *kvm)
11421{
11422 kvm_flush_pml_buffers(kvm);
11423}
11424
11425static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11426 struct kvm_memory_slot *memslot,
11427 gfn_t offset, unsigned long mask)
11428{
11429 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11430}
11431
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011432static void __pi_post_block(struct kvm_vcpu *vcpu)
11433{
11434 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11435 struct pi_desc old, new;
11436 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011437
11438 do {
11439 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011440 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11441 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011442
11443 dest = cpu_physical_id(vcpu->cpu);
11444
11445 if (x2apic_enabled())
11446 new.ndst = dest;
11447 else
11448 new.ndst = (dest << 8) & 0xFF00;
11449
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011450 /* set 'NV' to 'notification vector' */
11451 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011452 } while (cmpxchg64(&pi_desc->control, old.control,
11453 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011454
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011455 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11456 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011457 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011458 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011459 vcpu->pre_pcpu = -1;
11460 }
11461}
11462
Feng Wuefc64402015-09-18 22:29:51 +080011463/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011464 * This routine does the following things for vCPU which is going
11465 * to be blocked if VT-d PI is enabled.
11466 * - Store the vCPU to the wakeup list, so when interrupts happen
11467 * we can find the right vCPU to wake up.
11468 * - Change the Posted-interrupt descriptor as below:
11469 * 'NDST' <-- vcpu->pre_pcpu
11470 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11471 * - If 'ON' is set during this process, which means at least one
11472 * interrupt is posted for this vCPU, we cannot block it, in
11473 * this case, return 1, otherwise, return 0.
11474 *
11475 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011476static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011477{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011478 unsigned int dest;
11479 struct pi_desc old, new;
11480 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11481
11482 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011483 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11484 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011485 return 0;
11486
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011487 WARN_ON(irqs_disabled());
11488 local_irq_disable();
11489 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11490 vcpu->pre_pcpu = vcpu->cpu;
11491 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11492 list_add_tail(&vcpu->blocked_vcpu_list,
11493 &per_cpu(blocked_vcpu_on_cpu,
11494 vcpu->pre_pcpu));
11495 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11496 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011497
11498 do {
11499 old.control = new.control = pi_desc->control;
11500
Feng Wubf9f6ac2015-09-18 22:29:55 +080011501 WARN((pi_desc->sn == 1),
11502 "Warning: SN field of posted-interrupts "
11503 "is set before blocking\n");
11504
11505 /*
11506 * Since vCPU can be preempted during this process,
11507 * vcpu->cpu could be different with pre_pcpu, we
11508 * need to set pre_pcpu as the destination of wakeup
11509 * notification event, then we can find the right vCPU
11510 * to wakeup in wakeup handler if interrupts happen
11511 * when the vCPU is in blocked state.
11512 */
11513 dest = cpu_physical_id(vcpu->pre_pcpu);
11514
11515 if (x2apic_enabled())
11516 new.ndst = dest;
11517 else
11518 new.ndst = (dest << 8) & 0xFF00;
11519
11520 /* set 'NV' to 'wakeup vector' */
11521 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011522 } while (cmpxchg64(&pi_desc->control, old.control,
11523 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011524
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011525 /* We should not block the vCPU if an interrupt is posted for it. */
11526 if (pi_test_on(pi_desc) == 1)
11527 __pi_post_block(vcpu);
11528
11529 local_irq_enable();
11530 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011531}
11532
Yunhong Jiangbc225122016-06-13 14:19:58 -070011533static int vmx_pre_block(struct kvm_vcpu *vcpu)
11534{
11535 if (pi_pre_block(vcpu))
11536 return 1;
11537
Yunhong Jiang64672c92016-06-13 14:19:59 -070011538 if (kvm_lapic_hv_timer_in_use(vcpu))
11539 kvm_lapic_switch_to_sw_timer(vcpu);
11540
Yunhong Jiangbc225122016-06-13 14:19:58 -070011541 return 0;
11542}
11543
11544static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011545{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011546 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011547 return;
11548
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011549 WARN_ON(irqs_disabled());
11550 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011551 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011552 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011553}
11554
Yunhong Jiangbc225122016-06-13 14:19:58 -070011555static void vmx_post_block(struct kvm_vcpu *vcpu)
11556{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011557 if (kvm_x86_ops->set_hv_timer)
11558 kvm_lapic_switch_to_hv_timer(vcpu);
11559
Yunhong Jiangbc225122016-06-13 14:19:58 -070011560 pi_post_block(vcpu);
11561}
11562
Feng Wubf9f6ac2015-09-18 22:29:55 +080011563/*
Feng Wuefc64402015-09-18 22:29:51 +080011564 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11565 *
11566 * @kvm: kvm
11567 * @host_irq: host irq of the interrupt
11568 * @guest_irq: gsi of the interrupt
11569 * @set: set or unset PI
11570 * returns 0 on success, < 0 on failure
11571 */
11572static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11573 uint32_t guest_irq, bool set)
11574{
11575 struct kvm_kernel_irq_routing_entry *e;
11576 struct kvm_irq_routing_table *irq_rt;
11577 struct kvm_lapic_irq irq;
11578 struct kvm_vcpu *vcpu;
11579 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011580 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011581
11582 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011583 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11584 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011585 return 0;
11586
11587 idx = srcu_read_lock(&kvm->irq_srcu);
11588 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011589 if (guest_irq >= irq_rt->nr_rt_entries ||
11590 hlist_empty(&irq_rt->map[guest_irq])) {
11591 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11592 guest_irq, irq_rt->nr_rt_entries);
11593 goto out;
11594 }
Feng Wuefc64402015-09-18 22:29:51 +080011595
11596 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11597 if (e->type != KVM_IRQ_ROUTING_MSI)
11598 continue;
11599 /*
11600 * VT-d PI cannot support posting multicast/broadcast
11601 * interrupts to a vCPU, we still use interrupt remapping
11602 * for these kind of interrupts.
11603 *
11604 * For lowest-priority interrupts, we only support
11605 * those with single CPU as the destination, e.g. user
11606 * configures the interrupts via /proc/irq or uses
11607 * irqbalance to make the interrupts single-CPU.
11608 *
11609 * We will support full lowest-priority interrupt later.
11610 */
11611
Radim Krčmář371313132016-07-12 22:09:27 +020011612 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011613 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11614 /*
11615 * Make sure the IRTE is in remapped mode if
11616 * we don't handle it in posted mode.
11617 */
11618 ret = irq_set_vcpu_affinity(host_irq, NULL);
11619 if (ret < 0) {
11620 printk(KERN_INFO
11621 "failed to back to remapped mode, irq: %u\n",
11622 host_irq);
11623 goto out;
11624 }
11625
Feng Wuefc64402015-09-18 22:29:51 +080011626 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011627 }
Feng Wuefc64402015-09-18 22:29:51 +080011628
11629 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11630 vcpu_info.vector = irq.vector;
11631
Feng Wub6ce9782016-01-25 16:53:35 +080011632 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011633 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11634
11635 if (set)
11636 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011637 else
Feng Wuefc64402015-09-18 22:29:51 +080011638 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011639
11640 if (ret < 0) {
11641 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11642 __func__);
11643 goto out;
11644 }
11645 }
11646
11647 ret = 0;
11648out:
11649 srcu_read_unlock(&kvm->irq_srcu, idx);
11650 return ret;
11651}
11652
Ashok Rajc45dcc72016-06-22 14:59:56 +080011653static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11654{
11655 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11656 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11657 FEATURE_CONTROL_LMCE;
11658 else
11659 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11660 ~FEATURE_CONTROL_LMCE;
11661}
11662
Kees Cook404f6aa2016-08-08 16:29:06 -070011663static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011664 .cpu_has_kvm_support = cpu_has_kvm_support,
11665 .disabled_by_bios = vmx_disabled_by_bios,
11666 .hardware_setup = hardware_setup,
11667 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011668 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011669 .hardware_enable = hardware_enable,
11670 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011671 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011672 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011673
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011674 .vm_init = vmx_vm_init,
11675
Avi Kivity6aa8b732006-12-10 02:21:36 -080011676 .vcpu_create = vmx_create_vcpu,
11677 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011678 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011679
Avi Kivity04d2cc72007-09-10 18:10:54 +030011680 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011681 .vcpu_load = vmx_vcpu_load,
11682 .vcpu_put = vmx_vcpu_put,
11683
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011684 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky62d88fc2018-02-21 13:39:51 -060011685 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011686 .get_msr = vmx_get_msr,
11687 .set_msr = vmx_set_msr,
11688 .get_segment_base = vmx_get_segment_base,
11689 .get_segment = vmx_get_segment,
11690 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011691 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011692 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011693 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011694 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011695 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011696 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011697 .set_cr3 = vmx_set_cr3,
11698 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011699 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011700 .get_idt = vmx_get_idt,
11701 .set_idt = vmx_set_idt,
11702 .get_gdt = vmx_get_gdt,
11703 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011704 .get_dr6 = vmx_get_dr6,
11705 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011706 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011707 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011708 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011709 .get_rflags = vmx_get_rflags,
11710 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011711
11712 .get_pkru = vmx_get_pkru,
11713
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011714 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011715 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011716
11717 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011718
Avi Kivity6aa8b732006-12-10 02:21:36 -080011719 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011720 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011721 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011722 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11723 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011724 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011725 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011726 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011727 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011728 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011729 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011730 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011731 .get_nmi_mask = vmx_get_nmi_mask,
11732 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011733 .enable_nmi_window = enable_nmi_window,
11734 .enable_irq_window = enable_irq_window,
11735 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011736 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011737 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011738 .get_enable_apicv = vmx_get_enable_apicv,
11739 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011740 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11741 .hwapic_irr_update = vmx_hwapic_irr_update,
11742 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011743 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11744 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011745
Izik Eiduscbc94022007-10-25 00:29:55 +020011746 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011747 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011748 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011749
Avi Kivity586f9602010-11-18 13:09:54 +020011750 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011751
Sheng Yang17cc3932010-01-05 19:02:27 +080011752 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011753
11754 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011755
11756 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011757 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011758
11759 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011760
11761 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011762
11763 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011764
11765 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011766
11767 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011768 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011769 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011770 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011771
11772 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011773
11774 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011775
11776 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11777 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11778 .flush_log_dirty = vmx_flush_log_dirty,
11779 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011780
Feng Wubf9f6ac2015-09-18 22:29:55 +080011781 .pre_block = vmx_pre_block,
11782 .post_block = vmx_post_block,
11783
Wei Huang25462f72015-06-19 15:45:05 +020011784 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011785
11786 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011787
11788#ifdef CONFIG_X86_64
11789 .set_hv_timer = vmx_set_hv_timer,
11790 .cancel_hv_timer = vmx_cancel_hv_timer,
11791#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011792
11793 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011794};
11795
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011796static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011797{
11798 if (vmx_l1d_flush_pages) {
11799 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11800 vmx_l1d_flush_pages = NULL;
11801 }
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011802 /* Restore state so sysfs ignores VMX */
11803 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011804}
11805
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011806
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011807static void vmx_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011808{
Dave Young2965faa2015-09-09 15:38:55 -070011809#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011810 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011811 synchronize_rcu();
11812#endif
11813
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011814 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011815
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011816 vmx_cleanup_l1d_flush();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011817}
Avi Kivity6aa8b732006-12-10 02:21:36 -080011818module_exit(vmx_exit)
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011819
11820static int __init vmx_init(void)
11821{
11822 int r;
11823
11824 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11825 __alignof__(struct vcpu_vmx), THIS_MODULE);
11826 if (r)
11827 return r;
11828
11829 /*
Thomas Gleixner641a2112018-07-13 16:23:19 +020011830 * Must be called after kvm_init() so enable_ept is properly set
11831 * up. Hand the parameter mitigation value in which was stored in
11832 * the pre module init parser. If no parameter was given, it will
11833 * contain 'auto' which will be turned into the default 'cond'
11834 * mitigation mode.
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011835 */
Thomas Gleixner641a2112018-07-13 16:23:19 +020011836 if (boot_cpu_has(X86_BUG_L1TF)) {
11837 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
11838 if (r) {
11839 vmx_exit();
11840 return r;
11841 }
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011842 }
11843
11844#ifdef CONFIG_KEXEC_CORE
11845 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11846 crash_vmclear_local_loaded_vmcss);
11847#endif
11848
11849 return 0;
11850}
11851module_init(vmx_init)