blob: a34fb72840241c96c1a3bbc23ab00aebc8e736d8 [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 Ahmed755502f2018-02-01 22:59:44 +0100717 u64 arch_capabilities;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +0100718 u64 spec_ctrl;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100719
Gleb Natapov2961e8762013-11-25 15:37:13 +0200720 u32 vm_entry_controls_shadow;
721 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300722 /*
723 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
724 * non-nested (L1) guest, it always points to vmcs01. For a nested
725 * guest (L2), it points to a different VMCS.
726 */
727 struct loaded_vmcs vmcs01;
728 struct loaded_vmcs *loaded_vmcs;
729 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300730 struct msr_autoload {
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400731 struct vmx_msrs guest;
732 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300733 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400734 struct {
735 int loaded;
736 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300737#ifdef CONFIG_X86_64
738 u16 ds_sel, es_sel;
739#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200740 int gs_ldt_reload_needed;
741 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000742 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700743 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400744 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200745 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300746 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300747 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300748 struct kvm_segment segs[8];
749 } rmode;
750 struct {
751 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300752 struct kvm_save_segment {
753 u16 selector;
754 unsigned long base;
755 u32 limit;
756 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300757 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300758 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800759 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300760 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200761
762 /* Support for vnmi-less CPUs */
763 int soft_vnmi_blocked;
764 ktime_t entry_time;
765 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800766 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800767
Yang Zhang01e439b2013-04-11 19:25:12 +0800768 /* Posted interrupt descriptor */
769 struct pi_desc pi_desc;
770
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300771 /* Support for a guest hypervisor (nested VMX) */
772 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200773
774 /* Dynamic PLE window. */
775 int ple_window;
776 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800777
778 /* Support for PML */
779#define PML_ENTITY_NUM 512
780 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800781
Yunhong Jiang64672c92016-06-13 14:19:59 -0700782 /* apic deadline value in host tsc */
783 u64 hv_deadline_tsc;
784
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800785 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800786
787 bool guest_pkru_valid;
788 u32 guest_pkru;
789 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800790
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800791 /*
792 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
793 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
794 * in msr_ia32_feature_control_valid_bits.
795 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800796 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800797 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400798};
799
Avi Kivity2fb92db2011-04-27 19:42:18 +0300800enum segment_cache_field {
801 SEG_FIELD_SEL = 0,
802 SEG_FIELD_BASE = 1,
803 SEG_FIELD_LIMIT = 2,
804 SEG_FIELD_AR = 3,
805
806 SEG_FIELD_NR = 4
807};
808
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400809static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
810{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000811 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400812}
813
Feng Wuefc64402015-09-18 22:29:51 +0800814static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
815{
816 return &(to_vmx(vcpu)->pi_desc);
817}
818
Nadav Har'El22bd0352011-05-25 23:05:57 +0300819#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
820#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
821#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
822 [number##_HIGH] = VMCS12_OFFSET(name)+4
823
Abel Gordon4607c2d2013-04-18 14:35:55 +0300824
Bandan Dasfe2b2012014-04-21 15:20:14 -0400825static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300826 /*
827 * We do NOT shadow fields that are modified when L0
828 * traps and emulates any vmx instruction (e.g. VMPTRLD,
829 * VMXON...) executed by L1.
830 * For example, VM_INSTRUCTION_ERROR is read
831 * by L1 if a vmx instruction fails (part of the error path).
832 * Note the code assumes this logic. If for some reason
833 * we start shadowing these fields then we need to
834 * force a shadow sync when L0 emulates vmx instructions
835 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
836 * by nested_vmx_failValid)
837 */
838 VM_EXIT_REASON,
839 VM_EXIT_INTR_INFO,
840 VM_EXIT_INSTRUCTION_LEN,
841 IDT_VECTORING_INFO_FIELD,
842 IDT_VECTORING_ERROR_CODE,
843 VM_EXIT_INTR_ERROR_CODE,
844 EXIT_QUALIFICATION,
845 GUEST_LINEAR_ADDRESS,
846 GUEST_PHYSICAL_ADDRESS
847};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400848static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300849 ARRAY_SIZE(shadow_read_only_fields);
850
Bandan Dasfe2b2012014-04-21 15:20:14 -0400851static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800852 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300853 GUEST_RIP,
854 GUEST_RSP,
855 GUEST_CR0,
856 GUEST_CR3,
857 GUEST_CR4,
858 GUEST_INTERRUPTIBILITY_INFO,
859 GUEST_RFLAGS,
860 GUEST_CS_SELECTOR,
861 GUEST_CS_AR_BYTES,
862 GUEST_CS_LIMIT,
863 GUEST_CS_BASE,
864 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100865 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300866 CR0_GUEST_HOST_MASK,
867 CR0_READ_SHADOW,
868 CR4_READ_SHADOW,
869 TSC_OFFSET,
870 EXCEPTION_BITMAP,
871 CPU_BASED_VM_EXEC_CONTROL,
872 VM_ENTRY_EXCEPTION_ERROR_CODE,
873 VM_ENTRY_INTR_INFO_FIELD,
874 VM_ENTRY_INSTRUCTION_LEN,
875 VM_ENTRY_EXCEPTION_ERROR_CODE,
876 HOST_FS_BASE,
877 HOST_GS_BASE,
878 HOST_FS_SELECTOR,
879 HOST_GS_SELECTOR
880};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400881static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300882 ARRAY_SIZE(shadow_read_write_fields);
883
Mathias Krause772e0312012-08-30 01:30:19 +0200884static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300885 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800886 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300887 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
888 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
889 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
890 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
891 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
892 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
893 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
894 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800895 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300896 FIELD(HOST_ES_SELECTOR, host_es_selector),
897 FIELD(HOST_CS_SELECTOR, host_cs_selector),
898 FIELD(HOST_SS_SELECTOR, host_ss_selector),
899 FIELD(HOST_DS_SELECTOR, host_ds_selector),
900 FIELD(HOST_FS_SELECTOR, host_fs_selector),
901 FIELD(HOST_GS_SELECTOR, host_gs_selector),
902 FIELD(HOST_TR_SELECTOR, host_tr_selector),
903 FIELD64(IO_BITMAP_A, io_bitmap_a),
904 FIELD64(IO_BITMAP_B, io_bitmap_b),
905 FIELD64(MSR_BITMAP, msr_bitmap),
906 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
907 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
908 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
909 FIELD64(TSC_OFFSET, tsc_offset),
910 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
911 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800912 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300913 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800914 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
915 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
916 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
917 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800918 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300919 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
920 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
921 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
922 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
923 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
924 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
925 FIELD64(GUEST_PDPTR0, guest_pdptr0),
926 FIELD64(GUEST_PDPTR1, guest_pdptr1),
927 FIELD64(GUEST_PDPTR2, guest_pdptr2),
928 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100929 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300930 FIELD64(HOST_IA32_PAT, host_ia32_pat),
931 FIELD64(HOST_IA32_EFER, host_ia32_efer),
932 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
933 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
934 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
935 FIELD(EXCEPTION_BITMAP, exception_bitmap),
936 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
937 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
938 FIELD(CR3_TARGET_COUNT, cr3_target_count),
939 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
940 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
941 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
942 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
943 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
944 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
945 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
946 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
947 FIELD(TPR_THRESHOLD, tpr_threshold),
948 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
949 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
950 FIELD(VM_EXIT_REASON, vm_exit_reason),
951 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
952 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
953 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
954 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
955 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
956 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
957 FIELD(GUEST_ES_LIMIT, guest_es_limit),
958 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
959 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
960 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
961 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
962 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
963 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
964 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
965 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
966 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
967 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
968 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
969 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
970 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
971 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
972 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
973 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
974 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
975 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
976 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
977 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
978 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100979 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300980 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
981 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
982 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
983 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
984 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
985 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
986 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
987 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
988 FIELD(EXIT_QUALIFICATION, exit_qualification),
989 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
990 FIELD(GUEST_CR0, guest_cr0),
991 FIELD(GUEST_CR3, guest_cr3),
992 FIELD(GUEST_CR4, guest_cr4),
993 FIELD(GUEST_ES_BASE, guest_es_base),
994 FIELD(GUEST_CS_BASE, guest_cs_base),
995 FIELD(GUEST_SS_BASE, guest_ss_base),
996 FIELD(GUEST_DS_BASE, guest_ds_base),
997 FIELD(GUEST_FS_BASE, guest_fs_base),
998 FIELD(GUEST_GS_BASE, guest_gs_base),
999 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1000 FIELD(GUEST_TR_BASE, guest_tr_base),
1001 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1002 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1003 FIELD(GUEST_DR7, guest_dr7),
1004 FIELD(GUEST_RSP, guest_rsp),
1005 FIELD(GUEST_RIP, guest_rip),
1006 FIELD(GUEST_RFLAGS, guest_rflags),
1007 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1008 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1009 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1010 FIELD(HOST_CR0, host_cr0),
1011 FIELD(HOST_CR3, host_cr3),
1012 FIELD(HOST_CR4, host_cr4),
1013 FIELD(HOST_FS_BASE, host_fs_base),
1014 FIELD(HOST_GS_BASE, host_gs_base),
1015 FIELD(HOST_TR_BASE, host_tr_base),
1016 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1017 FIELD(HOST_IDTR_BASE, host_idtr_base),
1018 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1019 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1020 FIELD(HOST_RSP, host_rsp),
1021 FIELD(HOST_RIP, host_rip),
1022};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001023
1024static inline short vmcs_field_to_offset(unsigned long field)
1025{
Dan Williamseb99bd62018-01-31 17:47:03 -08001026 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1027 unsigned short offset;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001028
Dan Williamseb99bd62018-01-31 17:47:03 -08001029 BUILD_BUG_ON(size > SHRT_MAX);
1030 if (field >= size)
Andrew Honig012df712018-01-10 10:12:03 -08001031 return -ENOENT;
1032
Dan Williamseb99bd62018-01-31 17:47:03 -08001033 field = array_index_nospec(field, size);
1034 offset = vmcs_field_to_offset_table[field];
1035 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001036 return -ENOENT;
Dan Williamseb99bd62018-01-31 17:47:03 -08001037 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001038}
1039
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001040static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1041{
David Matlack4f2777b2016-07-13 17:16:37 -07001042 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001043}
1044
1045static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
1046{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001047 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08001048 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001049 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +08001050
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001051 return page;
1052}
1053
1054static void nested_release_page(struct page *page)
1055{
1056 kvm_release_page_dirty(page);
1057}
1058
1059static void nested_release_page_clean(struct page *page)
1060{
1061 kvm_release_page_clean(page);
1062}
1063
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001064static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +08001065static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001066static void kvm_cpu_vmxon(u64 addr);
1067static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001068static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +02001069static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001070static void vmx_set_segment(struct kvm_vcpu *vcpu,
1071 struct kvm_segment *var, int seg);
1072static void vmx_get_segment(struct kvm_vcpu *vcpu,
1073 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001074static bool guest_state_valid(struct kvm_vcpu *vcpu);
1075static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +03001076static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +03001077static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +08001078static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzini6236b782018-01-16 16:51:18 +01001079static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Yi Wang0c4d57a2018-11-08 11:22:21 +08001080static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj70131292018-02-01 22:59:43 +01001081 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001082
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1084static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001085/*
1086 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1087 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1088 */
1089static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +03001090static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
Feng Wubf9f6ac2015-09-18 22:29:55 +08001092/*
1093 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1094 * can find which vCPU should be waken up.
1095 */
1096static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1097static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1098
Avi Kivity3e7c73e2009-02-24 21:46:19 +02001099static unsigned long *vmx_io_bitmap_a;
1100static unsigned long *vmx_io_bitmap_b;
Abel Gordon4607c2d2013-04-18 14:35:55 +03001101static unsigned long *vmx_vmread_bitmap;
1102static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +03001103
Avi Kivity110312c2010-12-21 12:54:20 +02001104static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001105static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001106
Sheng Yang2384d2b2008-01-17 15:14:33 +08001107static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1108static DEFINE_SPINLOCK(vmx_vpid_lock);
1109
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001110static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 int size;
1112 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001113 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001115 u32 pin_based_exec_ctrl;
1116 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001117 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001118 u32 vmexit_ctrl;
1119 u32 vmentry_ctrl;
1120} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121
Hannes Ederefff9e52008-11-28 17:02:06 +01001122static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001123 u32 ept;
1124 u32 vpid;
1125} vmx_capability;
1126
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127#define VMX_SEGMENT_FIELD(seg) \
1128 [VCPU_SREG_##seg] = { \
1129 .selector = GUEST_##seg##_SELECTOR, \
1130 .base = GUEST_##seg##_BASE, \
1131 .limit = GUEST_##seg##_LIMIT, \
1132 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1133 }
1134
Mathias Krause772e0312012-08-30 01:30:19 +02001135static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 unsigned selector;
1137 unsigned base;
1138 unsigned limit;
1139 unsigned ar_bytes;
1140} kvm_vmx_segment_fields[] = {
1141 VMX_SEGMENT_FIELD(CS),
1142 VMX_SEGMENT_FIELD(DS),
1143 VMX_SEGMENT_FIELD(ES),
1144 VMX_SEGMENT_FIELD(FS),
1145 VMX_SEGMENT_FIELD(GS),
1146 VMX_SEGMENT_FIELD(SS),
1147 VMX_SEGMENT_FIELD(TR),
1148 VMX_SEGMENT_FIELD(LDTR),
1149};
1150
Avi Kivity26bb0982009-09-07 11:14:12 +03001151static u64 host_efer;
1152
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001153static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1154
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001155/*
Brian Gerst8c065852010-07-17 09:03:26 -04001156 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001157 * away by decrementing the array size.
1158 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001160#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001161 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001163 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165
Jan Kiszka5bb16012016-02-09 20:14:21 +01001166static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167{
1168 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1169 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001170 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1171}
1172
Jan Kiszka6f054852016-02-09 20:15:18 +01001173static inline bool is_debug(u32 intr_info)
1174{
1175 return is_exception_n(intr_info, DB_VECTOR);
1176}
1177
1178static inline bool is_breakpoint(u32 intr_info)
1179{
1180 return is_exception_n(intr_info, BP_VECTOR);
1181}
1182
Jan Kiszka5bb16012016-02-09 20:14:21 +01001183static inline bool is_page_fault(u32 intr_info)
1184{
1185 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186}
1187
Gui Jianfeng31299942010-03-15 17:29:09 +08001188static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001189{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001190 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001191}
1192
Gui Jianfeng31299942010-03-15 17:29:09 +08001193static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001194{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001195 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001196}
1197
Gui Jianfeng31299942010-03-15 17:29:09 +08001198static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199{
1200 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1201 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1202}
1203
Gui Jianfeng31299942010-03-15 17:29:09 +08001204static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001205{
1206 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1207 INTR_INFO_VALID_MASK)) ==
1208 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1209}
1210
Linus Torvalds587da2b2018-03-20 12:16:59 -07001211/* Undocumented: icebp/int1 */
1212static inline bool is_icebp(u32 intr_info)
1213{
1214 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1215 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1216}
1217
Gui Jianfeng31299942010-03-15 17:29:09 +08001218static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001219{
Sheng Yang04547152009-04-01 15:52:31 +08001220 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001221}
1222
Gui Jianfeng31299942010-03-15 17:29:09 +08001223static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001224{
Sheng Yang04547152009-04-01 15:52:31 +08001225 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001226}
1227
Paolo Bonzini35754c92015-07-29 12:05:37 +02001228static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001229{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001230 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001231}
1232
Gui Jianfeng31299942010-03-15 17:29:09 +08001233static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001234{
Sheng Yang04547152009-04-01 15:52:31 +08001235 return vmcs_config.cpu_based_exec_ctrl &
1236 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001237}
1238
Avi Kivity774ead32007-12-26 13:57:04 +02001239static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001240{
Sheng Yang04547152009-04-01 15:52:31 +08001241 return vmcs_config.cpu_based_2nd_exec_ctrl &
1242 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1243}
1244
Yang Zhang8d146952013-01-25 10:18:50 +08001245static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1246{
1247 return vmcs_config.cpu_based_2nd_exec_ctrl &
1248 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1249}
1250
Yang Zhang83d4c282013-01-25 10:18:49 +08001251static inline bool cpu_has_vmx_apic_register_virt(void)
1252{
1253 return vmcs_config.cpu_based_2nd_exec_ctrl &
1254 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1255}
1256
Yang Zhangc7c9c562013-01-25 10:18:51 +08001257static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1258{
1259 return vmcs_config.cpu_based_2nd_exec_ctrl &
1260 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1261}
1262
Yunhong Jiang64672c92016-06-13 14:19:59 -07001263/*
1264 * Comment's format: document - errata name - stepping - processor name.
1265 * Refer from
1266 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1267 */
1268static u32 vmx_preemption_cpu_tfms[] = {
1269/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12700x000206E6,
1271/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1272/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1273/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12740x00020652,
1275/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12760x00020655,
1277/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1278/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1279/*
1280 * 320767.pdf - AAP86 - B1 -
1281 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1282 */
12830x000106E5,
1284/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
12850x000106A0,
1286/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
12870x000106A1,
1288/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
12890x000106A4,
1290 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1291 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1292 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
12930x000106A5,
1294};
1295
1296static inline bool cpu_has_broken_vmx_preemption_timer(void)
1297{
1298 u32 eax = cpuid_eax(0x00000001), i;
1299
1300 /* Clear the reserved bits */
1301 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001302 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001303 if (eax == vmx_preemption_cpu_tfms[i])
1304 return true;
1305
1306 return false;
1307}
1308
1309static inline bool cpu_has_vmx_preemption_timer(void)
1310{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001311 return vmcs_config.pin_based_exec_ctrl &
1312 PIN_BASED_VMX_PREEMPTION_TIMER;
1313}
1314
Yang Zhang01e439b2013-04-11 19:25:12 +08001315static inline bool cpu_has_vmx_posted_intr(void)
1316{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001317 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1318 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001319}
1320
1321static inline bool cpu_has_vmx_apicv(void)
1322{
1323 return cpu_has_vmx_apic_register_virt() &&
1324 cpu_has_vmx_virtual_intr_delivery() &&
1325 cpu_has_vmx_posted_intr();
1326}
1327
Sheng Yang04547152009-04-01 15:52:31 +08001328static inline bool cpu_has_vmx_flexpriority(void)
1329{
1330 return cpu_has_vmx_tpr_shadow() &&
1331 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001332}
1333
Marcelo Tosattie7997942009-06-11 12:07:40 -03001334static inline bool cpu_has_vmx_ept_execute_only(void)
1335{
Gui Jianfeng31299942010-03-15 17:29:09 +08001336 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001337}
1338
Marcelo Tosattie7997942009-06-11 12:07:40 -03001339static inline bool cpu_has_vmx_ept_2m_page(void)
1340{
Gui Jianfeng31299942010-03-15 17:29:09 +08001341 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001342}
1343
Sheng Yang878403b2010-01-05 19:02:29 +08001344static inline bool cpu_has_vmx_ept_1g_page(void)
1345{
Gui Jianfeng31299942010-03-15 17:29:09 +08001346 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001347}
1348
Sheng Yang4bc9b982010-06-02 14:05:24 +08001349static inline bool cpu_has_vmx_ept_4levels(void)
1350{
1351 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1352}
1353
Xudong Hao83c3a332012-05-28 19:33:35 +08001354static inline bool cpu_has_vmx_ept_ad_bits(void)
1355{
1356 return vmx_capability.ept & VMX_EPT_AD_BIT;
1357}
1358
Gui Jianfeng31299942010-03-15 17:29:09 +08001359static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001360{
Gui Jianfeng31299942010-03-15 17:29:09 +08001361 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001362}
1363
Gui Jianfeng31299942010-03-15 17:29:09 +08001364static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001365{
Gui Jianfeng31299942010-03-15 17:29:09 +08001366 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001367}
1368
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001369static inline bool cpu_has_vmx_invvpid_single(void)
1370{
1371 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1372}
1373
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001374static inline bool cpu_has_vmx_invvpid_global(void)
1375{
1376 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1377}
1378
Wanpeng Li2df19692017-03-23 05:30:08 -07001379static inline bool cpu_has_vmx_invvpid(void)
1380{
1381 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1382}
1383
Gui Jianfeng31299942010-03-15 17:29:09 +08001384static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001385{
Sheng Yang04547152009-04-01 15:52:31 +08001386 return vmcs_config.cpu_based_2nd_exec_ctrl &
1387 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001388}
1389
Gui Jianfeng31299942010-03-15 17:29:09 +08001390static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001391{
1392 return vmcs_config.cpu_based_2nd_exec_ctrl &
1393 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1394}
1395
Gui Jianfeng31299942010-03-15 17:29:09 +08001396static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001397{
1398 return vmcs_config.cpu_based_2nd_exec_ctrl &
1399 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1400}
1401
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001402static inline bool cpu_has_vmx_basic_inout(void)
1403{
1404 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1405}
1406
Paolo Bonzini35754c92015-07-29 12:05:37 +02001407static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001408{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001409 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001410}
1411
Gui Jianfeng31299942010-03-15 17:29:09 +08001412static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001413{
Sheng Yang04547152009-04-01 15:52:31 +08001414 return vmcs_config.cpu_based_2nd_exec_ctrl &
1415 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001416}
1417
Gui Jianfeng31299942010-03-15 17:29:09 +08001418static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001419{
1420 return vmcs_config.cpu_based_2nd_exec_ctrl &
1421 SECONDARY_EXEC_RDTSCP;
1422}
1423
Mao, Junjiead756a12012-07-02 01:18:48 +00001424static inline bool cpu_has_vmx_invpcid(void)
1425{
1426 return vmcs_config.cpu_based_2nd_exec_ctrl &
1427 SECONDARY_EXEC_ENABLE_INVPCID;
1428}
1429
Gui Jianfeng31299942010-03-15 17:29:09 +08001430static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001431{
1432 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1433}
1434
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001435static inline bool cpu_has_vmx_wbinvd_exit(void)
1436{
1437 return vmcs_config.cpu_based_2nd_exec_ctrl &
1438 SECONDARY_EXEC_WBINVD_EXITING;
1439}
1440
Abel Gordonabc4fc52013-04-18 14:35:25 +03001441static inline bool cpu_has_vmx_shadow_vmcs(void)
1442{
1443 u64 vmx_msr;
1444 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1445 /* check if the cpu supports writing r/o exit information fields */
1446 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1447 return false;
1448
1449 return vmcs_config.cpu_based_2nd_exec_ctrl &
1450 SECONDARY_EXEC_SHADOW_VMCS;
1451}
1452
Kai Huang843e4332015-01-28 10:54:28 +08001453static inline bool cpu_has_vmx_pml(void)
1454{
1455 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1456}
1457
Haozhong Zhang64903d62015-10-20 15:39:09 +08001458static inline bool cpu_has_vmx_tsc_scaling(void)
1459{
1460 return vmcs_config.cpu_based_2nd_exec_ctrl &
1461 SECONDARY_EXEC_TSC_SCALING;
1462}
1463
Sheng Yang04547152009-04-01 15:52:31 +08001464static inline bool report_flexpriority(void)
1465{
1466 return flexpriority_enabled;
1467}
1468
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001469static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1470{
1471 return vmcs12->cpu_based_vm_exec_control & bit;
1472}
1473
1474static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1475{
1476 return (vmcs12->cpu_based_vm_exec_control &
1477 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1478 (vmcs12->secondary_vm_exec_control & bit);
1479}
1480
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001481static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001482{
1483 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1484}
1485
Jan Kiszkaf4124502014-03-07 20:03:13 +01001486static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1487{
1488 return vmcs12->pin_based_vm_exec_control &
1489 PIN_BASED_VMX_PREEMPTION_TIMER;
1490}
1491
Nadav Har'El155a97a2013-08-05 11:07:16 +03001492static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1493{
1494 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1495}
1496
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001497static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1498{
1499 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1500 vmx_xsaves_supported();
1501}
1502
Wincy Vanf2b93282015-02-03 23:56:03 +08001503static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1504{
1505 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1506}
1507
Wanpeng Li5c614b32015-10-13 09:18:36 -07001508static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1509{
1510 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1511}
1512
Wincy Van82f0dd42015-02-03 23:57:18 +08001513static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1514{
1515 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1516}
1517
Wincy Van608406e2015-02-03 23:57:51 +08001518static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1519{
1520 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1521}
1522
Wincy Van705699a2015-02-03 23:58:17 +08001523static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1524{
1525 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1526}
1527
Jim Mattson3f618a02016-12-12 11:01:37 -08001528static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001529{
1530 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001531 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001532}
1533
Jan Kiszka533558b2014-01-04 18:47:20 +01001534static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1535 u32 exit_intr_info,
1536 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001537static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1538 struct vmcs12 *vmcs12,
1539 u32 reason, unsigned long qualification);
1540
Rusty Russell8b9cf982007-07-30 16:31:43 +10001541static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001542{
1543 int i;
1544
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001545 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001546 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001547 return i;
1548 return -1;
1549}
1550
Sheng Yang2384d2b2008-01-17 15:14:33 +08001551static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1552{
1553 struct {
1554 u64 vpid : 16;
1555 u64 rsvd : 48;
1556 u64 gva;
1557 } operand = { vpid, 0, gva };
1558
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001559 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001560 /* CF==1 or ZF==1 --> rc = -1 */
1561 "; ja 1f ; ud2 ; 1:"
1562 : : "a"(&operand), "c"(ext) : "cc", "memory");
1563}
1564
Sheng Yang14394422008-04-28 12:24:45 +08001565static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1566{
1567 struct {
1568 u64 eptp, gpa;
1569 } operand = {eptp, gpa};
1570
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001571 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001572 /* CF==1 or ZF==1 --> rc = -1 */
1573 "; ja 1f ; ud2 ; 1:\n"
1574 : : "a" (&operand), "c" (ext) : "cc", "memory");
1575}
1576
Avi Kivity26bb0982009-09-07 11:14:12 +03001577static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001578{
1579 int i;
1580
Rusty Russell8b9cf982007-07-30 16:31:43 +10001581 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001582 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001583 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001584 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001585}
1586
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587static void vmcs_clear(struct vmcs *vmcs)
1588{
1589 u64 phys_addr = __pa(vmcs);
1590 u8 error;
1591
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001592 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001593 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 : "cc", "memory");
1595 if (error)
1596 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1597 vmcs, phys_addr);
1598}
1599
Nadav Har'Eld462b812011-05-24 15:26:10 +03001600static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1601{
1602 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001603 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1604 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001605 loaded_vmcs->cpu = -1;
1606 loaded_vmcs->launched = 0;
1607}
1608
Dongxiao Xu7725b892010-05-11 18:29:38 +08001609static void vmcs_load(struct vmcs *vmcs)
1610{
1611 u64 phys_addr = __pa(vmcs);
1612 u8 error;
1613
1614 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001615 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001616 : "cc", "memory");
1617 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001618 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001619 vmcs, phys_addr);
1620}
1621
Dave Young2965faa2015-09-09 15:38:55 -07001622#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001623/*
1624 * This bitmap is used to indicate whether the vmclear
1625 * operation is enabled on all cpus. All disabled by
1626 * default.
1627 */
1628static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1629
1630static inline void crash_enable_local_vmclear(int cpu)
1631{
1632 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1633}
1634
1635static inline void crash_disable_local_vmclear(int cpu)
1636{
1637 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1638}
1639
1640static inline int crash_local_vmclear_enabled(int cpu)
1641{
1642 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1643}
1644
1645static void crash_vmclear_local_loaded_vmcss(void)
1646{
1647 int cpu = raw_smp_processor_id();
1648 struct loaded_vmcs *v;
1649
1650 if (!crash_local_vmclear_enabled(cpu))
1651 return;
1652
1653 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1654 loaded_vmcss_on_cpu_link)
1655 vmcs_clear(v->vmcs);
1656}
1657#else
1658static inline void crash_enable_local_vmclear(int cpu) { }
1659static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001660#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001661
Nadav Har'Eld462b812011-05-24 15:26:10 +03001662static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001664 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001665 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666
Nadav Har'Eld462b812011-05-24 15:26:10 +03001667 if (loaded_vmcs->cpu != cpu)
1668 return; /* vcpu migration can race with cpu offline */
1669 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001671 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001672 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001673
1674 /*
1675 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1676 * is before setting loaded_vmcs->vcpu to -1 which is done in
1677 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1678 * then adds the vmcs into percpu list before it is deleted.
1679 */
1680 smp_wmb();
1681
Nadav Har'Eld462b812011-05-24 15:26:10 +03001682 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001683 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684}
1685
Nadav Har'Eld462b812011-05-24 15:26:10 +03001686static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001687{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001688 int cpu = loaded_vmcs->cpu;
1689
1690 if (cpu != -1)
1691 smp_call_function_single(cpu,
1692 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001693}
1694
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001695static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001696{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001697 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001698 return;
1699
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001700 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001701 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001702}
1703
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001704static inline void vpid_sync_vcpu_global(void)
1705{
1706 if (cpu_has_vmx_invvpid_global())
1707 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1708}
1709
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001710static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001711{
1712 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001713 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001714 else
1715 vpid_sync_vcpu_global();
1716}
1717
Sheng Yang14394422008-04-28 12:24:45 +08001718static inline void ept_sync_global(void)
1719{
1720 if (cpu_has_vmx_invept_global())
1721 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1722}
1723
1724static inline void ept_sync_context(u64 eptp)
1725{
Avi Kivity089d0342009-03-23 18:26:32 +02001726 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001727 if (cpu_has_vmx_invept_context())
1728 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1729 else
1730 ept_sync_global();
1731 }
1732}
1733
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001734static __always_inline void vmcs_check16(unsigned long field)
1735{
1736 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1737 "16-bit accessor invalid for 64-bit field");
1738 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1739 "16-bit accessor invalid for 64-bit high field");
1740 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1741 "16-bit accessor invalid for 32-bit high field");
1742 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1743 "16-bit accessor invalid for natural width field");
1744}
1745
1746static __always_inline void vmcs_check32(unsigned long field)
1747{
1748 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1749 "32-bit accessor invalid for 16-bit field");
1750 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1751 "32-bit accessor invalid for natural width field");
1752}
1753
1754static __always_inline void vmcs_check64(unsigned long field)
1755{
1756 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1757 "64-bit accessor invalid for 16-bit field");
1758 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1759 "64-bit accessor invalid for 64-bit high field");
1760 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1761 "64-bit accessor invalid for 32-bit field");
1762 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1763 "64-bit accessor invalid for natural width field");
1764}
1765
1766static __always_inline void vmcs_checkl(unsigned long field)
1767{
1768 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1769 "Natural width accessor invalid for 16-bit field");
1770 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1771 "Natural width accessor invalid for 64-bit field");
1772 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1773 "Natural width accessor invalid for 64-bit high field");
1774 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1775 "Natural width accessor invalid for 32-bit field");
1776}
1777
1778static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779{
Avi Kivity5e520e62011-05-15 10:13:12 -04001780 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781
Avi Kivity5e520e62011-05-15 10:13:12 -04001782 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1783 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784 return value;
1785}
1786
Avi Kivity96304212011-05-15 10:13:13 -04001787static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001789 vmcs_check16(field);
1790 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791}
1792
Avi Kivity96304212011-05-15 10:13:13 -04001793static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001795 vmcs_check32(field);
1796 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797}
1798
Avi Kivity96304212011-05-15 10:13:13 -04001799static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001801 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001802#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001803 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001805 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806#endif
1807}
1808
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001809static __always_inline unsigned long vmcs_readl(unsigned long field)
1810{
1811 vmcs_checkl(field);
1812 return __vmcs_readl(field);
1813}
1814
Avi Kivitye52de1b2007-01-05 16:36:56 -08001815static noinline void vmwrite_error(unsigned long field, unsigned long value)
1816{
1817 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1818 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1819 dump_stack();
1820}
1821
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001822static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823{
1824 u8 error;
1825
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001826 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001827 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001828 if (unlikely(error))
1829 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830}
1831
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001832static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001834 vmcs_check16(field);
1835 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836}
1837
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001838static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001840 vmcs_check32(field);
1841 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001842}
1843
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001844static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001846 vmcs_check64(field);
1847 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001848#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001850 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851#endif
1852}
1853
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001854static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001855{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001856 vmcs_checkl(field);
1857 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001858}
1859
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001860static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001861{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001862 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1863 "vmcs_clear_bits does not support 64-bit fields");
1864 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1865}
1866
1867static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1868{
1869 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1870 "vmcs_set_bits does not support 64-bit fields");
1871 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001872}
1873
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001874static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1875{
1876 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1877}
1878
Gleb Natapov2961e8762013-11-25 15:37:13 +02001879static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1880{
1881 vmcs_write32(VM_ENTRY_CONTROLS, val);
1882 vmx->vm_entry_controls_shadow = val;
1883}
1884
1885static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1886{
1887 if (vmx->vm_entry_controls_shadow != val)
1888 vm_entry_controls_init(vmx, val);
1889}
1890
1891static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1892{
1893 return vmx->vm_entry_controls_shadow;
1894}
1895
1896
1897static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1898{
1899 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1900}
1901
1902static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1903{
1904 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1905}
1906
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001907static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1908{
1909 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1910}
1911
Gleb Natapov2961e8762013-11-25 15:37:13 +02001912static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1913{
1914 vmcs_write32(VM_EXIT_CONTROLS, val);
1915 vmx->vm_exit_controls_shadow = val;
1916}
1917
1918static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1919{
1920 if (vmx->vm_exit_controls_shadow != val)
1921 vm_exit_controls_init(vmx, val);
1922}
1923
1924static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1925{
1926 return vmx->vm_exit_controls_shadow;
1927}
1928
1929
1930static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1931{
1932 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1933}
1934
1935static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1936{
1937 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1938}
1939
Avi Kivity2fb92db2011-04-27 19:42:18 +03001940static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1941{
1942 vmx->segment_cache.bitmask = 0;
1943}
1944
1945static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1946 unsigned field)
1947{
1948 bool ret;
1949 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1950
1951 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1952 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1953 vmx->segment_cache.bitmask = 0;
1954 }
1955 ret = vmx->segment_cache.bitmask & mask;
1956 vmx->segment_cache.bitmask |= mask;
1957 return ret;
1958}
1959
1960static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1961{
1962 u16 *p = &vmx->segment_cache.seg[seg].selector;
1963
1964 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1965 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1966 return *p;
1967}
1968
1969static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1970{
1971 ulong *p = &vmx->segment_cache.seg[seg].base;
1972
1973 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1974 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1975 return *p;
1976}
1977
1978static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1979{
1980 u32 *p = &vmx->segment_cache.seg[seg].limit;
1981
1982 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1983 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1984 return *p;
1985}
1986
1987static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1988{
1989 u32 *p = &vmx->segment_cache.seg[seg].ar;
1990
1991 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1992 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1993 return *p;
1994}
1995
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001996static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1997{
1998 u32 eb;
1999
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002000 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01002001 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002002 if ((vcpu->guest_debug &
2003 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2004 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2005 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002006 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002007 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002008 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002009 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02002010 if (vcpu->fpu_active)
2011 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002012
2013 /* When we are running a nested L2 guest and L1 specified for it a
2014 * certain exception bitmap, we must trap the same exceptions and pass
2015 * them to L1. When running L2, we will only handle the exceptions
2016 * specified above if L1 did not want them.
2017 */
2018 if (is_guest_mode(vcpu))
2019 eb |= get_vmcs12(vcpu)->exception_bitmap;
2020
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002021 vmcs_write32(EXCEPTION_BITMAP, eb);
2022}
2023
Ashok Raj70131292018-02-01 22:59:43 +01002024/*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01002025 * Check if MSR is intercepted for currently loaded MSR bitmap.
2026 */
2027static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2028{
2029 unsigned long *msr_bitmap;
2030 int f = sizeof(unsigned long);
2031
2032 if (!cpu_has_vmx_msr_bitmap())
2033 return true;
2034
2035 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2036
2037 if (msr <= 0x1fff) {
2038 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2039 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2040 msr &= 0x1fff;
2041 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2042 }
2043
2044 return true;
2045}
2046
2047/*
Ashok Raj70131292018-02-01 22:59:43 +01002048 * Check if MSR is intercepted for L01 MSR bitmap.
2049 */
2050static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2051{
2052 unsigned long *msr_bitmap;
2053 int f = sizeof(unsigned long);
2054
2055 if (!cpu_has_vmx_msr_bitmap())
2056 return true;
2057
2058 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2059
2060 if (msr <= 0x1fff) {
2061 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2062 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2063 msr &= 0x1fff;
2064 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2065 }
2066
2067 return true;
2068}
2069
Gleb Natapov2961e8762013-11-25 15:37:13 +02002070static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2071 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002072{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002073 vm_entry_controls_clearbit(vmx, entry);
2074 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002075}
2076
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002077static int find_msr(struct vmx_msrs *m, unsigned int msr)
2078{
2079 unsigned int i;
2080
2081 for (i = 0; i < m->nr; ++i) {
2082 if (m->val[i].index == msr)
2083 return i;
2084 }
2085 return -ENOENT;
2086}
2087
Avi Kivity61d2ef22010-04-28 16:40:38 +03002088static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2089{
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002090 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002091 struct msr_autoload *m = &vmx->msr_autoload;
2092
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002093 switch (msr) {
2094 case MSR_EFER:
2095 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002096 clear_atomic_switch_msr_special(vmx,
2097 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002098 VM_EXIT_LOAD_IA32_EFER);
2099 return;
2100 }
2101 break;
2102 case MSR_CORE_PERF_GLOBAL_CTRL:
2103 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002104 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002105 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2106 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2107 return;
2108 }
2109 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002110 }
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002111 i = find_msr(&m->guest, msr);
2112 if (i < 0)
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002113 goto skip_guest;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002114 --m->guest.nr;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002115 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002116 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002117
2118skip_guest:
2119 i = find_msr(&m->host, msr);
2120 if (i < 0)
2121 return;
2122
2123 --m->host.nr;
2124 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002125 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002126}
2127
Gleb Natapov2961e8762013-11-25 15:37:13 +02002128static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2129 unsigned long entry, unsigned long exit,
2130 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2131 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002132{
2133 vmcs_write64(guest_val_vmcs, guest_val);
2134 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002135 vm_entry_controls_setbit(vmx, entry);
2136 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002137}
2138
Avi Kivity61d2ef22010-04-28 16:40:38 +03002139static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002140 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002141{
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002142 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002143 struct msr_autoload *m = &vmx->msr_autoload;
2144
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002145 switch (msr) {
2146 case MSR_EFER:
2147 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002148 add_atomic_switch_msr_special(vmx,
2149 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002150 VM_EXIT_LOAD_IA32_EFER,
2151 GUEST_IA32_EFER,
2152 HOST_IA32_EFER,
2153 guest_val, host_val);
2154 return;
2155 }
2156 break;
2157 case MSR_CORE_PERF_GLOBAL_CTRL:
2158 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002159 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002160 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2161 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2162 GUEST_IA32_PERF_GLOBAL_CTRL,
2163 HOST_IA32_PERF_GLOBAL_CTRL,
2164 guest_val, host_val);
2165 return;
2166 }
2167 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002168 case MSR_IA32_PEBS_ENABLE:
2169 /* PEBS needs a quiescent period after being disabled (to write
2170 * a record). Disabling PEBS through VMX MSR swapping doesn't
2171 * provide that period, so a CPU could write host's record into
2172 * guest's memory.
2173 */
2174 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002175 }
2176
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002177 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002178 if (!entry_only)
2179 j = find_msr(&m->host, msr);
2180
Xiaoyao Liacd744b2019-02-14 12:08:58 +08002181 if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) ||
2182 (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002183 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002184 "Can't add msr %x\n", msr);
2185 return;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002186 }
2187 if (i < 0) {
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002188 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002189 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002190 }
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002191 m->guest.val[i].index = msr;
2192 m->guest.val[i].value = guest_val;
2193
2194 if (entry_only)
2195 return;
2196
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002197 if (j < 0) {
2198 j = m->host.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002199 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002200 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002201 m->host.val[j].index = msr;
2202 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002203}
2204
Avi Kivity33ed6322007-05-02 16:54:03 +03002205static void reload_tss(void)
2206{
Avi Kivity33ed6322007-05-02 16:54:03 +03002207 /*
2208 * VT restores TR but not its size. Useless.
2209 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002210 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002211 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002212
Avi Kivityd3591922010-07-26 18:32:39 +03002213 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002214 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2215 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002216}
2217
Avi Kivity92c0d902009-10-29 11:00:16 +02002218static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002219{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002220 u64 guest_efer = vmx->vcpu.arch.efer;
2221 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002222
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002223 if (!enable_ept) {
2224 /*
2225 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2226 * host CPUID is more efficient than testing guest CPUID
2227 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2228 */
2229 if (boot_cpu_has(X86_FEATURE_SMEP))
2230 guest_efer |= EFER_NX;
2231 else if (!(guest_efer & EFER_NX))
2232 ignore_bits |= EFER_NX;
2233 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002234
Avi Kivity51c6cf62007-08-29 03:48:05 +03002235 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002236 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002237 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002238 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002239#ifdef CONFIG_X86_64
2240 ignore_bits |= EFER_LMA | EFER_LME;
2241 /* SCE is meaningful only in long mode on Intel */
2242 if (guest_efer & EFER_LMA)
2243 ignore_bits &= ~(u64)EFER_SCE;
2244#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002245
2246 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002247
2248 /*
2249 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2250 * On CPUs that support "load IA32_EFER", always switch EFER
2251 * atomically, since it's faster than switching it manually.
2252 */
2253 if (cpu_has_load_ia32_efer ||
2254 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002255 if (!(guest_efer & EFER_LMA))
2256 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002257 if (guest_efer != host_efer)
2258 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002259 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002260 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002261 } else {
2262 guest_efer &= ~ignore_bits;
2263 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002264
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002265 vmx->guest_msrs[efer_offset].data = guest_efer;
2266 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2267
2268 return true;
2269 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002270}
2271
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002272static unsigned long segment_base(u16 selector)
2273{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002274 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002275 struct desc_struct *d;
2276 unsigned long table_base;
2277 unsigned long v;
2278
2279 if (!(selector & ~3))
2280 return 0;
2281
Avi Kivityd3591922010-07-26 18:32:39 +03002282 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002283
2284 if (selector & 4) { /* from ldt */
2285 u16 ldt_selector = kvm_read_ldt();
2286
2287 if (!(ldt_selector & ~3))
2288 return 0;
2289
2290 table_base = segment_base(ldt_selector);
2291 }
2292 d = (struct desc_struct *)(table_base + (selector & ~7));
2293 v = get_desc_base(d);
2294#ifdef CONFIG_X86_64
2295 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2296 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2297#endif
2298 return v;
2299}
2300
2301static inline unsigned long kvm_read_tr_base(void)
2302{
2303 u16 tr;
2304 asm("str %0" : "=g"(tr));
2305 return segment_base(tr);
2306}
2307
Avi Kivity04d2cc72007-09-10 18:10:54 +03002308static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002309{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002311 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002312
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002313 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002314 return;
2315
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002316 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002317 /*
2318 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2319 * allow segment selectors with cpl > 0 or ti == 1.
2320 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002321 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002322 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002323 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002324 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002325 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002326 vmx->host_state.fs_reload_needed = 0;
2327 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002328 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002329 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002330 }
Avi Kivity9581d442010-10-19 16:46:55 +02002331 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002332 if (!(vmx->host_state.gs_sel & 7))
2333 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002334 else {
2335 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002336 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002337 }
2338
2339#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002340 savesegment(ds, vmx->host_state.ds_sel);
2341 savesegment(es, vmx->host_state.es_sel);
2342#endif
2343
2344#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002345 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2346 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2347#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002348 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2349 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002350#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002351
2352#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002353 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2354 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002355 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002356#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002357 if (boot_cpu_has(X86_FEATURE_MPX))
2358 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002359 for (i = 0; i < vmx->save_nmsrs; ++i)
2360 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002361 vmx->guest_msrs[i].data,
2362 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002363}
2364
Avi Kivitya9b21b62008-06-24 11:48:49 +03002365static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002366{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002367 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002368 return;
2369
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002370 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002371 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002372#ifdef CONFIG_X86_64
2373 if (is_long_mode(&vmx->vcpu))
2374 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2375#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002376 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002377 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002378#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002379 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002380#else
2381 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002382#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002383 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002384 if (vmx->host_state.fs_reload_needed)
2385 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002386#ifdef CONFIG_X86_64
2387 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2388 loadsegment(ds, vmx->host_state.ds_sel);
2389 loadsegment(es, vmx->host_state.es_sel);
2390 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002391#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002392 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002393#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002394 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002395#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002396 if (vmx->host_state.msr_host_bndcfgs)
2397 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002398 /*
2399 * If the FPU is not active (through the host task or
2400 * the guest vcpu), then restore the cr0.TS bit.
2401 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002402 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002403 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002404 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002405}
2406
Avi Kivitya9b21b62008-06-24 11:48:49 +03002407static void vmx_load_host_state(struct vcpu_vmx *vmx)
2408{
2409 preempt_disable();
2410 __vmx_load_host_state(vmx);
2411 preempt_enable();
2412}
2413
Feng Wu28b835d2015-09-18 22:29:54 +08002414static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2415{
2416 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2417 struct pi_desc old, new;
2418 unsigned int dest;
2419
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002420 /*
2421 * In case of hot-plug or hot-unplug, we may have to undo
2422 * vmx_vcpu_pi_put even if there is no assigned device. And we
2423 * always keep PI.NDST up to date for simplicity: it makes the
2424 * code easier, and CPU migration is not a fast path.
2425 */
2426 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002427 return;
2428
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002429 /*
2430 * First handle the simple case where no cmpxchg is necessary; just
2431 * allow posting non-urgent interrupts.
2432 *
2433 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2434 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2435 * expects the VCPU to be on the blocked_vcpu_list that matches
2436 * PI.NDST.
2437 */
2438 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2439 vcpu->cpu == cpu) {
2440 pi_clear_sn(pi_desc);
2441 return;
2442 }
2443
2444 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002445 do {
2446 old.control = new.control = pi_desc->control;
2447
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002448 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002449
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002450 if (x2apic_enabled())
2451 new.ndst = dest;
2452 else
2453 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002454
Feng Wu28b835d2015-09-18 22:29:54 +08002455 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002456 } while (cmpxchg64(&pi_desc->control, old.control,
2457 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002458}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002459
Peter Feinerc95ba922016-08-17 09:36:47 -07002460static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2461{
2462 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2463 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2464}
2465
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466/*
2467 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2468 * vcpu mutex is already taken.
2469 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002470static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002472 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002473 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002474 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002476 if (!vmm_exclusive)
2477 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002478 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002479 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002481 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002482 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002483 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002484
2485 /*
2486 * Read loaded_vmcs->cpu should be before fetching
2487 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2488 * See the comments in __loaded_vmcs_clear().
2489 */
2490 smp_rmb();
2491
Nadav Har'Eld462b812011-05-24 15:26:10 +03002492 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2493 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002494 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002495 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002496 }
2497
2498 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2499 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2500 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002501 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002502 }
2503
2504 if (!already_loaded) {
2505 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2506 unsigned long sysenter_esp;
2507
2508 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002509
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 /*
2511 * Linux uses per-cpu TSS and GDT, so set these when switching
2512 * processors.
2513 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002514 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002515 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516
2517 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2518 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002519
Nadav Har'Eld462b812011-05-24 15:26:10 +03002520 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 }
Feng Wu28b835d2015-09-18 22:29:54 +08002522
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002523 /* Setup TSC multiplier */
2524 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002525 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2526 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002527
Feng Wu28b835d2015-09-18 22:29:54 +08002528 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002529 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002530}
2531
2532static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2533{
2534 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2535
2536 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002537 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2538 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002539 return;
2540
2541 /* Set SN when the vCPU is preempted */
2542 if (vcpu->preempted)
2543 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544}
2545
2546static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2547{
Feng Wu28b835d2015-09-18 22:29:54 +08002548 vmx_vcpu_pi_put(vcpu);
2549
Avi Kivitya9b21b62008-06-24 11:48:49 +03002550 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002551 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002552 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2553 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002554 kvm_cpu_vmxoff();
2555 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556}
2557
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002558static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2559{
Avi Kivity81231c62010-01-24 16:26:40 +02002560 ulong cr0;
2561
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002562 if (vcpu->fpu_active)
2563 return;
2564 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002565 cr0 = vmcs_readl(GUEST_CR0);
2566 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2567 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2568 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002569 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002570 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002571 if (is_guest_mode(vcpu))
2572 vcpu->arch.cr0_guest_owned_bits &=
2573 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002574 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002575}
2576
Avi Kivityedcafe32009-12-30 18:07:40 +02002577static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2578
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002579/*
2580 * Return the cr0 value that a nested guest would read. This is a combination
2581 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2582 * its hypervisor (cr0_read_shadow).
2583 */
2584static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2585{
2586 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2587 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2588}
2589static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2590{
2591 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2592 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2593}
2594
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002595static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2596{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002597 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2598 * set this *before* calling this function.
2599 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002600 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002601 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002602 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002603 vcpu->arch.cr0_guest_owned_bits = 0;
2604 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002605 if (is_guest_mode(vcpu)) {
2606 /*
2607 * L1's specified read shadow might not contain the TS bit,
2608 * so now that we turned on shadowing of this bit, we need to
2609 * set this bit of the shadow. Like in nested_vmx_run we need
2610 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2611 * up-to-date here because we just decached cr0.TS (and we'll
2612 * only update vmcs12->guest_cr0 on nested exit).
2613 */
2614 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2615 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2616 (vcpu->arch.cr0 & X86_CR0_TS);
2617 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2618 } else
2619 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002620}
2621
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2623{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002624 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002625
Avi Kivity6de12732011-03-07 12:51:22 +02002626 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2627 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2628 rflags = vmcs_readl(GUEST_RFLAGS);
2629 if (to_vmx(vcpu)->rmode.vm86_active) {
2630 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2631 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2632 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2633 }
2634 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002635 }
Avi Kivity6de12732011-03-07 12:51:22 +02002636 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637}
2638
2639static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2640{
Avi Kivity6de12732011-03-07 12:51:22 +02002641 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2642 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002643 if (to_vmx(vcpu)->rmode.vm86_active) {
2644 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002645 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002646 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 vmcs_writel(GUEST_RFLAGS, rflags);
2648}
2649
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002650static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2651{
2652 return to_vmx(vcpu)->guest_pkru;
2653}
2654
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002655static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002656{
2657 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2658 int ret = 0;
2659
2660 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002661 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002662 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002663 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002664
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002665 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002666}
2667
2668static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2669{
2670 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2671 u32 interruptibility = interruptibility_old;
2672
2673 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2674
Jan Kiszka48005f62010-02-19 19:38:07 +01002675 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002676 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002677 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002678 interruptibility |= GUEST_INTR_STATE_STI;
2679
2680 if ((interruptibility != interruptibility_old))
2681 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2682}
2683
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2685{
2686 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002688 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002690 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691
Glauber Costa2809f5d2009-05-12 16:21:05 -04002692 /* skipping an emulated instruction also counts */
2693 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694}
2695
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002696/*
2697 * KVM wants to inject page-faults which it got to the guest. This function
2698 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002699 */
Gleb Natapove011c662013-09-25 12:51:35 +03002700static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002701{
2702 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2703
Gleb Natapove011c662013-09-25 12:51:35 +03002704 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002705 return 0;
2706
Wanpeng Lia29fd272017-06-05 05:19:09 -07002707 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002708 vmcs_read32(VM_EXIT_INTR_INFO),
2709 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002710 return 1;
2711}
2712
Avi Kivity298101d2007-11-25 13:41:11 +02002713static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002714 bool has_error_code, u32 error_code,
2715 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002716{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002717 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002718 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002719
Gleb Natapove011c662013-09-25 12:51:35 +03002720 if (!reinject && is_guest_mode(vcpu) &&
2721 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002722 return;
2723
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002724 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002725 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002726 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2727 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002728
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002729 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002730 int inc_eip = 0;
2731 if (kvm_exception_is_soft(nr))
2732 inc_eip = vcpu->arch.event_exit_inst_len;
2733 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002734 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002735 return;
2736 }
2737
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002738 WARN_ON_ONCE(vmx->emulation_required);
2739
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002740 if (kvm_exception_is_soft(nr)) {
2741 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2742 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002743 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2744 } else
2745 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2746
2747 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002748}
2749
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002750static bool vmx_rdtscp_supported(void)
2751{
2752 return cpu_has_vmx_rdtscp();
2753}
2754
Mao, Junjiead756a12012-07-02 01:18:48 +00002755static bool vmx_invpcid_supported(void)
2756{
2757 return cpu_has_vmx_invpcid() && enable_ept;
2758}
2759
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760/*
Eddie Donga75beee2007-05-17 18:55:15 +03002761 * Swap MSR entry in host/guest MSR entry array.
2762 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002763static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002764{
Avi Kivity26bb0982009-09-07 11:14:12 +03002765 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002766
2767 tmp = vmx->guest_msrs[to];
2768 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2769 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002770}
2771
2772/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002773 * Set up the vmcs to automatically save and restore system
2774 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2775 * mode, as fiddling with msrs is very expensive.
2776 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002777static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002778{
Avi Kivity26bb0982009-09-07 11:14:12 +03002779 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002780
Eddie Donga75beee2007-05-17 18:55:15 +03002781 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002782#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002783 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002784 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002785 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002786 move_msr_up(vmx, index, save_nmsrs++);
2787 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002788 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002789 move_msr_up(vmx, index, save_nmsrs++);
2790 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002791 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002792 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002793 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002794 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002795 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002796 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002797 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002798 * if efer.sce is enabled.
2799 */
Brian Gerst8c065852010-07-17 09:03:26 -04002800 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002801 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002802 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002803 }
Eddie Donga75beee2007-05-17 18:55:15 +03002804#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002805 index = __find_msr_index(vmx, MSR_EFER);
2806 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002807 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002808
Avi Kivity26bb0982009-09-07 11:14:12 +03002809 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002810
Yang Zhang8d146952013-01-25 10:18:50 +08002811 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002812 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002813}
2814
2815/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002817 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2818 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002820static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821{
2822 u64 host_tsc, tsc_offset;
2823
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002824 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002826 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827}
2828
2829/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002830 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002832static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002834 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002835 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002836 * We're here if L1 chose not to trap WRMSR to TSC. According
2837 * to the spec, this should set L1's TSC; The offset that L1
2838 * set for L2 remains unchanged, and still needs to be added
2839 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002840 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002841 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002842 /* recalculate vmcs02.TSC_OFFSET: */
2843 vmcs12 = get_vmcs12(vcpu);
2844 vmcs_write64(TSC_OFFSET, offset +
2845 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2846 vmcs12->tsc_offset : 0));
2847 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002848 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2849 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002850 vmcs_write64(TSC_OFFSET, offset);
2851 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852}
2853
Nadav Har'El801d3422011-05-25 23:02:23 +03002854static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2855{
2856 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2857 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2858}
2859
2860/*
2861 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2862 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2863 * all guests if the "nested" module option is off, and can also be disabled
2864 * for a single guest by disabling its VMX cpuid bit.
2865 */
2866static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2867{
2868 return nested && guest_cpuid_has_vmx(vcpu);
2869}
2870
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002872 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2873 * returned for the various VMX controls MSRs when nested VMX is enabled.
2874 * The same values should also be used to verify that vmcs12 control fields are
2875 * valid during nested entry from L1 to L2.
2876 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2877 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2878 * bit in the high half is on if the corresponding bit in the control field
2879 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002880 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002881static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002882{
2883 /*
2884 * Note that as a general rule, the high half of the MSRs (bits in
2885 * the control fields which may be 1) should be initialized by the
2886 * intersection of the underlying hardware's MSR (i.e., features which
2887 * can be supported) and the list of features we want to expose -
2888 * because they are known to be properly supported in our code.
2889 * Also, usually, the low half of the MSRs (bits which must be 1) can
2890 * be set to 0, meaning that L1 may turn off any of these bits. The
2891 * reason is that if one of these bits is necessary, it will appear
2892 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2893 * fields of vmcs01 and vmcs02, will turn these bits off - and
2894 * nested_vmx_exit_handled() will not pass related exits to L1.
2895 * These rules have exceptions below.
2896 */
2897
2898 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002899 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002900 vmx->nested.nested_vmx_pinbased_ctls_low,
2901 vmx->nested.nested_vmx_pinbased_ctls_high);
2902 vmx->nested.nested_vmx_pinbased_ctls_low |=
2903 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2904 vmx->nested.nested_vmx_pinbased_ctls_high &=
2905 PIN_BASED_EXT_INTR_MASK |
2906 PIN_BASED_NMI_EXITING |
2907 PIN_BASED_VIRTUAL_NMIS;
2908 vmx->nested.nested_vmx_pinbased_ctls_high |=
2909 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002910 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002911 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002912 vmx->nested.nested_vmx_pinbased_ctls_high |=
2913 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002914
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002915 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002916 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002917 vmx->nested.nested_vmx_exit_ctls_low,
2918 vmx->nested.nested_vmx_exit_ctls_high);
2919 vmx->nested.nested_vmx_exit_ctls_low =
2920 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002921
Wincy Vanb9c237b2015-02-03 23:56:30 +08002922 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002923#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002924 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002925#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002926 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002927 vmx->nested.nested_vmx_exit_ctls_high |=
2928 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002929 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002930 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2931
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002932 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002933 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002934
Jan Kiszka2996fca2014-06-16 13:59:43 +02002935 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002936 vmx->nested.nested_vmx_true_exit_ctls_low =
2937 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002938 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2939
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002940 /* entry controls */
2941 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002942 vmx->nested.nested_vmx_entry_ctls_low,
2943 vmx->nested.nested_vmx_entry_ctls_high);
2944 vmx->nested.nested_vmx_entry_ctls_low =
2945 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2946 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002947#ifdef CONFIG_X86_64
2948 VM_ENTRY_IA32E_MODE |
2949#endif
2950 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002951 vmx->nested.nested_vmx_entry_ctls_high |=
2952 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002953 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002954 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002955
Jan Kiszka2996fca2014-06-16 13:59:43 +02002956 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002957 vmx->nested.nested_vmx_true_entry_ctls_low =
2958 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002959 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2960
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002961 /* cpu-based controls */
2962 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002963 vmx->nested.nested_vmx_procbased_ctls_low,
2964 vmx->nested.nested_vmx_procbased_ctls_high);
2965 vmx->nested.nested_vmx_procbased_ctls_low =
2966 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2967 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002968 CPU_BASED_VIRTUAL_INTR_PENDING |
2969 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002970 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2971 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2972 CPU_BASED_CR3_STORE_EXITING |
2973#ifdef CONFIG_X86_64
2974 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2975#endif
2976 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002977 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2978 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2979 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2980 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002981 /*
2982 * We can allow some features even when not supported by the
2983 * hardware. For example, L1 can specify an MSR bitmap - and we
2984 * can use it to avoid exits to L1 - even when L0 runs L2
2985 * without MSR bitmaps.
2986 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002987 vmx->nested.nested_vmx_procbased_ctls_high |=
2988 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002989 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002990
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002991 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002992 vmx->nested.nested_vmx_true_procbased_ctls_low =
2993 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002994 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2995
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002996 /* secondary cpu-based controls */
2997 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002998 vmx->nested.nested_vmx_secondary_ctls_low,
2999 vmx->nested.nested_vmx_secondary_ctls_high);
3000 vmx->nested.nested_vmx_secondary_ctls_low = 0;
3001 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003002 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01003003 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08003004 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07003005 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08003006 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003007 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08003008 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07003009 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003010
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003011 if (enable_ept) {
3012 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003013 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003014 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003015 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01003016 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
3017 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003018 if (cpu_has_vmx_ept_execute_only())
3019 vmx->nested.nested_vmx_ept_caps |=
3020 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003021 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04003022 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3023 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003024 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08003025 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003026
Paolo Bonzinief697a72016-03-18 16:58:38 +01003027 /*
3028 * Old versions of KVM use the single-context version without
3029 * checking for support, so declare that it is supported even
3030 * though it is treated as global context. The alternative is
3031 * not failing the single-context invvpid, and it is worse.
3032 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07003033 if (enable_vpid)
3034 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01003035 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07003036 else
3037 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003038
Radim Krčmář0790ec12015-03-17 14:02:32 +01003039 if (enable_unrestricted_guest)
3040 vmx->nested.nested_vmx_secondary_ctls_high |=
3041 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3042
Jan Kiszkac18911a2013-03-13 16:06:41 +01003043 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003044 rdmsr(MSR_IA32_VMX_MISC,
3045 vmx->nested.nested_vmx_misc_low,
3046 vmx->nested.nested_vmx_misc_high);
3047 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
3048 vmx->nested.nested_vmx_misc_low |=
3049 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003050 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003051 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003052}
3053
3054static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3055{
3056 /*
3057 * Bits 0 in high must be 0, and bits 1 in low must be 1.
3058 */
3059 return ((control & high) | low) == control;
3060}
3061
3062static inline u64 vmx_control_msr(u32 low, u32 high)
3063{
3064 return low | ((u64)high << 32);
3065}
3066
Jan Kiszkacae50132014-01-04 18:47:22 +01003067/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003068static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3069{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003070 struct vcpu_vmx *vmx = to_vmx(vcpu);
3071
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003072 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003073 case MSR_IA32_VMX_BASIC:
3074 /*
3075 * This MSR reports some information about VMX support. We
3076 * should return information about the VMX we emulate for the
3077 * guest, and the VMCS structure we give it - not about the
3078 * VMX support of the underlying hardware.
3079 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003080 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003081 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3082 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003083 if (cpu_has_vmx_basic_inout())
3084 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003085 break;
3086 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3087 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003088 *pdata = vmx_control_msr(
3089 vmx->nested.nested_vmx_pinbased_ctls_low,
3090 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003091 break;
3092 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003093 *pdata = vmx_control_msr(
3094 vmx->nested.nested_vmx_true_procbased_ctls_low,
3095 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02003096 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003097 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003098 *pdata = vmx_control_msr(
3099 vmx->nested.nested_vmx_procbased_ctls_low,
3100 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003101 break;
3102 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003103 *pdata = vmx_control_msr(
3104 vmx->nested.nested_vmx_true_exit_ctls_low,
3105 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003106 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003107 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003108 *pdata = vmx_control_msr(
3109 vmx->nested.nested_vmx_exit_ctls_low,
3110 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003111 break;
3112 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003113 *pdata = vmx_control_msr(
3114 vmx->nested.nested_vmx_true_entry_ctls_low,
3115 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003116 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003117 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003118 *pdata = vmx_control_msr(
3119 vmx->nested.nested_vmx_entry_ctls_low,
3120 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003121 break;
3122 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003123 *pdata = vmx_control_msr(
3124 vmx->nested.nested_vmx_misc_low,
3125 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003126 break;
3127 /*
3128 * These MSRs specify bits which the guest must keep fixed (on or off)
3129 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3130 * We picked the standard core2 setting.
3131 */
3132#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3133#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3134 case MSR_IA32_VMX_CR0_FIXED0:
3135 *pdata = VMXON_CR0_ALWAYSON;
3136 break;
3137 case MSR_IA32_VMX_CR0_FIXED1:
3138 *pdata = -1ULL;
3139 break;
3140 case MSR_IA32_VMX_CR4_FIXED0:
3141 *pdata = VMXON_CR4_ALWAYSON;
3142 break;
3143 case MSR_IA32_VMX_CR4_FIXED1:
3144 *pdata = -1ULL;
3145 break;
3146 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003147 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003148 break;
3149 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003150 *pdata = vmx_control_msr(
3151 vmx->nested.nested_vmx_secondary_ctls_low,
3152 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003153 break;
3154 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003155 *pdata = vmx->nested.nested_vmx_ept_caps |
3156 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003157 break;
3158 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003159 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003160 }
3161
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003162 return 0;
3163}
3164
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003165static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3166 uint64_t val)
3167{
3168 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3169
3170 return !(val & ~valid_bits);
3171}
3172
Tom Lendacky62d88fc2018-02-21 13:39:51 -06003173static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3174{
3175 return 1;
3176}
3177
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003178/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 * Reads an msr value (of 'msr_index') into 'pdata'.
3180 * Returns 0 on success, non-0 otherwise.
3181 * Assumes vcpu_load() was already called.
3182 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003183static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184{
Avi Kivity26bb0982009-09-07 11:14:12 +03003185 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003187 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003188#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003190 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 break;
3192 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003193 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003195 case MSR_KERNEL_GS_BASE:
3196 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003197 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003198 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003199#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003201 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303202 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003203 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003205 case MSR_IA32_SPEC_CTRL:
3206 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003207 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003208 return 1;
3209
3210 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3211 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003212 case MSR_IA32_ARCH_CAPABILITIES:
3213 if (!msr_info->host_initiated &&
3214 !guest_cpuid_has_arch_capabilities(vcpu))
3215 return 1;
3216 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3217 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003219 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 break;
3221 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003222 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 break;
3224 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003225 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003227 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003228 if (!kvm_mpx_supported() ||
3229 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003230 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003231 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003232 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003233 case MSR_IA32_MCG_EXT_CTL:
3234 if (!msr_info->host_initiated &&
3235 !(to_vmx(vcpu)->msr_ia32_feature_control &
3236 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003237 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003238 msr_info->data = vcpu->arch.mcg_ext_ctl;
3239 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003240 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003241 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003242 break;
3243 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3244 if (!nested_vmx_allowed(vcpu))
3245 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003246 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003247 case MSR_IA32_XSS:
3248 if (!vmx_xsaves_supported())
3249 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003250 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003251 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003252 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003253 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003254 return 1;
3255 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003257 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003258 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003259 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003260 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003262 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263 }
3264
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265 return 0;
3266}
3267
Jan Kiszkacae50132014-01-04 18:47:22 +01003268static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3269
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270/*
3271 * Writes msr value into into the appropriate "register".
3272 * Returns 0 on success, non-0 otherwise.
3273 * Assumes vcpu_load() was already called.
3274 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003275static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003277 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003278 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003279 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003280 u32 msr_index = msr_info->index;
3281 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003282
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003284 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003285 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003286 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003287#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003289 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290 vmcs_writel(GUEST_FS_BASE, data);
3291 break;
3292 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003293 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294 vmcs_writel(GUEST_GS_BASE, data);
3295 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003296 case MSR_KERNEL_GS_BASE:
3297 vmx_load_host_state(vmx);
3298 vmx->msr_guest_kernel_gs_base = data;
3299 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300#endif
3301 case MSR_IA32_SYSENTER_CS:
3302 vmcs_write32(GUEST_SYSENTER_CS, data);
3303 break;
3304 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003305 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 break;
3307 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003308 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003310 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003311 if (!kvm_mpx_supported() ||
3312 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003313 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003314 if (is_noncanonical_address(data & PAGE_MASK) ||
3315 (data & MSR_IA32_BNDCFGS_RSVD))
3316 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003317 vmcs_write64(GUEST_BNDCFGS, data);
3318 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303319 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003320 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003322 case MSR_IA32_SPEC_CTRL:
3323 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003324 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003325 return 1;
3326
3327 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003328 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003329 return 1;
3330
3331 vmx->spec_ctrl = data;
3332
3333 if (!data)
3334 break;
3335
3336 /*
3337 * For non-nested:
3338 * When it's written (to non-zero) for the first time, pass
3339 * it through.
3340 *
3341 * For nested:
3342 * The handling of the MSR bitmap for L2 guests is done in
3343 * nested_vmx_merge_msr_bitmap. We should not touch the
3344 * vmcs02.msr_bitmap here since it gets completely overwritten
3345 * in the merging. We update the vmcs01 here for L1 as well
3346 * since it will end up touching the MSR anyway now.
3347 */
3348 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3349 MSR_IA32_SPEC_CTRL,
3350 MSR_TYPE_RW);
3351 break;
Ashok Raj70131292018-02-01 22:59:43 +01003352 case MSR_IA32_PRED_CMD:
3353 if (!msr_info->host_initiated &&
3354 !guest_cpuid_has_ibpb(vcpu))
3355 return 1;
3356
3357 if (data & ~PRED_CMD_IBPB)
3358 return 1;
3359
3360 if (!data)
3361 break;
3362
3363 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3364
3365 /*
3366 * For non-nested:
3367 * When it's written (to non-zero) for the first time, pass
3368 * it through.
3369 *
3370 * For nested:
3371 * The handling of the MSR bitmap for L2 guests is done in
3372 * nested_vmx_merge_msr_bitmap. We should not touch the
3373 * vmcs02.msr_bitmap here since it gets completely overwritten
3374 * in the merging.
3375 */
3376 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3377 MSR_TYPE_W);
3378 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003379 case MSR_IA32_ARCH_CAPABILITIES:
3380 if (!msr_info->host_initiated)
3381 return 1;
3382 vmx->arch_capabilities = data;
3383 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003384 case MSR_IA32_CR_PAT:
3385 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003386 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3387 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003388 vmcs_write64(GUEST_IA32_PAT, data);
3389 vcpu->arch.pat = data;
3390 break;
3391 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003392 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003393 break;
Will Auldba904632012-11-29 12:42:50 -08003394 case MSR_IA32_TSC_ADJUST:
3395 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003396 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003397 case MSR_IA32_MCG_EXT_CTL:
3398 if ((!msr_info->host_initiated &&
3399 !(to_vmx(vcpu)->msr_ia32_feature_control &
3400 FEATURE_CONTROL_LMCE)) ||
3401 (data & ~MCG_EXT_CTL_LMCE_EN))
3402 return 1;
3403 vcpu->arch.mcg_ext_ctl = data;
3404 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003405 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003406 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003407 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003408 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3409 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003410 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003411 if (msr_info->host_initiated && data == 0)
3412 vmx_leave_nested(vcpu);
3413 break;
3414 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3415 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003416 case MSR_IA32_XSS:
3417 if (!vmx_xsaves_supported())
3418 return 1;
3419 /*
3420 * The only supported bit as of Skylake is bit 8, but
3421 * it is not supported on KVM.
3422 */
3423 if (data != 0)
3424 return 1;
3425 vcpu->arch.ia32_xss = data;
3426 if (vcpu->arch.ia32_xss != host_xss)
3427 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04003428 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08003429 else
3430 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3431 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003432 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003433 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003434 return 1;
3435 /* Check reserved bit, higher 32 bits should be zero */
3436 if ((data >> 32) != 0)
3437 return 1;
3438 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003440 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003441 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003442 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003443 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003444 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3445 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003446 ret = kvm_set_shared_msr(msr->index, msr->data,
3447 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003448 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003449 if (ret)
3450 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003451 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003452 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003454 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455 }
3456
Eddie Dong2cc51562007-05-21 07:28:09 +03003457 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458}
3459
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003460static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003462 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3463 switch (reg) {
3464 case VCPU_REGS_RSP:
3465 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3466 break;
3467 case VCPU_REGS_RIP:
3468 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3469 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003470 case VCPU_EXREG_PDPTR:
3471 if (enable_ept)
3472 ept_save_pdptrs(vcpu);
3473 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003474 default:
3475 break;
3476 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477}
3478
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479static __init int cpu_has_kvm_support(void)
3480{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003481 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482}
3483
3484static __init int vmx_disabled_by_bios(void)
3485{
3486 u64 msr;
3487
3488 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003489 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003490 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003491 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3492 && tboot_enabled())
3493 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003494 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003495 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003496 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003497 && !tboot_enabled()) {
3498 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003499 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003500 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003501 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003502 /* launched w/o TXT and VMX disabled */
3503 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3504 && !tboot_enabled())
3505 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003506 }
3507
3508 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509}
3510
Dongxiao Xu7725b892010-05-11 18:29:38 +08003511static void kvm_cpu_vmxon(u64 addr)
3512{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003513 intel_pt_handle_vmx(1);
3514
Dongxiao Xu7725b892010-05-11 18:29:38 +08003515 asm volatile (ASM_VMX_VMXON_RAX
3516 : : "a"(&addr), "m"(addr)
3517 : "memory", "cc");
3518}
3519
Radim Krčmář13a34e02014-08-28 15:13:03 +02003520static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521{
3522 int cpu = raw_smp_processor_id();
3523 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003524 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003526 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003527 return -EBUSY;
3528
Nadav Har'Eld462b812011-05-24 15:26:10 +03003529 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003530 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3531 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003532
3533 /*
3534 * Now we can enable the vmclear operation in kdump
3535 * since the loaded_vmcss_on_cpu list on this cpu
3536 * has been initialized.
3537 *
3538 * Though the cpu is not in VMX operation now, there
3539 * is no problem to enable the vmclear operation
3540 * for the loaded_vmcss_on_cpu list is empty!
3541 */
3542 crash_enable_local_vmclear(cpu);
3543
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003545
3546 test_bits = FEATURE_CONTROL_LOCKED;
3547 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3548 if (tboot_enabled())
3549 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3550
3551 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003553 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3554 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003555 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003556
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003557 if (vmm_exclusive) {
3558 kvm_cpu_vmxon(phys_addr);
3559 ept_sync_global();
3560 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003561
Christoph Lameter89cbc762014-08-17 12:30:40 -05003562 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003563
Alexander Graf10474ae2009-09-15 11:37:46 +02003564 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003565}
3566
Nadav Har'Eld462b812011-05-24 15:26:10 +03003567static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003568{
3569 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003570 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003571
Nadav Har'Eld462b812011-05-24 15:26:10 +03003572 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3573 loaded_vmcss_on_cpu_link)
3574 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003575}
3576
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003577
3578/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3579 * tricks.
3580 */
3581static void kvm_cpu_vmxoff(void)
3582{
3583 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003584
3585 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003586}
3587
Radim Krčmář13a34e02014-08-28 15:13:03 +02003588static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003589{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003590 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003591 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003592 kvm_cpu_vmxoff();
3593 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003594 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003595}
3596
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003597static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003598 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003599{
3600 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003601 u32 ctl = ctl_min | ctl_opt;
3602
3603 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3604
3605 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3606 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3607
3608 /* Ensure minimum (required) set of control bits are supported. */
3609 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003610 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003611
3612 *result = ctl;
3613 return 0;
3614}
3615
Avi Kivity110312c2010-12-21 12:54:20 +02003616static __init bool allow_1_setting(u32 msr, u32 ctl)
3617{
3618 u32 vmx_msr_low, vmx_msr_high;
3619
3620 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3621 return vmx_msr_high & ctl;
3622}
3623
Yang, Sheng002c7f72007-07-31 14:23:01 +03003624static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003625{
3626 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003627 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003628 u32 _pin_based_exec_control = 0;
3629 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003630 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003631 u32 _vmexit_control = 0;
3632 u32 _vmentry_control = 0;
3633
Raghavendra K T10166742012-02-07 23:19:20 +05303634 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003635#ifdef CONFIG_X86_64
3636 CPU_BASED_CR8_LOAD_EXITING |
3637 CPU_BASED_CR8_STORE_EXITING |
3638#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003639 CPU_BASED_CR3_LOAD_EXITING |
3640 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003641 CPU_BASED_USE_IO_BITMAPS |
3642 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003643 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003644 CPU_BASED_MWAIT_EXITING |
3645 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003646 CPU_BASED_INVLPG_EXITING |
3647 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003648
Sheng Yangf78e0e22007-10-29 09:40:42 +08003649 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003650 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003651 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003652 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3653 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003654 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003655#ifdef CONFIG_X86_64
3656 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3657 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3658 ~CPU_BASED_CR8_STORE_EXITING;
3659#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003660 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003661 min2 = 0;
3662 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003663 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003664 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003665 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003666 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003667 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003668 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003669 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003670 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003671 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003672 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003673 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003674 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003675 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003676 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003677 if (adjust_vmx_controls(min2, opt2,
3678 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003679 &_cpu_based_2nd_exec_control) < 0)
3680 return -EIO;
3681 }
3682#ifndef CONFIG_X86_64
3683 if (!(_cpu_based_2nd_exec_control &
3684 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3685 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3686#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003687
3688 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3689 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003690 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003691 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3692 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003693
Sheng Yangd56f5462008-04-25 10:13:16 +08003694 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003695 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3696 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003697 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3698 CPU_BASED_CR3_STORE_EXITING |
3699 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003700 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3701 vmx_capability.ept, vmx_capability.vpid);
3702 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003703
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003704 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003705#ifdef CONFIG_X86_64
3706 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3707#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003708 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003709 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003710 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3711 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003712 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003713
Yang Zhang01e439b2013-04-11 19:25:12 +08003714 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003715 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3716 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003717 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3718 &_pin_based_exec_control) < 0)
3719 return -EIO;
3720
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003721 if (cpu_has_broken_vmx_preemption_timer())
3722 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003723 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003724 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003725 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3726
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003727 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003728 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003729 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3730 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003731 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003732
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003733 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003734
3735 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3736 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003737 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003738
3739#ifdef CONFIG_X86_64
3740 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3741 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003742 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003743#endif
3744
3745 /* Require Write-Back (WB) memory type for VMCS accesses. */
3746 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003747 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003748
Yang, Sheng002c7f72007-07-31 14:23:01 +03003749 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003750 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003751 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003752 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003753
Yang, Sheng002c7f72007-07-31 14:23:01 +03003754 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3755 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003756 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003757 vmcs_conf->vmexit_ctrl = _vmexit_control;
3758 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003759
Avi Kivity110312c2010-12-21 12:54:20 +02003760 cpu_has_load_ia32_efer =
3761 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3762 VM_ENTRY_LOAD_IA32_EFER)
3763 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3764 VM_EXIT_LOAD_IA32_EFER);
3765
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003766 cpu_has_load_perf_global_ctrl =
3767 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3768 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3769 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3770 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3771
3772 /*
3773 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003774 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003775 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3776 *
3777 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3778 *
3779 * AAK155 (model 26)
3780 * AAP115 (model 30)
3781 * AAT100 (model 37)
3782 * BC86,AAY89,BD102 (model 44)
3783 * BA97 (model 46)
3784 *
3785 */
3786 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3787 switch (boot_cpu_data.x86_model) {
3788 case 26:
3789 case 30:
3790 case 37:
3791 case 44:
3792 case 46:
3793 cpu_has_load_perf_global_ctrl = false;
3794 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3795 "does not work properly. Using workaround\n");
3796 break;
3797 default:
3798 break;
3799 }
3800 }
3801
Borislav Petkov782511b2016-04-04 22:25:03 +02003802 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003803 rdmsrl(MSR_IA32_XSS, host_xss);
3804
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003805 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003806}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807
3808static struct vmcs *alloc_vmcs_cpu(int cpu)
3809{
3810 int node = cpu_to_node(cpu);
3811 struct page *pages;
3812 struct vmcs *vmcs;
3813
Vlastimil Babka96db8002015-09-08 15:03:50 -07003814 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815 if (!pages)
3816 return NULL;
3817 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003818 memset(vmcs, 0, vmcs_config.size);
3819 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820 return vmcs;
3821}
3822
Avi Kivity6aa8b732006-12-10 02:21:36 -08003823static void free_vmcs(struct vmcs *vmcs)
3824{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003825 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826}
3827
Nadav Har'Eld462b812011-05-24 15:26:10 +03003828/*
3829 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3830 */
3831static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3832{
3833 if (!loaded_vmcs->vmcs)
3834 return;
3835 loaded_vmcs_clear(loaded_vmcs);
3836 free_vmcs(loaded_vmcs->vmcs);
3837 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003838 if (loaded_vmcs->msr_bitmap)
3839 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003840 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003841}
3842
Paolo Bonziniff546f92018-01-11 12:16:15 +01003843static struct vmcs *alloc_vmcs(void)
3844{
3845 return alloc_vmcs_cpu(raw_smp_processor_id());
3846}
3847
3848static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3849{
3850 loaded_vmcs->vmcs = alloc_vmcs();
3851 if (!loaded_vmcs->vmcs)
3852 return -ENOMEM;
3853
3854 loaded_vmcs->shadow_vmcs = NULL;
3855 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003856
3857 if (cpu_has_vmx_msr_bitmap()) {
3858 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3859 if (!loaded_vmcs->msr_bitmap)
3860 goto out_vmcs;
3861 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3862 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003863 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003864
3865out_vmcs:
3866 free_loaded_vmcs(loaded_vmcs);
3867 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003868}
3869
Sam Ravnborg39959582007-06-01 00:47:13 -07003870static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871{
3872 int cpu;
3873
Zachary Amsden3230bb42009-09-29 11:38:37 -10003874 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003876 per_cpu(vmxarea, cpu) = NULL;
3877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878}
3879
Bandan Dasfe2b2012014-04-21 15:20:14 -04003880static void init_vmcs_shadow_fields(void)
3881{
3882 int i, j;
3883
3884 /* No checks for read only fields yet */
3885
3886 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3887 switch (shadow_read_write_fields[i]) {
3888 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003889 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003890 continue;
3891 break;
3892 default:
3893 break;
3894 }
3895
3896 if (j < i)
3897 shadow_read_write_fields[j] =
3898 shadow_read_write_fields[i];
3899 j++;
3900 }
3901 max_shadow_read_write_fields = j;
3902
3903 /* shadowed fields guest access without vmexit */
3904 for (i = 0; i < max_shadow_read_write_fields; i++) {
3905 clear_bit(shadow_read_write_fields[i],
3906 vmx_vmwrite_bitmap);
3907 clear_bit(shadow_read_write_fields[i],
3908 vmx_vmread_bitmap);
3909 }
3910 for (i = 0; i < max_shadow_read_only_fields; i++)
3911 clear_bit(shadow_read_only_fields[i],
3912 vmx_vmread_bitmap);
3913}
3914
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915static __init int alloc_kvm_area(void)
3916{
3917 int cpu;
3918
Zachary Amsden3230bb42009-09-29 11:38:37 -10003919 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920 struct vmcs *vmcs;
3921
3922 vmcs = alloc_vmcs_cpu(cpu);
3923 if (!vmcs) {
3924 free_kvm_area();
3925 return -ENOMEM;
3926 }
3927
3928 per_cpu(vmxarea, cpu) = vmcs;
3929 }
3930 return 0;
3931}
3932
Gleb Natapov14168782013-01-21 15:36:49 +02003933static bool emulation_required(struct kvm_vcpu *vcpu)
3934{
3935 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3936}
3937
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003938static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003939 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003940{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003941 if (!emulate_invalid_guest_state) {
3942 /*
3943 * CS and SS RPL should be equal during guest entry according
3944 * to VMX spec, but in reality it is not always so. Since vcpu
3945 * is in the middle of the transition from real mode to
3946 * protected mode it is safe to assume that RPL 0 is a good
3947 * default value.
3948 */
3949 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003950 save->selector &= ~SEGMENT_RPL_MASK;
3951 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003952 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003954 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955}
3956
3957static void enter_pmode(struct kvm_vcpu *vcpu)
3958{
3959 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003960 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961
Gleb Natapovd99e4152012-12-20 16:57:45 +02003962 /*
3963 * Update real mode segment cache. It may be not up-to-date if sement
3964 * register was written while vcpu was in a guest mode.
3965 */
3966 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3967 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3968 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3969 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3970 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3971 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3972
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003973 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003974
Avi Kivity2fb92db2011-04-27 19:42:18 +03003975 vmx_segment_cache_clear(vmx);
3976
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003977 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978
3979 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003980 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3981 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982 vmcs_writel(GUEST_RFLAGS, flags);
3983
Rusty Russell66aee912007-07-17 23:34:16 +10003984 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3985 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986
3987 update_exception_bitmap(vcpu);
3988
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003989 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3990 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3991 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3992 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3993 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3994 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995}
3996
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003997static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998{
Mathias Krause772e0312012-08-30 01:30:19 +02003999 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004000 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004001
Gleb Natapovd99e4152012-12-20 16:57:45 +02004002 var.dpl = 0x3;
4003 if (seg == VCPU_SREG_CS)
4004 var.type = 0x3;
4005
4006 if (!emulate_invalid_guest_state) {
4007 var.selector = var.base >> 4;
4008 var.base = var.base & 0xffff0;
4009 var.limit = 0xffff;
4010 var.g = 0;
4011 var.db = 0;
4012 var.present = 1;
4013 var.s = 1;
4014 var.l = 0;
4015 var.unusable = 0;
4016 var.type = 0x3;
4017 var.avl = 0;
4018 if (save->base & 0xf)
4019 printk_once(KERN_WARNING "kvm: segment base is not "
4020 "paragraph aligned when entering "
4021 "protected mode (seg=%d)", seg);
4022 }
4023
4024 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05004025 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004026 vmcs_write32(sf->limit, var.limit);
4027 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004028}
4029
4030static void enter_rmode(struct kvm_vcpu *vcpu)
4031{
4032 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004033 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004035 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4036 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4037 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4038 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4039 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004040 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4041 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004042
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004043 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044
Gleb Natapov776e58e2011-03-13 12:34:27 +02004045 /*
4046 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004047 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004048 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004049 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004050 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4051 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004052
Avi Kivity2fb92db2011-04-27 19:42:18 +03004053 vmx_segment_cache_clear(vmx);
4054
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004055 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4058
4059 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004060 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004062 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063
4064 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004065 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066 update_exception_bitmap(vcpu);
4067
Gleb Natapovd99e4152012-12-20 16:57:45 +02004068 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4069 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4070 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4071 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4072 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4073 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004074
Eddie Dong8668a3c2007-10-10 14:26:45 +08004075 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076}
4077
Amit Shah401d10d2009-02-20 22:53:37 +05304078static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4079{
4080 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004081 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4082
4083 if (!msr)
4084 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304085
Avi Kivity44ea2b12009-09-06 15:55:37 +03004086 /*
4087 * Force kernel_gs_base reloading before EFER changes, as control
4088 * of this msr depends on is_long_mode().
4089 */
4090 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004091 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304092 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004093 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304094 msr->data = efer;
4095 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004096 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304097
4098 msr->data = efer & ~EFER_LME;
4099 }
4100 setup_msrs(vmx);
4101}
4102
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004103#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104
4105static void enter_lmode(struct kvm_vcpu *vcpu)
4106{
4107 u32 guest_tr_ar;
4108
Avi Kivity2fb92db2011-04-27 19:42:18 +03004109 vmx_segment_cache_clear(to_vmx(vcpu));
4110
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004112 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004113 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4114 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004115 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004116 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4117 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004118 }
Avi Kivityda38f432010-07-06 11:30:49 +03004119 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120}
4121
4122static void exit_lmode(struct kvm_vcpu *vcpu)
4123{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004124 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004125 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126}
4127
4128#endif
4129
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004130static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004131{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004132 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004133 if (enable_ept) {
4134 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4135 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004136 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004137 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004138}
4139
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004140static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4141{
4142 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4143}
4144
Jim Mattson8386ff52017-03-16 13:53:59 -07004145static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4146{
4147 if (enable_ept)
4148 vmx_flush_tlb(vcpu);
4149}
4150
Avi Kivitye8467fd2009-12-29 18:43:06 +02004151static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4152{
4153 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4154
4155 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4156 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4157}
4158
Avi Kivityaff48ba2010-12-05 18:56:11 +02004159static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4160{
4161 if (enable_ept && is_paging(vcpu))
4162 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4163 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4164}
4165
Anthony Liguori25c4c272007-04-27 09:29:21 +03004166static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004167{
Avi Kivityfc78f512009-12-07 12:16:48 +02004168 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4169
4170 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4171 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004172}
4173
Sheng Yang14394422008-04-28 12:24:45 +08004174static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4175{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004176 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4177
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004178 if (!test_bit(VCPU_EXREG_PDPTR,
4179 (unsigned long *)&vcpu->arch.regs_dirty))
4180 return;
4181
Sheng Yang14394422008-04-28 12:24:45 +08004182 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004183 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4184 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4185 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4186 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004187 }
4188}
4189
Avi Kivity8f5d5492009-05-31 18:41:29 +03004190static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4191{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004192 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4193
Avi Kivity8f5d5492009-05-31 18:41:29 +03004194 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004195 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4196 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4197 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4198 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004199 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004200
4201 __set_bit(VCPU_EXREG_PDPTR,
4202 (unsigned long *)&vcpu->arch.regs_avail);
4203 __set_bit(VCPU_EXREG_PDPTR,
4204 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004205}
4206
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004207static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004208
4209static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4210 unsigned long cr0,
4211 struct kvm_vcpu *vcpu)
4212{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004213 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4214 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004215 if (!(cr0 & X86_CR0_PG)) {
4216 /* From paging/starting to nonpaging */
4217 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004218 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004219 (CPU_BASED_CR3_LOAD_EXITING |
4220 CPU_BASED_CR3_STORE_EXITING));
4221 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004222 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004223 } else if (!is_paging(vcpu)) {
4224 /* From nonpaging to paging */
4225 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004226 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004227 ~(CPU_BASED_CR3_LOAD_EXITING |
4228 CPU_BASED_CR3_STORE_EXITING));
4229 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004230 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004231 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004232
4233 if (!(cr0 & X86_CR0_WP))
4234 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004235}
4236
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4238{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004239 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004240 unsigned long hw_cr0;
4241
Gleb Natapov50378782013-02-04 16:00:28 +02004242 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004243 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004244 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004245 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004246 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004247
Gleb Natapov218e7632013-01-21 15:36:45 +02004248 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4249 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250
Gleb Natapov218e7632013-01-21 15:36:45 +02004251 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4252 enter_rmode(vcpu);
4253 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004255#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004256 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004257 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004259 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004260 exit_lmode(vcpu);
4261 }
4262#endif
4263
Avi Kivity089d0342009-03-23 18:26:32 +02004264 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004265 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4266
Avi Kivity02daab22009-12-30 12:40:26 +02004267 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004268 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004269
Avi Kivity6aa8b732006-12-10 02:21:36 -08004270 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004271 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004272 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004273
4274 /* depends on vcpu->arch.cr0 to be set to a new value */
4275 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276}
4277
Sheng Yang14394422008-04-28 12:24:45 +08004278static u64 construct_eptp(unsigned long root_hpa)
4279{
4280 u64 eptp;
4281
4282 /* TODO write the value reading from MSR */
4283 eptp = VMX_EPT_DEFAULT_MT |
4284 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004285 if (enable_ept_ad_bits)
4286 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004287 eptp |= (root_hpa & PAGE_MASK);
4288
4289 return eptp;
4290}
4291
Avi Kivity6aa8b732006-12-10 02:21:36 -08004292static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4293{
Sheng Yang14394422008-04-28 12:24:45 +08004294 unsigned long guest_cr3;
4295 u64 eptp;
4296
4297 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004298 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004299 eptp = construct_eptp(cr3);
4300 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004301 if (is_paging(vcpu) || is_guest_mode(vcpu))
4302 guest_cr3 = kvm_read_cr3(vcpu);
4303 else
4304 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004305 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004306 }
4307
Sheng Yang2384d2b2008-01-17 15:14:33 +08004308 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004309 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310}
4311
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004312static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004314 /*
4315 * Pass through host's Machine Check Enable value to hw_cr4, which
4316 * is in force while we are in guest mode. Do not let guests control
4317 * this bit, even if host CR4.MCE == 0.
4318 */
4319 unsigned long hw_cr4 =
4320 (cr4_read_shadow() & X86_CR4_MCE) |
4321 (cr4 & ~X86_CR4_MCE) |
4322 (to_vmx(vcpu)->rmode.vm86_active ?
4323 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004324
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004325 if (cr4 & X86_CR4_VMXE) {
4326 /*
4327 * To use VMXON (and later other VMX instructions), a guest
4328 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4329 * So basically the check on whether to allow nested VMX
4330 * is here.
4331 */
4332 if (!nested_vmx_allowed(vcpu))
4333 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004334 }
4335 if (to_vmx(vcpu)->nested.vmxon &&
4336 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004337 return 1;
4338
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004339 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004340 if (enable_ept) {
4341 if (!is_paging(vcpu)) {
4342 hw_cr4 &= ~X86_CR4_PAE;
4343 hw_cr4 |= X86_CR4_PSE;
4344 } else if (!(cr4 & X86_CR4_PAE)) {
4345 hw_cr4 &= ~X86_CR4_PAE;
4346 }
4347 }
Sheng Yang14394422008-04-28 12:24:45 +08004348
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004349 if (!enable_unrestricted_guest && !is_paging(vcpu))
4350 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004351 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4352 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4353 * to be manually disabled when guest switches to non-paging
4354 * mode.
4355 *
4356 * If !enable_unrestricted_guest, the CPU is always running
4357 * with CR0.PG=1 and CR4 needs to be modified.
4358 * If enable_unrestricted_guest, the CPU automatically
4359 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004360 */
Huaitong Handdba2622016-03-22 16:51:15 +08004361 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004362
Sheng Yang14394422008-04-28 12:24:45 +08004363 vmcs_writel(CR4_READ_SHADOW, cr4);
4364 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004365 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004366}
4367
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368static void vmx_get_segment(struct kvm_vcpu *vcpu,
4369 struct kvm_segment *var, int seg)
4370{
Avi Kivitya9179492011-01-03 14:28:52 +02004371 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004372 u32 ar;
4373
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004374 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004375 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004376 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004377 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004378 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004379 var->base = vmx_read_guest_seg_base(vmx, seg);
4380 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4381 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004382 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004383 var->base = vmx_read_guest_seg_base(vmx, seg);
4384 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4385 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4386 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004387 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004388 var->type = ar & 15;
4389 var->s = (ar >> 4) & 1;
4390 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004391 /*
4392 * Some userspaces do not preserve unusable property. Since usable
4393 * segment has to be present according to VMX spec we can use present
4394 * property to amend userspace bug by making unusable segment always
4395 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4396 * segment as unusable.
4397 */
4398 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399 var->avl = (ar >> 12) & 1;
4400 var->l = (ar >> 13) & 1;
4401 var->db = (ar >> 14) & 1;
4402 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403}
4404
Avi Kivitya9179492011-01-03 14:28:52 +02004405static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4406{
Avi Kivitya9179492011-01-03 14:28:52 +02004407 struct kvm_segment s;
4408
4409 if (to_vmx(vcpu)->rmode.vm86_active) {
4410 vmx_get_segment(vcpu, &s, seg);
4411 return s.base;
4412 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004413 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004414}
4415
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004416static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004417{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004418 struct vcpu_vmx *vmx = to_vmx(vcpu);
4419
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004420 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004421 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004422 else {
4423 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004424 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004425 }
Avi Kivity69c73022011-03-07 15:26:44 +02004426}
4427
Avi Kivity653e3102007-05-07 10:55:37 +03004428static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004429{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430 u32 ar;
4431
Avi Kivityf0495f92012-06-07 17:06:10 +03004432 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 ar = 1 << 16;
4434 else {
4435 ar = var->type & 15;
4436 ar |= (var->s & 1) << 4;
4437 ar |= (var->dpl & 3) << 5;
4438 ar |= (var->present & 1) << 7;
4439 ar |= (var->avl & 1) << 12;
4440 ar |= (var->l & 1) << 13;
4441 ar |= (var->db & 1) << 14;
4442 ar |= (var->g & 1) << 15;
4443 }
Avi Kivity653e3102007-05-07 10:55:37 +03004444
4445 return ar;
4446}
4447
4448static void vmx_set_segment(struct kvm_vcpu *vcpu,
4449 struct kvm_segment *var, int seg)
4450{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004451 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004452 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004453
Avi Kivity2fb92db2011-04-27 19:42:18 +03004454 vmx_segment_cache_clear(vmx);
4455
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004456 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4457 vmx->rmode.segs[seg] = *var;
4458 if (seg == VCPU_SREG_TR)
4459 vmcs_write16(sf->selector, var->selector);
4460 else if (var->s)
4461 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004462 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004463 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004464
Avi Kivity653e3102007-05-07 10:55:37 +03004465 vmcs_writel(sf->base, var->base);
4466 vmcs_write32(sf->limit, var->limit);
4467 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004468
4469 /*
4470 * Fix the "Accessed" bit in AR field of segment registers for older
4471 * qemu binaries.
4472 * IA32 arch specifies that at the time of processor reset the
4473 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004474 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004475 * state vmexit when "unrestricted guest" mode is turned on.
4476 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4477 * tree. Newer qemu binaries with that qemu fix would not need this
4478 * kvm hack.
4479 */
4480 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004481 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004482
Gleb Natapovf924d662012-12-12 19:10:55 +02004483 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004484
4485out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004486 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487}
4488
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4490{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004491 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004492
4493 *db = (ar >> 14) & 1;
4494 *l = (ar >> 13) & 1;
4495}
4496
Gleb Natapov89a27f42010-02-16 10:51:48 +02004497static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004498{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004499 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4500 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004501}
4502
Gleb Natapov89a27f42010-02-16 10:51:48 +02004503static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004504{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004505 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4506 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004507}
4508
Gleb Natapov89a27f42010-02-16 10:51:48 +02004509static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004510{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004511 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4512 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004513}
4514
Gleb Natapov89a27f42010-02-16 10:51:48 +02004515static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004517 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4518 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004519}
4520
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004521static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4522{
4523 struct kvm_segment var;
4524 u32 ar;
4525
4526 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004527 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004528 if (seg == VCPU_SREG_CS)
4529 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004530 ar = vmx_segment_access_rights(&var);
4531
4532 if (var.base != (var.selector << 4))
4533 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004534 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004535 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004536 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004537 return false;
4538
4539 return true;
4540}
4541
4542static bool code_segment_valid(struct kvm_vcpu *vcpu)
4543{
4544 struct kvm_segment cs;
4545 unsigned int cs_rpl;
4546
4547 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004548 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004549
Avi Kivity1872a3f2009-01-04 23:26:52 +02004550 if (cs.unusable)
4551 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004552 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004553 return false;
4554 if (!cs.s)
4555 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004556 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004557 if (cs.dpl > cs_rpl)
4558 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004559 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004560 if (cs.dpl != cs_rpl)
4561 return false;
4562 }
4563 if (!cs.present)
4564 return false;
4565
4566 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4567 return true;
4568}
4569
4570static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4571{
4572 struct kvm_segment ss;
4573 unsigned int ss_rpl;
4574
4575 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004576 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004577
Avi Kivity1872a3f2009-01-04 23:26:52 +02004578 if (ss.unusable)
4579 return true;
4580 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004581 return false;
4582 if (!ss.s)
4583 return false;
4584 if (ss.dpl != ss_rpl) /* DPL != RPL */
4585 return false;
4586 if (!ss.present)
4587 return false;
4588
4589 return true;
4590}
4591
4592static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4593{
4594 struct kvm_segment var;
4595 unsigned int rpl;
4596
4597 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004598 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004599
Avi Kivity1872a3f2009-01-04 23:26:52 +02004600 if (var.unusable)
4601 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004602 if (!var.s)
4603 return false;
4604 if (!var.present)
4605 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004606 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004607 if (var.dpl < rpl) /* DPL < RPL */
4608 return false;
4609 }
4610
4611 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4612 * rights flags
4613 */
4614 return true;
4615}
4616
4617static bool tr_valid(struct kvm_vcpu *vcpu)
4618{
4619 struct kvm_segment tr;
4620
4621 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4622
Avi Kivity1872a3f2009-01-04 23:26:52 +02004623 if (tr.unusable)
4624 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004625 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004626 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004627 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004628 return false;
4629 if (!tr.present)
4630 return false;
4631
4632 return true;
4633}
4634
4635static bool ldtr_valid(struct kvm_vcpu *vcpu)
4636{
4637 struct kvm_segment ldtr;
4638
4639 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4640
Avi Kivity1872a3f2009-01-04 23:26:52 +02004641 if (ldtr.unusable)
4642 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004643 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004644 return false;
4645 if (ldtr.type != 2)
4646 return false;
4647 if (!ldtr.present)
4648 return false;
4649
4650 return true;
4651}
4652
4653static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4654{
4655 struct kvm_segment cs, ss;
4656
4657 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4658 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4659
Nadav Amitb32a9912015-03-29 16:33:04 +03004660 return ((cs.selector & SEGMENT_RPL_MASK) ==
4661 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004662}
4663
4664/*
4665 * Check if guest state is valid. Returns true if valid, false if
4666 * not.
4667 * We assume that registers are always usable
4668 */
4669static bool guest_state_valid(struct kvm_vcpu *vcpu)
4670{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004671 if (enable_unrestricted_guest)
4672 return true;
4673
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004674 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004675 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004676 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4677 return false;
4678 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4679 return false;
4680 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4681 return false;
4682 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4683 return false;
4684 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4685 return false;
4686 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4687 return false;
4688 } else {
4689 /* protected mode guest state checks */
4690 if (!cs_ss_rpl_check(vcpu))
4691 return false;
4692 if (!code_segment_valid(vcpu))
4693 return false;
4694 if (!stack_segment_valid(vcpu))
4695 return false;
4696 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4697 return false;
4698 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4699 return false;
4700 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4701 return false;
4702 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4703 return false;
4704 if (!tr_valid(vcpu))
4705 return false;
4706 if (!ldtr_valid(vcpu))
4707 return false;
4708 }
4709 /* TODO:
4710 * - Add checks on RIP
4711 * - Add checks on RFLAGS
4712 */
4713
4714 return true;
4715}
4716
Mike Dayd77c26f2007-10-08 09:02:08 -04004717static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004719 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004720 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004721 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004722
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004723 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004724 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004725 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4726 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004727 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004728 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004729 r = kvm_write_guest_page(kvm, fn++, &data,
4730 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004731 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004732 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004733 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4734 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004735 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004736 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4737 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004738 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004739 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004740 r = kvm_write_guest_page(kvm, fn, &data,
4741 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4742 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004743out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004744 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004745 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746}
4747
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004748static int init_rmode_identity_map(struct kvm *kvm)
4749{
Tang Chenf51770e2014-09-16 18:41:59 +08004750 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004751 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004752 u32 tmp;
4753
Avi Kivity089d0342009-03-23 18:26:32 +02004754 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004755 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004756
4757 /* Protect kvm->arch.ept_identity_pagetable_done. */
4758 mutex_lock(&kvm->slots_lock);
4759
Tang Chenf51770e2014-09-16 18:41:59 +08004760 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004761 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004762
Sheng Yangb927a3c2009-07-21 10:42:48 +08004763 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004764
4765 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004766 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004767 goto out2;
4768
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004769 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004770 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4771 if (r < 0)
4772 goto out;
4773 /* Set up identity-mapping pagetable for EPT in real mode */
4774 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4775 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4776 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4777 r = kvm_write_guest_page(kvm, identity_map_pfn,
4778 &tmp, i * sizeof(tmp), sizeof(tmp));
4779 if (r < 0)
4780 goto out;
4781 }
4782 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004783
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004784out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004785 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004786
4787out2:
4788 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004789 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004790}
4791
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792static void seg_setup(int seg)
4793{
Mathias Krause772e0312012-08-30 01:30:19 +02004794 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004795 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796
4797 vmcs_write16(sf->selector, 0);
4798 vmcs_writel(sf->base, 0);
4799 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004800 ar = 0x93;
4801 if (seg == VCPU_SREG_CS)
4802 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004803
4804 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004805}
4806
Sheng Yangf78e0e22007-10-29 09:40:42 +08004807static int alloc_apic_access_page(struct kvm *kvm)
4808{
Xiao Guangrong44841412012-09-07 14:14:20 +08004809 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004810 int r = 0;
4811
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004812 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004813 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004814 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004815 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4816 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004817 if (r)
4818 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004819
Tang Chen73a6d942014-09-11 13:38:00 +08004820 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004821 if (is_error_page(page)) {
4822 r = -EFAULT;
4823 goto out;
4824 }
4825
Tang Chenc24ae0d2014-09-24 15:57:58 +08004826 /*
4827 * Do not pin the page in memory, so that memory hot-unplug
4828 * is able to migrate it.
4829 */
4830 put_page(page);
4831 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004832out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004833 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004834 return r;
4835}
4836
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004837static int alloc_identity_pagetable(struct kvm *kvm)
4838{
Tang Chena255d472014-09-16 18:41:58 +08004839 /* Called with kvm->slots_lock held. */
4840
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004841 int r = 0;
4842
Tang Chena255d472014-09-16 18:41:58 +08004843 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4844
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004845 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4846 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004847
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004848 return r;
4849}
4850
Wanpeng Li991e7a02015-09-16 17:30:05 +08004851static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004852{
4853 int vpid;
4854
Avi Kivity919818a2009-03-23 18:01:29 +02004855 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004856 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004857 spin_lock(&vmx_vpid_lock);
4858 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004859 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004860 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004861 else
4862 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004863 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004864 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004865}
4866
Wanpeng Li991e7a02015-09-16 17:30:05 +08004867static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004868{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004869 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004870 return;
4871 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004872 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004873 spin_unlock(&vmx_vpid_lock);
4874}
4875
Yi Wang0c4d57a2018-11-08 11:22:21 +08004876static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004877 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004878{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004879 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004880
4881 if (!cpu_has_vmx_msr_bitmap())
4882 return;
4883
4884 /*
4885 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4886 * have the write-low and read-high bitmap offsets the wrong way round.
4887 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4888 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004889 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004890 if (type & MSR_TYPE_R)
4891 /* read-low */
4892 __clear_bit(msr, msr_bitmap + 0x000 / f);
4893
4894 if (type & MSR_TYPE_W)
4895 /* write-low */
4896 __clear_bit(msr, msr_bitmap + 0x800 / f);
4897
Sheng Yang25c5f222008-03-28 13:18:56 +08004898 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4899 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004900 if (type & MSR_TYPE_R)
4901 /* read-high */
4902 __clear_bit(msr, msr_bitmap + 0x400 / f);
4903
4904 if (type & MSR_TYPE_W)
4905 /* write-high */
4906 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4907
4908 }
4909}
4910
Yi Wang0c4d57a2018-11-08 11:22:21 +08004911static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004912 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004913{
4914 int f = sizeof(unsigned long);
4915
4916 if (!cpu_has_vmx_msr_bitmap())
4917 return;
4918
4919 /*
4920 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4921 * have the write-low and read-high bitmap offsets the wrong way round.
4922 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4923 */
4924 if (msr <= 0x1fff) {
4925 if (type & MSR_TYPE_R)
4926 /* read-low */
4927 __set_bit(msr, msr_bitmap + 0x000 / f);
4928
4929 if (type & MSR_TYPE_W)
4930 /* write-low */
4931 __set_bit(msr, msr_bitmap + 0x800 / f);
4932
4933 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4934 msr &= 0x1fff;
4935 if (type & MSR_TYPE_R)
4936 /* read-high */
4937 __set_bit(msr, msr_bitmap + 0x400 / f);
4938
4939 if (type & MSR_TYPE_W)
4940 /* write-high */
4941 __set_bit(msr, msr_bitmap + 0xc00 / f);
4942
Sheng Yang25c5f222008-03-28 13:18:56 +08004943 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004944}
4945
Yi Wang0c4d57a2018-11-08 11:22:21 +08004946static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini6236b782018-01-16 16:51:18 +01004947 u32 msr, int type, bool value)
4948{
4949 if (value)
4950 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4951 else
4952 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4953}
4954
Wincy Vanf2b93282015-02-03 23:56:03 +08004955/*
4956 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4957 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4958 */
4959static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4960 unsigned long *msr_bitmap_nested,
4961 u32 msr, int type)
4962{
4963 int f = sizeof(unsigned long);
4964
4965 if (!cpu_has_vmx_msr_bitmap()) {
4966 WARN_ON(1);
4967 return;
4968 }
4969
4970 /*
4971 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4972 * have the write-low and read-high bitmap offsets the wrong way round.
4973 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4974 */
4975 if (msr <= 0x1fff) {
4976 if (type & MSR_TYPE_R &&
4977 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4978 /* read-low */
4979 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4980
4981 if (type & MSR_TYPE_W &&
4982 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4983 /* write-low */
4984 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4985
4986 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4987 msr &= 0x1fff;
4988 if (type & MSR_TYPE_R &&
4989 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4990 /* read-high */
4991 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4992
4993 if (type & MSR_TYPE_W &&
4994 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4995 /* write-high */
4996 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4997
4998 }
4999}
5000
Paolo Bonzini6236b782018-01-16 16:51:18 +01005001static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005002{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005003 u8 mode = 0;
5004
5005 if (cpu_has_secondary_exec_ctrls() &&
5006 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5007 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5008 mode |= MSR_BITMAP_MODE_X2APIC;
5009 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5010 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5011 }
5012
5013 if (is_long_mode(vcpu))
5014 mode |= MSR_BITMAP_MODE_LM;
5015
5016 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005017}
5018
Paolo Bonzini6236b782018-01-16 16:51:18 +01005019#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5020
5021static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5022 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005023{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005024 int msr;
5025
5026 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5027 unsigned word = msr / BITS_PER_LONG;
5028 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5029 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5030 }
5031
5032 if (mode & MSR_BITMAP_MODE_X2APIC) {
5033 /*
5034 * TPR reads and writes can be virtualized even if virtual interrupt
5035 * delivery is not in use.
5036 */
5037 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5038 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5039 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5040 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5041 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5042 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005043 }
Yang Zhang8d146952013-01-25 10:18:50 +08005044}
5045
Paolo Bonzini6236b782018-01-16 16:51:18 +01005046static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005047{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005048 struct vcpu_vmx *vmx = to_vmx(vcpu);
5049 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5050 u8 mode = vmx_msr_bitmap_mode(vcpu);
5051 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005052
Paolo Bonzini6236b782018-01-16 16:51:18 +01005053 if (!changed)
5054 return;
5055
5056 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5057 !(mode & MSR_BITMAP_MODE_LM));
5058
5059 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5060 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5061
5062 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005063}
5064
Andrey Smetanind62caab2015-11-10 15:36:33 +03005065static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005066{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005067 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005068}
5069
David Matlackb7649e12017-08-01 14:00:40 -07005070static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5071{
5072 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5073 gfn_t gfn;
5074
5075 /*
5076 * Don't need to mark the APIC access page dirty; it is never
5077 * written to by the CPU during APIC virtualization.
5078 */
5079
5080 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5081 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5082 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5083 }
5084
5085 if (nested_cpu_has_posted_intr(vmcs12)) {
5086 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5087 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5088 }
5089}
5090
5091
David Hildenbrand1edccf22017-01-25 11:58:58 +01005092static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005093{
5094 struct vcpu_vmx *vmx = to_vmx(vcpu);
5095 int max_irr;
5096 void *vapic_page;
5097 u16 status;
5098
David Matlackb7649e12017-08-01 14:00:40 -07005099 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5100 return;
Wincy Van705699a2015-02-03 23:58:17 +08005101
David Matlackb7649e12017-08-01 14:00:40 -07005102 vmx->nested.pi_pending = false;
5103 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5104 return;
Wincy Van705699a2015-02-03 23:58:17 +08005105
David Matlackb7649e12017-08-01 14:00:40 -07005106 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5107 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005108 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005109 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5110 kunmap(vmx->nested.virtual_apic_page);
5111
5112 status = vmcs_read16(GUEST_INTR_STATUS);
5113 if ((u8)max_irr > ((u8)status & 0xff)) {
5114 status &= ~0xff;
5115 status |= (u8)max_irr;
5116 vmcs_write16(GUEST_INTR_STATUS, status);
5117 }
5118 }
David Matlackb7649e12017-08-01 14:00:40 -07005119
5120 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005121}
5122
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005123static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5124{
5125#ifdef CONFIG_SMP
5126 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005127 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005128 * The vector of interrupt to be delivered to vcpu had
5129 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005130 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005131 * Following cases will be reached in this block, and
5132 * we always send a notification event in all cases as
5133 * explained below.
5134 *
5135 * Case 1: vcpu keeps in non-root mode. Sending a
5136 * notification event posts the interrupt to vcpu.
5137 *
5138 * Case 2: vcpu exits to root mode and is still
5139 * runnable. PIR will be synced to vIRR before the
5140 * next vcpu entry. Sending a notification event in
5141 * this case has no effect, as vcpu is not in root
5142 * mode.
5143 *
5144 * Case 3: vcpu exits to root mode and is blocked.
5145 * vcpu_block() has already synced PIR to vIRR and
5146 * never blocks vcpu if vIRR is not cleared. Therefore,
5147 * a blocked vcpu here does not wait for any requested
5148 * interrupts in PIR, and sending a notification event
5149 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005150 */
Feng Wu28b835d2015-09-18 22:29:54 +08005151
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005152 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5153 POSTED_INTR_VECTOR);
5154 return true;
5155 }
5156#endif
5157 return false;
5158}
5159
Wincy Van705699a2015-02-03 23:58:17 +08005160static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5161 int vector)
5162{
5163 struct vcpu_vmx *vmx = to_vmx(vcpu);
5164
5165 if (is_guest_mode(vcpu) &&
5166 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005167 /*
5168 * If a posted intr is not recognized by hardware,
5169 * we will accomplish it in the next vmentry.
5170 */
5171 vmx->nested.pi_pending = true;
5172 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005173 /* the PIR and ON have been set by L1. */
5174 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5175 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005176 return 0;
5177 }
5178 return -1;
5179}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005180/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005181 * Send interrupt to vcpu via posted interrupt way.
5182 * 1. If target vcpu is running(non-root mode), send posted interrupt
5183 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5184 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5185 * interrupt from PIR in next vmentry.
5186 */
5187static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5188{
5189 struct vcpu_vmx *vmx = to_vmx(vcpu);
5190 int r;
5191
Wincy Van705699a2015-02-03 23:58:17 +08005192 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5193 if (!r)
5194 return;
5195
Yang Zhanga20ed542013-04-11 19:25:15 +08005196 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5197 return;
5198
5199 r = pi_test_and_set_on(&vmx->pi_desc);
5200 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005201 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005202 kvm_vcpu_kick(vcpu);
5203}
5204
5205static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5206{
5207 struct vcpu_vmx *vmx = to_vmx(vcpu);
5208
5209 if (!pi_test_and_clear_on(&vmx->pi_desc))
5210 return;
5211
5212 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5213}
5214
Avi Kivity6aa8b732006-12-10 02:21:36 -08005215/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005216 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5217 * will not change in the lifetime of the guest.
5218 * Note that host-state that does change is set elsewhere. E.g., host-state
5219 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5220 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005221static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005222{
5223 u32 low32, high32;
5224 unsigned long tmpl;
5225 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005226 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005227
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005228 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005229 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5230
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005231 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005232 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005233 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5234 vmx->host_state.vmcs_host_cr4 = cr4;
5235
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005236 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005237#ifdef CONFIG_X86_64
5238 /*
5239 * Load null selectors, so we can avoid reloading them in
5240 * __vmx_load_host_state(), in case userspace uses the null selectors
5241 * too (the expected case).
5242 */
5243 vmcs_write16(HOST_DS_SELECTOR, 0);
5244 vmcs_write16(HOST_ES_SELECTOR, 0);
5245#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005246 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5247 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005248#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005249 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5250 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5251
5252 native_store_idt(&dt);
5253 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005254 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005255
Avi Kivity83287ea422012-09-16 15:10:57 +03005256 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005257
5258 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5259 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5260 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5261 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5262
5263 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5264 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5265 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5266 }
5267}
5268
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005269static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5270{
5271 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5272 if (enable_ept)
5273 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005274 if (is_guest_mode(&vmx->vcpu))
5275 vmx->vcpu.arch.cr4_guest_owned_bits &=
5276 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005277 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5278}
5279
Yang Zhang01e439b2013-04-11 19:25:12 +08005280static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5281{
5282 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5283
Andrey Smetanind62caab2015-11-10 15:36:33 +03005284 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005285 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005286 /* Enable the preemption timer dynamically */
5287 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005288 return pin_based_exec_ctrl;
5289}
5290
Andrey Smetanind62caab2015-11-10 15:36:33 +03005291static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5292{
5293 struct vcpu_vmx *vmx = to_vmx(vcpu);
5294
5295 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005296 if (cpu_has_secondary_exec_ctrls()) {
5297 if (kvm_vcpu_apicv_active(vcpu))
5298 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5299 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5300 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5301 else
5302 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5303 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5304 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5305 }
5306
5307 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005308 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005309}
5310
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005311static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5312{
5313 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005314
5315 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5316 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5317
Paolo Bonzini35754c92015-07-29 12:05:37 +02005318 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005319 exec_control &= ~CPU_BASED_TPR_SHADOW;
5320#ifdef CONFIG_X86_64
5321 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5322 CPU_BASED_CR8_LOAD_EXITING;
5323#endif
5324 }
5325 if (!enable_ept)
5326 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5327 CPU_BASED_CR3_LOAD_EXITING |
5328 CPU_BASED_INVLPG_EXITING;
5329 return exec_control;
5330}
5331
5332static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5333{
5334 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005335 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005336 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5337 if (vmx->vpid == 0)
5338 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5339 if (!enable_ept) {
5340 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5341 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005342 /* Enable INVPCID for non-ept guests may cause performance regression. */
5343 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005344 }
5345 if (!enable_unrestricted_guest)
5346 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5347 if (!ple_gap)
5348 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005349 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005350 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5351 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005352 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005353 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5354 (handle_vmptrld).
5355 We can NOT enable shadow_vmcs here because we don't have yet
5356 a current VMCS12
5357 */
5358 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005359
5360 if (!enable_pml)
5361 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005362
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005363 return exec_control;
5364}
5365
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005366static void ept_set_mmio_spte_mask(void)
5367{
5368 /*
5369 * EPT Misconfigurations can be generated if the value of bits 2:0
5370 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005371 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005372 * spte.
5373 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005374 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005375}
5376
Wanpeng Lif53cd632014-12-02 19:14:58 +08005377#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005378/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379 * Sets up the vmcs for emulated real mode.
5380 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005381static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005382{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005383#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005384 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005385#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005386 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387
Avi Kivity6aa8b732006-12-10 02:21:36 -08005388 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005389 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5390 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005391
Abel Gordon4607c2d2013-04-18 14:35:55 +03005392 if (enable_shadow_vmcs) {
5393 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5394 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5395 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005396 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005397 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005398
Avi Kivity6aa8b732006-12-10 02:21:36 -08005399 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5400
Avi Kivity6aa8b732006-12-10 02:21:36 -08005401 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005402 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005403 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005404
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005405 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005406
Dan Williamsdfa169b2016-06-02 11:17:24 -07005407 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005408 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5409 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005410 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005411
Andrey Smetanind62caab2015-11-10 15:36:33 +03005412 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005413 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5414 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5415 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5416 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5417
5418 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005419
Li RongQing0bcf2612015-12-03 13:29:34 +08005420 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005421 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005422 }
5423
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005424 if (ple_gap) {
5425 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005426 vmx->ple_window = ple_window;
5427 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005428 }
5429
Xiao Guangrongc3707952011-07-12 03:28:04 +08005430 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5431 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005432 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5433
Avi Kivity9581d442010-10-19 16:46:55 +02005434 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5435 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005436 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005437#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005438 rdmsrl(MSR_FS_BASE, a);
5439 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5440 rdmsrl(MSR_GS_BASE, a);
5441 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5442#else
5443 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5444 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5445#endif
5446
Eddie Dong2cc51562007-05-21 07:28:09 +03005447 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5448 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005449 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005450 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005451 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452
Radim Krčmář74545702015-04-27 15:11:25 +02005453 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5454 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005455
Paolo Bonzini03916db2014-07-24 14:21:57 +02005456 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457 u32 index = vmx_msr_index[i];
5458 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005459 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005460
5461 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5462 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005463 if (wrmsr_safe(index, data_low, data_high) < 0)
5464 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005465 vmx->guest_msrs[j].index = i;
5466 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005467 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005468 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005469 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005470
Paolo Bonzinif56c8ee2018-08-05 16:07:47 +02005471 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02005472
5473 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005474
5475 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005476 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005477
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005478 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005479 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005480
Wanpeng Lif53cd632014-12-02 19:14:58 +08005481 if (vmx_xsaves_supported())
5482 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5483
Peter Feiner4e595162016-07-07 14:49:58 -07005484 if (enable_pml) {
5485 ASSERT(vmx->pml_pg);
5486 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5487 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5488 }
5489
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005490 return 0;
5491}
5492
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005493static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005494{
5495 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005496 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005497 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005498
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005499 vmx->rmode.vm86_active = 0;
Wanpeng Li7a1eac82018-02-28 14:03:31 +08005500 vcpu->arch.microcode_version = 0x100000000ULL;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005501 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005502
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005503 vmx->soft_vnmi_blocked = 0;
5504
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005505 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005506 kvm_set_cr8(vcpu, 0);
5507
5508 if (!init_event) {
5509 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5510 MSR_IA32_APICBASE_ENABLE;
5511 if (kvm_vcpu_is_reset_bsp(vcpu))
5512 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5513 apic_base_msr.host_initiated = true;
5514 kvm_set_apic_base(vcpu, &apic_base_msr);
5515 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005516
Avi Kivity2fb92db2011-04-27 19:42:18 +03005517 vmx_segment_cache_clear(vmx);
5518
Avi Kivity5706be02008-08-20 15:07:31 +03005519 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005520 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005521 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005522
5523 seg_setup(VCPU_SREG_DS);
5524 seg_setup(VCPU_SREG_ES);
5525 seg_setup(VCPU_SREG_FS);
5526 seg_setup(VCPU_SREG_GS);
5527 seg_setup(VCPU_SREG_SS);
5528
5529 vmcs_write16(GUEST_TR_SELECTOR, 0);
5530 vmcs_writel(GUEST_TR_BASE, 0);
5531 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5532 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5533
5534 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5535 vmcs_writel(GUEST_LDTR_BASE, 0);
5536 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5537 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5538
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005539 if (!init_event) {
5540 vmcs_write32(GUEST_SYSENTER_CS, 0);
5541 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5542 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5543 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5544 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005545
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005546 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005547 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005548
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005549 vmcs_writel(GUEST_GDTR_BASE, 0);
5550 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5551
5552 vmcs_writel(GUEST_IDTR_BASE, 0);
5553 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5554
Anthony Liguori443381a2010-12-06 10:53:38 -06005555 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005556 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005557 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005558
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005559 setup_msrs(vmx);
5560
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5562
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005563 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005564 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005565 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005566 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005567 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005568 vmcs_write32(TPR_THRESHOLD, 0);
5569 }
5570
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005571 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005572
Andrey Smetanind62caab2015-11-10 15:36:33 +03005573 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005574 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5575
Sheng Yang2384d2b2008-01-17 15:14:33 +08005576 if (vmx->vpid != 0)
5577 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5578
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005579 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005580 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005581 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005582 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005583 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005584 vmx_fpu_activate(vcpu);
5585 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005586
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005587 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005588}
5589
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005590/*
5591 * In nested virtualization, check if L1 asked to exit on external interrupts.
5592 * For most existing hypervisors, this will always return true.
5593 */
5594static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5595{
5596 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5597 PIN_BASED_EXT_INTR_MASK;
5598}
5599
Bandan Das77b0f5d2014-04-19 18:17:45 -04005600/*
5601 * In nested virtualization, check if L1 has set
5602 * VM_EXIT_ACK_INTR_ON_EXIT
5603 */
5604static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5605{
5606 return get_vmcs12(vcpu)->vm_exit_controls &
5607 VM_EXIT_ACK_INTR_ON_EXIT;
5608}
5609
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005610static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5611{
5612 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5613 PIN_BASED_NMI_EXITING;
5614}
5615
Jan Kiszkac9a79532014-03-07 20:03:15 +01005616static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005617{
5618 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005619
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005620 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5621 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5622 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5623}
5624
Jan Kiszkac9a79532014-03-07 20:03:15 +01005625static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005626{
5627 u32 cpu_based_vm_exec_control;
5628
Jan Kiszkac9a79532014-03-07 20:03:15 +01005629 if (!cpu_has_virtual_nmis() ||
5630 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5631 enable_irq_window(vcpu);
5632 return;
5633 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005634
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005635 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5636 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5637 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5638}
5639
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005640static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005641{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005642 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005643 uint32_t intr;
5644 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005645
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005646 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005647
Avi Kivityfa89a812008-09-01 15:57:51 +03005648 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005649 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005650 int inc_eip = 0;
5651 if (vcpu->arch.interrupt.soft)
5652 inc_eip = vcpu->arch.event_exit_inst_len;
5653 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005654 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005655 return;
5656 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005657 intr = irq | INTR_INFO_VALID_MASK;
5658 if (vcpu->arch.interrupt.soft) {
5659 intr |= INTR_TYPE_SOFT_INTR;
5660 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5661 vmx->vcpu.arch.event_exit_inst_len);
5662 } else
5663 intr |= INTR_TYPE_EXT_INTR;
5664 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005665}
5666
Sheng Yangf08864b2008-05-15 18:23:25 +08005667static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5668{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005669 struct vcpu_vmx *vmx = to_vmx(vcpu);
5670
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005671 if (!is_guest_mode(vcpu)) {
5672 if (!cpu_has_virtual_nmis()) {
5673 /*
5674 * Tracking the NMI-blocked state in software is built upon
5675 * finding the next open IRQ window. This, in turn, depends on
5676 * well-behaving guests: They have to keep IRQs disabled at
5677 * least as long as the NMI handler runs. Otherwise we may
5678 * cause NMI nesting, maybe breaking the guest. But as this is
5679 * highly unlikely, we can live with the residual risk.
5680 */
5681 vmx->soft_vnmi_blocked = 1;
5682 vmx->vnmi_blocked_time = 0;
5683 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005684
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005685 ++vcpu->stat.nmi_injections;
5686 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005687 }
5688
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005689 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005690 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005691 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005692 return;
5693 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005694
Sheng Yangf08864b2008-05-15 18:23:25 +08005695 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5696 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005697}
5698
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005699static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5700{
5701 if (!cpu_has_virtual_nmis())
5702 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005703 if (to_vmx(vcpu)->nmi_known_unmasked)
5704 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005705 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005706}
5707
5708static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5709{
5710 struct vcpu_vmx *vmx = to_vmx(vcpu);
5711
5712 if (!cpu_has_virtual_nmis()) {
5713 if (vmx->soft_vnmi_blocked != masked) {
5714 vmx->soft_vnmi_blocked = masked;
5715 vmx->vnmi_blocked_time = 0;
5716 }
5717 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005718 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005719 if (masked)
5720 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5721 GUEST_INTR_STATE_NMI);
5722 else
5723 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5724 GUEST_INTR_STATE_NMI);
5725 }
5726}
5727
Jan Kiszka2505dc92013-04-14 12:12:47 +02005728static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5729{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005730 if (to_vmx(vcpu)->nested.nested_run_pending)
5731 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005732
Jan Kiszka2505dc92013-04-14 12:12:47 +02005733 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5734 return 0;
5735
5736 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5737 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5738 | GUEST_INTR_STATE_NMI));
5739}
5740
Gleb Natapov78646122009-03-23 12:12:11 +02005741static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5742{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005743 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5744 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005745 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5746 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005747}
5748
Izik Eiduscbc94022007-10-25 00:29:55 +02005749static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5750{
5751 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005752
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005753 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5754 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005755 if (ret)
5756 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005757 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005758 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005759}
5760
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005761static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005762{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005763 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005764 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005765 /*
5766 * Update instruction length as we may reinject the exception
5767 * from user space while in guest debugging mode.
5768 */
5769 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5770 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005771 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005772 return false;
5773 /* fall through */
5774 case DB_VECTOR:
5775 if (vcpu->guest_debug &
5776 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5777 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005778 /* fall through */
5779 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005780 case OF_VECTOR:
5781 case BR_VECTOR:
5782 case UD_VECTOR:
5783 case DF_VECTOR:
5784 case SS_VECTOR:
5785 case GP_VECTOR:
5786 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005787 return true;
5788 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005789 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005790 return false;
5791}
5792
5793static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5794 int vec, u32 err_code)
5795{
5796 /*
5797 * Instruction with address size override prefix opcode 0x67
5798 * Cause the #SS fault with 0 error code in VM86 mode.
5799 */
5800 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5801 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5802 if (vcpu->arch.halt_request) {
5803 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005804 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005805 }
5806 return 1;
5807 }
5808 return 0;
5809 }
5810
5811 /*
5812 * Forward all other exceptions that are valid in real mode.
5813 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5814 * the required debugging infrastructure rework.
5815 */
5816 kvm_queue_exception(vcpu, vec);
5817 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005818}
5819
Andi Kleena0861c02009-06-08 17:37:09 +08005820/*
5821 * Trigger machine check on the host. We assume all the MSRs are already set up
5822 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5823 * We pass a fake environment to the machine check handler because we want
5824 * the guest to be always treated like user space, no matter what context
5825 * it used internally.
5826 */
5827static void kvm_machine_check(void)
5828{
5829#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5830 struct pt_regs regs = {
5831 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5832 .flags = X86_EFLAGS_IF,
5833 };
5834
5835 do_machine_check(&regs, 0);
5836#endif
5837}
5838
Avi Kivity851ba692009-08-24 11:10:17 +03005839static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005840{
5841 /* already handled by vcpu_run */
5842 return 1;
5843}
5844
Avi Kivity851ba692009-08-24 11:10:17 +03005845static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005846{
Avi Kivity1155f762007-11-22 11:30:47 +02005847 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005848 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005849 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005850 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005851 u32 vect_info;
5852 enum emulation_result er;
5853
Avi Kivity1155f762007-11-22 11:30:47 +02005854 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005855 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856
Andi Kleena0861c02009-06-08 17:37:09 +08005857 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005858 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005859
Jim Mattson3f618a02016-12-12 11:01:37 -08005860 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005861 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005862
5863 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005864 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005865 return 1;
5866 }
5867
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005868 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005869 if (is_guest_mode(vcpu)) {
5870 kvm_queue_exception(vcpu, UD_VECTOR);
5871 return 1;
5872 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005873 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005874 if (er == EMULATE_USER_EXIT)
5875 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005876 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005877 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005878 return 1;
5879 }
5880
Avi Kivity6aa8b732006-12-10 02:21:36 -08005881 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005882 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005883 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005884
5885 /*
5886 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5887 * MMIO, it is better to report an internal error.
5888 * See the comments in vmx_handle_exit.
5889 */
5890 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5891 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5892 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5893 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005894 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005895 vcpu->run->internal.data[0] = vect_info;
5896 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005897 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005898 return 0;
5899 }
5900
Avi Kivity6aa8b732006-12-10 02:21:36 -08005901 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005902 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005903 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005904 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005905 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005906 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005907
Gleb Natapov3298b752009-05-11 13:35:46 +03005908 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005909 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005910 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005911 }
5912
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005913 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005914
5915 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5916 return handle_rmode_exception(vcpu, ex_no, error_code);
5917
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005918 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005919 case AC_VECTOR:
5920 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5921 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005922 case DB_VECTOR:
5923 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5924 if (!(vcpu->guest_debug &
5925 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005926 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005927 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005928 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005929 skip_emulated_instruction(vcpu);
5930
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005931 kvm_queue_exception(vcpu, DB_VECTOR);
5932 return 1;
5933 }
5934 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5935 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5936 /* fall through */
5937 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005938 /*
5939 * Update instruction length as we may reinject #BP from
5940 * user space while in guest debugging mode. Reading it for
5941 * #DB as well causes no harm, it is not used in that case.
5942 */
5943 vmx->vcpu.arch.event_exit_inst_len =
5944 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005945 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005946 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005947 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5948 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005949 break;
5950 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005951 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5952 kvm_run->ex.exception = ex_no;
5953 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005954 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005955 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005956 return 0;
5957}
5958
Avi Kivity851ba692009-08-24 11:10:17 +03005959static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005960{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005961 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005962 return 1;
5963}
5964
Avi Kivity851ba692009-08-24 11:10:17 +03005965static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005966{
Avi Kivity851ba692009-08-24 11:10:17 +03005967 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Li5e29da02017-08-09 22:33:12 -07005968 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08005969 return 0;
5970}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005971
Avi Kivity851ba692009-08-24 11:10:17 +03005972static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005973{
He, Qingbfdaab02007-09-12 14:18:28 +08005974 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005975 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005976 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005977
He, Qingbfdaab02007-09-12 14:18:28 +08005978 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005979 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005980 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005981
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005982 ++vcpu->stat.io_exits;
5983
5984 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005985 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005986
5987 port = exit_qualification >> 16;
5988 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005989 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005990
5991 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005992}
5993
Ingo Molnar102d8322007-02-19 14:37:47 +02005994static void
5995vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5996{
5997 /*
5998 * Patch in the VMCALL instruction:
5999 */
6000 hypercall[0] = 0x0f;
6001 hypercall[1] = 0x01;
6002 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006003}
6004
Wincy Vanb9c237b2015-02-03 23:56:30 +08006005static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006006{
6007 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08006008 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006009
Wincy Vanb9c237b2015-02-03 23:56:30 +08006010 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02006011 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
6012 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
6013 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
6014 return (val & always_on) == always_on;
6015}
6016
Guo Chao0fa06072012-06-28 15:16:19 +08006017/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006018static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6019{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006020 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006021 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6022 unsigned long orig_val = val;
6023
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006024 /*
6025 * We get here when L2 changed cr0 in a way that did not change
6026 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006027 * but did change L0 shadowed bits. So we first calculate the
6028 * effective cr0 value that L1 would like to write into the
6029 * hardware. It consists of the L2-owned bits from the new
6030 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006031 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006032 val = (val & ~vmcs12->cr0_guest_host_mask) |
6033 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6034
Wincy Vanb9c237b2015-02-03 23:56:30 +08006035 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006036 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006037
6038 if (kvm_set_cr0(vcpu, val))
6039 return 1;
6040 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006041 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006042 } else {
6043 if (to_vmx(vcpu)->nested.vmxon &&
6044 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
6045 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006046 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006047 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006048}
6049
6050static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6051{
6052 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006053 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6054 unsigned long orig_val = val;
6055
6056 /* analogously to handle_set_cr0 */
6057 val = (val & ~vmcs12->cr4_guest_host_mask) |
6058 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6059 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006060 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006061 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006062 return 0;
6063 } else
6064 return kvm_set_cr4(vcpu, val);
6065}
6066
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08006067/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006068static void handle_clts(struct kvm_vcpu *vcpu)
6069{
6070 if (is_guest_mode(vcpu)) {
6071 /*
6072 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
6073 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
6074 * just pretend it's off (also in arch.cr0 for fpu_activate).
6075 */
6076 vmcs_writel(CR0_READ_SHADOW,
6077 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
6078 vcpu->arch.cr0 &= ~X86_CR0_TS;
6079 } else
6080 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6081}
6082
Avi Kivity851ba692009-08-24 11:10:17 +03006083static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006084{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006085 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006086 int cr;
6087 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006088 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006089
He, Qingbfdaab02007-09-12 14:18:28 +08006090 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006091 cr = exit_qualification & 15;
6092 reg = (exit_qualification >> 8) & 15;
6093 switch ((exit_qualification >> 4) & 3) {
6094 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006095 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006096 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006097 switch (cr) {
6098 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006099 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006100 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006101 return 1;
6102 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006103 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006104 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006105 return 1;
6106 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006107 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006108 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006109 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006110 case 8: {
6111 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006112 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006113 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006114 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006115 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006116 return 1;
6117 if (cr8_prev <= cr8)
6118 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03006119 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006120 return 0;
6121 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006122 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006123 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006124 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006125 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006126 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006127 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006128 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006129 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006130 case 1: /*mov from cr*/
6131 switch (cr) {
6132 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006133 val = kvm_read_cr3(vcpu);
6134 kvm_register_write(vcpu, reg, val);
6135 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006136 skip_emulated_instruction(vcpu);
6137 return 1;
6138 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006139 val = kvm_get_cr8(vcpu);
6140 kvm_register_write(vcpu, reg, val);
6141 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006142 skip_emulated_instruction(vcpu);
6143 return 1;
6144 }
6145 break;
6146 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006147 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006148 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006149 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006150
6151 skip_emulated_instruction(vcpu);
6152 return 1;
6153 default:
6154 break;
6155 }
Avi Kivity851ba692009-08-24 11:10:17 +03006156 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006157 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006158 (int)(exit_qualification >> 4) & 3, cr);
6159 return 0;
6160}
6161
Avi Kivity851ba692009-08-24 11:10:17 +03006162static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006163{
He, Qingbfdaab02007-09-12 14:18:28 +08006164 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006165 int dr, dr7, reg;
6166
6167 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6168 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6169
6170 /* First, if DR does not exist, trigger UD */
6171 if (!kvm_require_dr(vcpu, dr))
6172 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006173
Jan Kiszkaf2483412010-01-20 18:20:20 +01006174 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006175 if (!kvm_require_cpl(vcpu, 0))
6176 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006177 dr7 = vmcs_readl(GUEST_DR7);
6178 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006179 /*
6180 * As the vm-exit takes precedence over the debug trap, we
6181 * need to emulate the latter, either for the host or the
6182 * guest debugging itself.
6183 */
6184 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006185 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006186 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006187 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006188 vcpu->run->debug.arch.exception = DB_VECTOR;
6189 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006190 return 0;
6191 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006192 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006193 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006194 kvm_queue_exception(vcpu, DB_VECTOR);
6195 return 1;
6196 }
6197 }
6198
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006199 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006200 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6201 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006202
6203 /*
6204 * No more DR vmexits; force a reload of the debug registers
6205 * and reenter on this instruction. The next vmexit will
6206 * retrieve the full state of the debug registers.
6207 */
6208 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6209 return 1;
6210 }
6211
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006212 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6213 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006214 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006215
6216 if (kvm_get_dr(vcpu, dr, &val))
6217 return 1;
6218 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006219 } else
Nadav Amit57773922014-06-18 17:19:23 +03006220 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006221 return 1;
6222
Avi Kivity6aa8b732006-12-10 02:21:36 -08006223 skip_emulated_instruction(vcpu);
6224 return 1;
6225}
6226
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006227static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6228{
6229 return vcpu->arch.dr6;
6230}
6231
6232static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6233{
6234}
6235
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006236static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6237{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006238 get_debugreg(vcpu->arch.db[0], 0);
6239 get_debugreg(vcpu->arch.db[1], 1);
6240 get_debugreg(vcpu->arch.db[2], 2);
6241 get_debugreg(vcpu->arch.db[3], 3);
6242 get_debugreg(vcpu->arch.dr6, 6);
6243 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6244
6245 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006246 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006247}
6248
Gleb Natapov020df072010-04-13 10:05:23 +03006249static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6250{
6251 vmcs_writel(GUEST_DR7, val);
6252}
6253
Avi Kivity851ba692009-08-24 11:10:17 +03006254static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006255{
Avi Kivity06465c52007-02-28 20:46:53 +02006256 kvm_emulate_cpuid(vcpu);
6257 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006258}
6259
Avi Kivity851ba692009-08-24 11:10:17 +03006260static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006261{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006262 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006263 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006264
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006265 msr_info.index = ecx;
6266 msr_info.host_initiated = false;
6267 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006268 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006269 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006270 return 1;
6271 }
6272
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006273 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006274
Avi Kivity6aa8b732006-12-10 02:21:36 -08006275 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006276 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6277 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006278 skip_emulated_instruction(vcpu);
6279 return 1;
6280}
6281
Avi Kivity851ba692009-08-24 11:10:17 +03006282static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006283{
Will Auld8fe8ab42012-11-29 12:42:12 -08006284 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006285 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6286 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6287 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006288
Will Auld8fe8ab42012-11-29 12:42:12 -08006289 msr.data = data;
6290 msr.index = ecx;
6291 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006292 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006293 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006294 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006295 return 1;
6296 }
6297
Avi Kivity59200272010-01-25 19:47:02 +02006298 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006299 skip_emulated_instruction(vcpu);
6300 return 1;
6301}
6302
Avi Kivity851ba692009-08-24 11:10:17 +03006303static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006304{
Avi Kivity3842d132010-07-27 12:30:24 +03006305 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006306 return 1;
6307}
6308
Avi Kivity851ba692009-08-24 11:10:17 +03006309static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006310{
Eddie Dong85f455f2007-07-06 12:20:49 +03006311 u32 cpu_based_vm_exec_control;
6312
6313 /* clear pending irq */
6314 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6315 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6316 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006317
Avi Kivity3842d132010-07-27 12:30:24 +03006318 kvm_make_request(KVM_REQ_EVENT, vcpu);
6319
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006320 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006321 return 1;
6322}
6323
Avi Kivity851ba692009-08-24 11:10:17 +03006324static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325{
Avi Kivityd3bef152007-06-05 15:53:05 +03006326 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006327}
6328
Avi Kivity851ba692009-08-24 11:10:17 +03006329static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006330{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006331 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006332}
6333
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006334static int handle_invd(struct kvm_vcpu *vcpu)
6335{
Andre Przywara51d8b662010-12-21 11:12:02 +01006336 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006337}
6338
Avi Kivity851ba692009-08-24 11:10:17 +03006339static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006340{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006341 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006342
6343 kvm_mmu_invlpg(vcpu, exit_qualification);
6344 skip_emulated_instruction(vcpu);
6345 return 1;
6346}
6347
Avi Kivityfee84b02011-11-10 14:57:25 +02006348static int handle_rdpmc(struct kvm_vcpu *vcpu)
6349{
6350 int err;
6351
6352 err = kvm_rdpmc(vcpu);
6353 kvm_complete_insn_gp(vcpu, err);
6354
6355 return 1;
6356}
6357
Avi Kivity851ba692009-08-24 11:10:17 +03006358static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006359{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006360 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006361 return 1;
6362}
6363
Dexuan Cui2acf9232010-06-10 11:27:12 +08006364static int handle_xsetbv(struct kvm_vcpu *vcpu)
6365{
6366 u64 new_bv = kvm_read_edx_eax(vcpu);
6367 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6368
6369 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6370 skip_emulated_instruction(vcpu);
6371 return 1;
6372}
6373
Wanpeng Lif53cd632014-12-02 19:14:58 +08006374static int handle_xsaves(struct kvm_vcpu *vcpu)
6375{
6376 skip_emulated_instruction(vcpu);
6377 WARN(1, "this should never happen\n");
6378 return 1;
6379}
6380
6381static int handle_xrstors(struct kvm_vcpu *vcpu)
6382{
6383 skip_emulated_instruction(vcpu);
6384 WARN(1, "this should never happen\n");
6385 return 1;
6386}
6387
Avi Kivity851ba692009-08-24 11:10:17 +03006388static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006389{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006390 if (likely(fasteoi)) {
6391 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6392 int access_type, offset;
6393
6394 access_type = exit_qualification & APIC_ACCESS_TYPE;
6395 offset = exit_qualification & APIC_ACCESS_OFFSET;
6396 /*
6397 * Sane guest uses MOV to write EOI, with written value
6398 * not cared. So make a short-circuit here by avoiding
6399 * heavy instruction emulation.
6400 */
6401 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6402 (offset == APIC_EOI)) {
6403 kvm_lapic_set_eoi(vcpu);
6404 skip_emulated_instruction(vcpu);
6405 return 1;
6406 }
6407 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006408 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006409}
6410
Yang Zhangc7c9c562013-01-25 10:18:51 +08006411static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6412{
6413 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6414 int vector = exit_qualification & 0xff;
6415
6416 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6417 kvm_apic_set_eoi_accelerated(vcpu, vector);
6418 return 1;
6419}
6420
Yang Zhang83d4c282013-01-25 10:18:49 +08006421static int handle_apic_write(struct kvm_vcpu *vcpu)
6422{
6423 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6424 u32 offset = exit_qualification & 0xfff;
6425
6426 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6427 kvm_apic_write_nodecode(vcpu, offset);
6428 return 1;
6429}
6430
Avi Kivity851ba692009-08-24 11:10:17 +03006431static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006432{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006433 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006434 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006435 bool has_error_code = false;
6436 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006437 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006438 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006439
6440 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006441 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006442 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006443
6444 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6445
6446 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006447 if (reason == TASK_SWITCH_GATE && idt_v) {
6448 switch (type) {
6449 case INTR_TYPE_NMI_INTR:
6450 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006451 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006452 break;
6453 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006454 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006455 kvm_clear_interrupt_queue(vcpu);
6456 break;
6457 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006458 if (vmx->idt_vectoring_info &
6459 VECTORING_INFO_DELIVER_CODE_MASK) {
6460 has_error_code = true;
6461 error_code =
6462 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6463 }
6464 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006465 case INTR_TYPE_SOFT_EXCEPTION:
6466 kvm_clear_exception_queue(vcpu);
6467 break;
6468 default:
6469 break;
6470 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006471 }
Izik Eidus37817f22008-03-24 23:14:53 +02006472 tss_selector = exit_qualification;
6473
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006474 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6475 type != INTR_TYPE_EXT_INTR &&
6476 type != INTR_TYPE_NMI_INTR))
6477 skip_emulated_instruction(vcpu);
6478
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006479 if (kvm_task_switch(vcpu, tss_selector,
6480 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6481 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006482 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6483 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6484 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006485 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006486 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006487
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006488 /*
6489 * TODO: What about debug traps on tss switch?
6490 * Are we supposed to inject them and update dr6?
6491 */
6492
6493 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006494}
6495
Avi Kivity851ba692009-08-24 11:10:17 +03006496static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006497{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006498 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006499 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006500 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006501 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006502
Sheng Yangf9c617f2009-03-25 10:08:52 +08006503 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006504
Sheng Yang14394422008-04-28 12:24:45 +08006505 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006506 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006507 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6508 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6509 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006510 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006511 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6512 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006513 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6514 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006515 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006516 }
6517
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006518 /*
6519 * EPT violation happened while executing iret from NMI,
6520 * "blocked by NMI" bit has to be set before next VM entry.
6521 * There are errata that may cause this bit to not be set:
6522 * AAK134, BY25.
6523 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006524 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6525 cpu_has_virtual_nmis() &&
6526 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006527 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6528
Sheng Yang14394422008-04-28 12:24:45 +08006529 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006530 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006531
Bandan Dasd95c5562016-07-12 18:18:51 -04006532 /* it is a read fault? */
6533 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6534 /* it is a write fault? */
6535 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006536 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006537 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006538 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006539 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006540
Yang Zhang25d92082013-08-06 12:00:32 +03006541 vcpu->arch.exit_qualification = exit_qualification;
6542
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006543 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006544}
6545
Avi Kivity851ba692009-08-24 11:10:17 +03006546static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006547{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006548 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006549 gpa_t gpa;
6550
6551 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006552 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006553 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsov737dcb92018-01-25 16:37:07 +01006554 /*
6555 * Doing kvm_skip_emulated_instruction() depends on undefined
6556 * behavior: Intel's manual doesn't mandate
6557 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6558 * occurs and while on real hardware it was observed to be set,
6559 * other hypervisors (namely Hyper-V) don't set it, we end up
6560 * advancing IP with some random value. Disable fast mmio when
6561 * running nested and keep it for real hardware in hope that
6562 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6563 */
6564 if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) {
6565 skip_emulated_instruction(vcpu);
6566 return 1;
6567 }
6568 else
6569 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6570 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006571 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006572
Paolo Bonzini450869d2015-11-04 13:41:21 +01006573 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006574 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006575 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6576 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006577
6578 if (unlikely(ret == RET_MMIO_PF_INVALID))
6579 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6580
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006581 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006582 return 1;
6583
6584 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006585 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006586
Avi Kivity851ba692009-08-24 11:10:17 +03006587 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6588 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006589
6590 return 0;
6591}
6592
Avi Kivity851ba692009-08-24 11:10:17 +03006593static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006594{
6595 u32 cpu_based_vm_exec_control;
6596
6597 /* clear pending NMI */
6598 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6599 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6600 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6601 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006602 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006603
6604 return 1;
6605}
6606
Mohammed Gamal80ced182009-09-01 12:48:18 +02006607static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006608{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006609 struct vcpu_vmx *vmx = to_vmx(vcpu);
6610 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006611 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006612 u32 cpu_exec_ctrl;
6613 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006614 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006615
6616 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6617 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006618
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006619 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006620 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006621 return handle_interrupt_window(&vmx->vcpu);
6622
Avi Kivityde87dcd2012-06-12 20:21:38 +03006623 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6624 return 1;
6625
Liran Alon114de9b2017-11-05 16:56:34 +02006626 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006627
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006628 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006629 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006630 ret = 0;
6631 goto out;
6632 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006633
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006634 if (err != EMULATE_DONE)
6635 goto emulation_error;
6636
6637 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6638 vcpu->arch.exception.pending)
6639 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006640
Gleb Natapov8d76c492013-05-08 18:38:44 +03006641 if (vcpu->arch.halt_request) {
6642 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006643 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006644 goto out;
6645 }
6646
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006647 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006648 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006649 if (need_resched())
6650 schedule();
6651 }
6652
Mohammed Gamal80ced182009-09-01 12:48:18 +02006653out:
6654 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006655
6656emulation_error:
6657 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6658 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6659 vcpu->run->internal.ndata = 0;
6660 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006661}
6662
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006663static int __grow_ple_window(int val)
6664{
6665 if (ple_window_grow < 1)
6666 return ple_window;
6667
6668 val = min(val, ple_window_actual_max);
6669
6670 if (ple_window_grow < ple_window)
6671 val *= ple_window_grow;
6672 else
6673 val += ple_window_grow;
6674
6675 return val;
6676}
6677
6678static int __shrink_ple_window(int val, int modifier, int minimum)
6679{
6680 if (modifier < 1)
6681 return ple_window;
6682
6683 if (modifier < ple_window)
6684 val /= modifier;
6685 else
6686 val -= modifier;
6687
6688 return max(val, minimum);
6689}
6690
6691static void grow_ple_window(struct kvm_vcpu *vcpu)
6692{
6693 struct vcpu_vmx *vmx = to_vmx(vcpu);
6694 int old = vmx->ple_window;
6695
6696 vmx->ple_window = __grow_ple_window(old);
6697
6698 if (vmx->ple_window != old)
6699 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006700
6701 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006702}
6703
6704static void shrink_ple_window(struct kvm_vcpu *vcpu)
6705{
6706 struct vcpu_vmx *vmx = to_vmx(vcpu);
6707 int old = vmx->ple_window;
6708
6709 vmx->ple_window = __shrink_ple_window(old,
6710 ple_window_shrink, ple_window);
6711
6712 if (vmx->ple_window != old)
6713 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006714
6715 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006716}
6717
6718/*
6719 * ple_window_actual_max is computed to be one grow_ple_window() below
6720 * ple_window_max. (See __grow_ple_window for the reason.)
6721 * This prevents overflows, because ple_window_max is int.
6722 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6723 * this process.
6724 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6725 */
6726static void update_ple_window_actual_max(void)
6727{
6728 ple_window_actual_max =
6729 __shrink_ple_window(max(ple_window_max, ple_window),
6730 ple_window_grow, INT_MIN);
6731}
6732
Feng Wubf9f6ac2015-09-18 22:29:55 +08006733/*
6734 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6735 */
6736static void wakeup_handler(void)
6737{
6738 struct kvm_vcpu *vcpu;
6739 int cpu = smp_processor_id();
6740
6741 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6742 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6743 blocked_vcpu_list) {
6744 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6745
6746 if (pi_test_on(pi_desc) == 1)
6747 kvm_vcpu_kick(vcpu);
6748 }
6749 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6750}
6751
Tiejun Chenf2c76482014-10-28 10:14:47 +08006752static __init int hardware_setup(void)
6753{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006754 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006755
6756 rdmsrl_safe(MSR_EFER, &host_efer);
6757
6758 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6759 kvm_define_shared_msr(i, vmx_msr_index[i]);
6760
6761 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6762 if (!vmx_io_bitmap_a)
6763 return r;
6764
6765 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6766 if (!vmx_io_bitmap_b)
6767 goto out;
6768
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006769 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6770 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006771 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006772
6773 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6774 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006775 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006776
6777 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6778 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6779
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006780 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006781
6782 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6783
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006784 if (setup_vmcs_config(&vmcs_config) < 0) {
6785 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006786 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006787 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006788
6789 if (boot_cpu_has(X86_FEATURE_NX))
6790 kvm_enable_efer_bits(EFER_NX);
6791
Wanpeng Li2df19692017-03-23 05:30:08 -07006792 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6793 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006794 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006795
Tiejun Chenf2c76482014-10-28 10:14:47 +08006796 if (!cpu_has_vmx_shadow_vmcs())
6797 enable_shadow_vmcs = 0;
6798 if (enable_shadow_vmcs)
6799 init_vmcs_shadow_fields();
6800
6801 if (!cpu_has_vmx_ept() ||
6802 !cpu_has_vmx_ept_4levels()) {
6803 enable_ept = 0;
6804 enable_unrestricted_guest = 0;
6805 enable_ept_ad_bits = 0;
6806 }
6807
6808 if (!cpu_has_vmx_ept_ad_bits())
6809 enable_ept_ad_bits = 0;
6810
6811 if (!cpu_has_vmx_unrestricted_guest())
6812 enable_unrestricted_guest = 0;
6813
Paolo Bonziniad15a292015-01-30 16:18:49 +01006814 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006815 flexpriority_enabled = 0;
6816
Paolo Bonziniad15a292015-01-30 16:18:49 +01006817 /*
6818 * set_apic_access_page_addr() is used to reload apic access
6819 * page upon invalidation. No need to do anything if not
6820 * using the APIC_ACCESS_ADDR VMCS field.
6821 */
6822 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006823 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006824
6825 if (!cpu_has_vmx_tpr_shadow())
6826 kvm_x86_ops->update_cr8_intercept = NULL;
6827
6828 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6829 kvm_disable_largepages();
6830
6831 if (!cpu_has_vmx_ple())
6832 ple_gap = 0;
6833
6834 if (!cpu_has_vmx_apicv())
6835 enable_apicv = 0;
6836
Haozhong Zhang64903d62015-10-20 15:39:09 +08006837 if (cpu_has_vmx_tsc_scaling()) {
6838 kvm_has_tsc_control = true;
6839 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6840 kvm_tsc_scaling_ratio_frac_bits = 48;
6841 }
6842
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006843 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6844
Tiejun Chenbaa03522014-12-23 16:21:11 +08006845 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006846 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006847 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6848 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006849 0ull, VMX_EPT_EXECUTABLE_MASK,
6850 cpu_has_vmx_ept_execute_only() ?
6851 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006852 ept_set_mmio_spte_mask();
6853 kvm_enable_tdp();
6854 } else
6855 kvm_disable_tdp();
6856
6857 update_ple_window_actual_max();
6858
Kai Huang843e4332015-01-28 10:54:28 +08006859 /*
6860 * Only enable PML when hardware supports PML feature, and both EPT
6861 * and EPT A/D bit features are enabled -- PML depends on them to work.
6862 */
6863 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6864 enable_pml = 0;
6865
6866 if (!enable_pml) {
6867 kvm_x86_ops->slot_enable_log_dirty = NULL;
6868 kvm_x86_ops->slot_disable_log_dirty = NULL;
6869 kvm_x86_ops->flush_log_dirty = NULL;
6870 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6871 }
6872
Yunhong Jiang64672c92016-06-13 14:19:59 -07006873 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6874 u64 vmx_msr;
6875
6876 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6877 cpu_preemption_timer_multi =
6878 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6879 } else {
6880 kvm_x86_ops->set_hv_timer = NULL;
6881 kvm_x86_ops->cancel_hv_timer = NULL;
6882 }
6883
Feng Wubf9f6ac2015-09-18 22:29:55 +08006884 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6885
Ashok Rajc45dcc72016-06-22 14:59:56 +08006886 kvm_mce_cap_supported |= MCG_LMCE_P;
6887
Tiejun Chenf2c76482014-10-28 10:14:47 +08006888 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006889
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006890out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006891 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006892out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006893 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006894out1:
6895 free_page((unsigned long)vmx_io_bitmap_b);
6896out:
6897 free_page((unsigned long)vmx_io_bitmap_a);
6898
6899 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006900}
6901
6902static __exit void hardware_unsetup(void)
6903{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006904 free_page((unsigned long)vmx_io_bitmap_b);
6905 free_page((unsigned long)vmx_io_bitmap_a);
6906 free_page((unsigned long)vmx_vmwrite_bitmap);
6907 free_page((unsigned long)vmx_vmread_bitmap);
6908
Tiejun Chenf2c76482014-10-28 10:14:47 +08006909 free_kvm_area();
6910}
6911
Avi Kivity6aa8b732006-12-10 02:21:36 -08006912/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006913 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6914 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6915 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006916static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006917{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006918 if (ple_gap)
6919 grow_ple_window(vcpu);
6920
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006921 skip_emulated_instruction(vcpu);
6922 kvm_vcpu_on_spin(vcpu);
6923
6924 return 1;
6925}
6926
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006927static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006928{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006929 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006930 return 1;
6931}
6932
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006933static int handle_mwait(struct kvm_vcpu *vcpu)
6934{
6935 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6936 return handle_nop(vcpu);
6937}
6938
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006939static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6940{
6941 return 1;
6942}
6943
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006944static int handle_monitor(struct kvm_vcpu *vcpu)
6945{
6946 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6947 return handle_nop(vcpu);
6948}
6949
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006950/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006951 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6952 * set the success or error code of an emulated VMX instruction, as specified
6953 * by Vol 2B, VMX Instruction Reference, "Conventions".
6954 */
6955static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6956{
6957 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6958 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6959 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6960}
6961
6962static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6963{
6964 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6965 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6966 X86_EFLAGS_SF | X86_EFLAGS_OF))
6967 | X86_EFLAGS_CF);
6968}
6969
Abel Gordon145c28d2013-04-18 14:36:55 +03006970static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006971 u32 vm_instruction_error)
6972{
6973 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6974 /*
6975 * failValid writes the error number to the current VMCS, which
6976 * can't be done there isn't a current VMCS.
6977 */
6978 nested_vmx_failInvalid(vcpu);
6979 return;
6980 }
6981 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6982 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6983 X86_EFLAGS_SF | X86_EFLAGS_OF))
6984 | X86_EFLAGS_ZF);
6985 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6986 /*
6987 * We don't need to force a shadow sync because
6988 * VM_INSTRUCTION_ERROR is not shadowed
6989 */
6990}
Abel Gordon145c28d2013-04-18 14:36:55 +03006991
Wincy Vanff651cb2014-12-11 08:52:58 +03006992static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6993{
6994 /* TODO: not to reset guest simply here. */
6995 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006996 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006997}
6998
Jan Kiszkaf4124502014-03-07 20:03:13 +01006999static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7000{
7001 struct vcpu_vmx *vmx =
7002 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7003
7004 vmx->nested.preemption_timer_expired = true;
7005 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7006 kvm_vcpu_kick(&vmx->vcpu);
7007
7008 return HRTIMER_NORESTART;
7009}
7010
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007011/*
Bandan Das19677e32014-05-06 02:19:15 -04007012 * Decode the memory-address operand of a vmx instruction, as recorded on an
7013 * exit caused by such an instruction (run by a guest hypervisor).
7014 * On success, returns 0. When the operand is invalid, returns 1 and throws
7015 * #UD or #GP.
7016 */
7017static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7018 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007019 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007020{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007021 gva_t off;
7022 bool exn;
7023 struct kvm_segment s;
7024
Bandan Das19677e32014-05-06 02:19:15 -04007025 /*
7026 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7027 * Execution", on an exit, vmx_instruction_info holds most of the
7028 * addressing components of the operand. Only the displacement part
7029 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7030 * For how an actual address is calculated from all these components,
7031 * refer to Vol. 1, "Operand Addressing".
7032 */
7033 int scaling = vmx_instruction_info & 3;
7034 int addr_size = (vmx_instruction_info >> 7) & 7;
7035 bool is_reg = vmx_instruction_info & (1u << 10);
7036 int seg_reg = (vmx_instruction_info >> 15) & 7;
7037 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7038 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7039 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7040 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7041
7042 if (is_reg) {
7043 kvm_queue_exception(vcpu, UD_VECTOR);
7044 return 1;
7045 }
7046
7047 /* Addr = segment_base + offset */
7048 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007049 off = exit_qualification; /* holds the displacement */
Sean Christopherson97483542019-01-23 14:39:23 -08007050 if (addr_size == 1)
7051 off = (gva_t)sign_extend64(off, 31);
7052 else if (addr_size == 0)
7053 off = (gva_t)sign_extend64(off, 15);
Bandan Das19677e32014-05-06 02:19:15 -04007054 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007055 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007056 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007057 off += kvm_register_read(vcpu, index_reg)<<scaling;
7058 vmx_get_segment(vcpu, &s, seg_reg);
7059 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007060
7061 if (addr_size == 1) /* 32 bit */
7062 *ret &= 0xffffffff;
7063
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007064 /* Checks for #GP/#SS exceptions. */
7065 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007066 if (is_long_mode(vcpu)) {
7067 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7068 * non-canonical form. This is the only check on the memory
7069 * destination for long mode!
7070 */
7071 exn = is_noncanonical_address(*ret);
7072 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007073 /* Protected mode: apply checks for segment validity in the
7074 * following order:
7075 * - segment type check (#GP(0) may be thrown)
7076 * - usability check (#GP(0)/#SS(0))
7077 * - limit check (#GP(0)/#SS(0))
7078 */
7079 if (wr)
7080 /* #GP(0) if the destination operand is located in a
7081 * read-only data segment or any code segment.
7082 */
7083 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7084 else
7085 /* #GP(0) if the source operand is located in an
7086 * execute-only code segment
7087 */
7088 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007089 if (exn) {
7090 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7091 return 1;
7092 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007093 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7094 */
7095 exn = (s.unusable != 0);
Sean Christopherson7b3c6c42019-01-23 14:39:25 -08007096
7097 /*
7098 * Protected mode: #GP(0)/#SS(0) if the memory operand is
7099 * outside the segment limit. All CPUs that support VMX ignore
7100 * limit checks for flat segments, i.e. segments with base==0,
7101 * limit==0xffffffff and of type expand-up data or code.
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007102 */
Sean Christopherson7b3c6c42019-01-23 14:39:25 -08007103 if (!(s.base == 0 && s.limit == 0xffffffff &&
7104 ((s.type & 8) || !(s.type & 4))))
7105 exn = exn || (off + sizeof(u64) > s.limit);
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007106 }
7107 if (exn) {
7108 kvm_queue_exception_e(vcpu,
7109 seg_reg == VCPU_SREG_SS ?
7110 SS_VECTOR : GP_VECTOR,
7111 0);
7112 return 1;
7113 }
7114
Bandan Das19677e32014-05-06 02:19:15 -04007115 return 0;
7116}
7117
7118/*
Bandan Das3573e222014-05-06 02:19:16 -04007119 * This function performs the various checks including
7120 * - if it's 4KB aligned
7121 * - No bits beyond the physical address width are set
7122 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04007123 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04007124 */
Bandan Das4291b582014-05-06 02:19:18 -04007125static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
7126 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007127{
7128 gva_t gva;
7129 gpa_t vmptr;
7130 struct x86_exception e;
7131 struct page *page;
7132 struct vcpu_vmx *vmx = to_vmx(vcpu);
7133 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7134
7135 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007136 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007137 return 1;
7138
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007139 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007140 kvm_inject_page_fault(vcpu, &e);
7141 return 1;
7142 }
7143
7144 switch (exit_reason) {
7145 case EXIT_REASON_VMON:
7146 /*
7147 * SDM 3: 24.11.5
7148 * The first 4 bytes of VMXON region contain the supported
7149 * VMCS revision identifier
7150 *
7151 * Note - IA32_VMX_BASIC[48] will never be 1
7152 * for the nested case;
7153 * which replaces physical address width with 32
7154 *
7155 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007156 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007157 nested_vmx_failInvalid(vcpu);
7158 skip_emulated_instruction(vcpu);
7159 return 1;
7160 }
7161
7162 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007163 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007164 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007165 skip_emulated_instruction(vcpu);
7166 return 1;
7167 }
7168 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007169 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007170 nested_release_page_clean(page);
7171 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007172 skip_emulated_instruction(vcpu);
7173 return 1;
7174 }
7175 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007176 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007177 vmx->nested.vmxon_ptr = vmptr;
7178 break;
Bandan Das4291b582014-05-06 02:19:18 -04007179 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007180 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007181 nested_vmx_failValid(vcpu,
7182 VMXERR_VMCLEAR_INVALID_ADDRESS);
7183 skip_emulated_instruction(vcpu);
7184 return 1;
7185 }
Bandan Das3573e222014-05-06 02:19:16 -04007186
Bandan Das4291b582014-05-06 02:19:18 -04007187 if (vmptr == vmx->nested.vmxon_ptr) {
7188 nested_vmx_failValid(vcpu,
7189 VMXERR_VMCLEAR_VMXON_POINTER);
7190 skip_emulated_instruction(vcpu);
7191 return 1;
7192 }
7193 break;
7194 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007195 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007196 nested_vmx_failValid(vcpu,
7197 VMXERR_VMPTRLD_INVALID_ADDRESS);
7198 skip_emulated_instruction(vcpu);
7199 return 1;
7200 }
7201
7202 if (vmptr == vmx->nested.vmxon_ptr) {
7203 nested_vmx_failValid(vcpu,
7204 VMXERR_VMCLEAR_VMXON_POINTER);
7205 skip_emulated_instruction(vcpu);
7206 return 1;
7207 }
7208 break;
Bandan Das3573e222014-05-06 02:19:16 -04007209 default:
7210 return 1; /* shouldn't happen */
7211 }
7212
Bandan Das4291b582014-05-06 02:19:18 -04007213 if (vmpointer)
7214 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007215 return 0;
7216}
7217
7218/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007219 * Emulate the VMXON instruction.
7220 * Currently, we just remember that VMX is active, and do not save or even
7221 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7222 * do not currently need to store anything in that guest-allocated memory
7223 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7224 * argument is different from the VMXON pointer (which the spec says they do).
7225 */
7226static int handle_vmon(struct kvm_vcpu *vcpu)
7227{
7228 struct kvm_segment cs;
7229 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007230 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007231 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7232 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007233 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007234
7235 /* The Intel VMX Instruction Reference lists a bunch of bits that
7236 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7237 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7238 * Otherwise, we should fail with #UD. We test these now:
7239 */
7240 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7241 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7242 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7243 kvm_queue_exception(vcpu, UD_VECTOR);
7244 return 1;
7245 }
7246
7247 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7248 if (is_long_mode(vcpu) && !cs.l) {
7249 kvm_queue_exception(vcpu, UD_VECTOR);
7250 return 1;
7251 }
7252
7253 if (vmx_get_cpl(vcpu)) {
7254 kvm_inject_gp(vcpu, 0);
7255 return 1;
7256 }
Bandan Das3573e222014-05-06 02:19:16 -04007257
Bandan Das4291b582014-05-06 02:19:18 -04007258 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007259 return 1;
7260
Abel Gordon145c28d2013-04-18 14:36:55 +03007261 if (vmx->nested.vmxon) {
7262 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7263 skip_emulated_instruction(vcpu);
7264 return 1;
7265 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007266
Haozhong Zhang3b840802016-06-22 14:59:54 +08007267 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007268 != VMXON_NEEDED_FEATURES) {
7269 kvm_inject_gp(vcpu, 0);
7270 return 1;
7271 }
7272
Paolo Bonziniff546f92018-01-11 12:16:15 +01007273 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7274 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007275 goto out_vmcs02;
Jim Mattson46e24df2017-11-27 17:22:25 -06007276
David Matlack4f2777b2016-07-13 17:16:37 -07007277 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7278 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007279 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007280
Abel Gordon8de48832013-04-18 14:37:25 +03007281 if (enable_shadow_vmcs) {
7282 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007283 if (!shadow_vmcs)
7284 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007285 /* mark vmcs as shadow */
7286 shadow_vmcs->revision_id |= (1u << 31);
7287 /* init shadow vmcs */
7288 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007289 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007290 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007291
Jan Kiszkaf4124502014-03-07 20:03:13 +01007292 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007293 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007294 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7295
Roman Kagan020a90f2018-07-19 21:59:07 +03007296 vmx->nested.vpid02 = allocate_vpid();
7297
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007298 vmx->nested.vmxon = true;
7299
7300 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007301 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007302 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007303
7304out_shadow_vmcs:
7305 kfree(vmx->nested.cached_vmcs12);
7306
7307out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007308 free_loaded_vmcs(&vmx->nested.vmcs02);
7309
7310out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007311 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007312}
7313
7314/*
7315 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7316 * for running VMX instructions (except VMXON, whose prerequisites are
7317 * slightly different). It also specifies what exception to inject otherwise.
7318 */
7319static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7320{
7321 struct kvm_segment cs;
7322 struct vcpu_vmx *vmx = to_vmx(vcpu);
7323
7324 if (!vmx->nested.vmxon) {
7325 kvm_queue_exception(vcpu, UD_VECTOR);
7326 return 0;
7327 }
7328
7329 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7330 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7331 (is_long_mode(vcpu) && !cs.l)) {
7332 kvm_queue_exception(vcpu, UD_VECTOR);
7333 return 0;
7334 }
7335
7336 if (vmx_get_cpl(vcpu)) {
7337 kvm_inject_gp(vcpu, 0);
7338 return 0;
7339 }
7340
7341 return 1;
7342}
7343
Abel Gordone7953d72013-04-18 14:37:55 +03007344static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7345{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007346 if (vmx->nested.current_vmptr == -1ull)
7347 return;
7348
7349 /* current_vmptr and current_vmcs12 are always set/reset together */
7350 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7351 return;
7352
Abel Gordon012f83c2013-04-18 14:39:25 +03007353 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007354 /* copy to memory all shadowed fields in case
7355 they were modified */
7356 copy_shadow_to_vmcs12(vmx);
7357 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007358 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7359 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007360 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007361 }
Wincy Van705699a2015-02-03 23:58:17 +08007362 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007363
7364 /* Flush VMCS12 to guest memory */
7365 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7366 VMCS12_SIZE);
7367
Abel Gordone7953d72013-04-18 14:37:55 +03007368 kunmap(vmx->nested.current_vmcs12_page);
7369 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007370 vmx->nested.current_vmptr = -1ull;
7371 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007372}
7373
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007374/*
7375 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7376 * just stops using VMX.
7377 */
7378static void free_nested(struct vcpu_vmx *vmx)
7379{
7380 if (!vmx->nested.vmxon)
7381 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007382
Peter Shiera2c34d22018-10-11 11:46:46 -07007383 hrtimer_cancel(&vmx->nested.preemption_timer);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007384 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007385 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007386 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007387 if (enable_shadow_vmcs) {
7388 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7389 free_vmcs(vmx->vmcs01.shadow_vmcs);
7390 vmx->vmcs01.shadow_vmcs = NULL;
7391 }
David Matlack4f2777b2016-07-13 17:16:37 -07007392 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007393 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007394 if (vmx->nested.apic_access_page) {
7395 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007396 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007397 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007398 if (vmx->nested.virtual_apic_page) {
7399 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007400 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007401 }
Wincy Van705699a2015-02-03 23:58:17 +08007402 if (vmx->nested.pi_desc_page) {
7403 kunmap(vmx->nested.pi_desc_page);
7404 nested_release_page(vmx->nested.pi_desc_page);
7405 vmx->nested.pi_desc_page = NULL;
7406 vmx->nested.pi_desc = NULL;
7407 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007408
Jim Mattson46e24df2017-11-27 17:22:25 -06007409 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007410}
7411
7412/* Emulate the VMXOFF instruction */
7413static int handle_vmoff(struct kvm_vcpu *vcpu)
7414{
7415 if (!nested_vmx_check_permission(vcpu))
7416 return 1;
7417 free_nested(to_vmx(vcpu));
7418 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007419 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007420 return 1;
7421}
7422
Nadav Har'El27d6c862011-05-25 23:06:59 +03007423/* Emulate the VMCLEAR instruction */
7424static int handle_vmclear(struct kvm_vcpu *vcpu)
7425{
7426 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007427 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007428 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007429
7430 if (!nested_vmx_check_permission(vcpu))
7431 return 1;
7432
Bandan Das4291b582014-05-06 02:19:18 -04007433 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007434 return 1;
7435
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007436 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007437 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007438
Jim Mattson29deec42017-03-02 12:41:48 -08007439 kvm_vcpu_write_guest(vcpu,
7440 vmptr + offsetof(struct vmcs12, launch_state),
7441 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007442
Nadav Har'El27d6c862011-05-25 23:06:59 +03007443 skip_emulated_instruction(vcpu);
7444 nested_vmx_succeed(vcpu);
7445 return 1;
7446}
7447
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007448static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7449
7450/* Emulate the VMLAUNCH instruction */
7451static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7452{
7453 return nested_vmx_run(vcpu, true);
7454}
7455
7456/* Emulate the VMRESUME instruction */
7457static int handle_vmresume(struct kvm_vcpu *vcpu)
7458{
7459
7460 return nested_vmx_run(vcpu, false);
7461}
7462
Nadav Har'El49f705c2011-05-25 23:08:30 +03007463enum vmcs_field_type {
7464 VMCS_FIELD_TYPE_U16 = 0,
7465 VMCS_FIELD_TYPE_U64 = 1,
7466 VMCS_FIELD_TYPE_U32 = 2,
7467 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7468};
7469
7470static inline int vmcs_field_type(unsigned long field)
7471{
7472 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7473 return VMCS_FIELD_TYPE_U32;
7474 return (field >> 13) & 0x3 ;
7475}
7476
7477static inline int vmcs_field_readonly(unsigned long field)
7478{
7479 return (((field >> 10) & 0x3) == 1);
7480}
7481
7482/*
7483 * Read a vmcs12 field. Since these can have varying lengths and we return
7484 * one type, we chose the biggest type (u64) and zero-extend the return value
7485 * to that size. Note that the caller, handle_vmread, might need to use only
7486 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7487 * 64-bit fields are to be returned).
7488 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007489static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7490 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007491{
7492 short offset = vmcs_field_to_offset(field);
7493 char *p;
7494
7495 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007496 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007497
7498 p = ((char *)(get_vmcs12(vcpu))) + offset;
7499
7500 switch (vmcs_field_type(field)) {
7501 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7502 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007503 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007504 case VMCS_FIELD_TYPE_U16:
7505 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007506 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007507 case VMCS_FIELD_TYPE_U32:
7508 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007509 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007510 case VMCS_FIELD_TYPE_U64:
7511 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007512 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007513 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007514 WARN_ON(1);
7515 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007516 }
7517}
7518
Abel Gordon20b97fe2013-04-18 14:36:25 +03007519
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007520static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7521 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007522 short offset = vmcs_field_to_offset(field);
7523 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7524 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007525 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007526
7527 switch (vmcs_field_type(field)) {
7528 case VMCS_FIELD_TYPE_U16:
7529 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007530 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007531 case VMCS_FIELD_TYPE_U32:
7532 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007533 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007534 case VMCS_FIELD_TYPE_U64:
7535 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007536 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007537 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7538 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007539 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007540 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007541 WARN_ON(1);
7542 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007543 }
7544
7545}
7546
Abel Gordon16f5b902013-04-18 14:38:25 +03007547static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7548{
7549 int i;
7550 unsigned long field;
7551 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007552 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007553 const unsigned long *fields = shadow_read_write_fields;
7554 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007555
Jan Kiszka282da872014-10-08 18:05:39 +02007556 preempt_disable();
7557
Abel Gordon16f5b902013-04-18 14:38:25 +03007558 vmcs_load(shadow_vmcs);
7559
7560 for (i = 0; i < num_fields; i++) {
7561 field = fields[i];
7562 switch (vmcs_field_type(field)) {
7563 case VMCS_FIELD_TYPE_U16:
7564 field_value = vmcs_read16(field);
7565 break;
7566 case VMCS_FIELD_TYPE_U32:
7567 field_value = vmcs_read32(field);
7568 break;
7569 case VMCS_FIELD_TYPE_U64:
7570 field_value = vmcs_read64(field);
7571 break;
7572 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7573 field_value = vmcs_readl(field);
7574 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007575 default:
7576 WARN_ON(1);
7577 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007578 }
7579 vmcs12_write_any(&vmx->vcpu, field, field_value);
7580 }
7581
7582 vmcs_clear(shadow_vmcs);
7583 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007584
7585 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007586}
7587
Abel Gordonc3114422013-04-18 14:38:55 +03007588static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7589{
Mathias Krausec2bae892013-06-26 20:36:21 +02007590 const unsigned long *fields[] = {
7591 shadow_read_write_fields,
7592 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007593 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007594 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007595 max_shadow_read_write_fields,
7596 max_shadow_read_only_fields
7597 };
7598 int i, q;
7599 unsigned long field;
7600 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007601 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007602
7603 vmcs_load(shadow_vmcs);
7604
Mathias Krausec2bae892013-06-26 20:36:21 +02007605 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007606 for (i = 0; i < max_fields[q]; i++) {
7607 field = fields[q][i];
7608 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7609
7610 switch (vmcs_field_type(field)) {
7611 case VMCS_FIELD_TYPE_U16:
7612 vmcs_write16(field, (u16)field_value);
7613 break;
7614 case VMCS_FIELD_TYPE_U32:
7615 vmcs_write32(field, (u32)field_value);
7616 break;
7617 case VMCS_FIELD_TYPE_U64:
7618 vmcs_write64(field, (u64)field_value);
7619 break;
7620 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7621 vmcs_writel(field, (long)field_value);
7622 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007623 default:
7624 WARN_ON(1);
7625 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007626 }
7627 }
7628 }
7629
7630 vmcs_clear(shadow_vmcs);
7631 vmcs_load(vmx->loaded_vmcs->vmcs);
7632}
7633
Nadav Har'El49f705c2011-05-25 23:08:30 +03007634/*
7635 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7636 * used before) all generate the same failure when it is missing.
7637 */
7638static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7639{
7640 struct vcpu_vmx *vmx = to_vmx(vcpu);
7641 if (vmx->nested.current_vmptr == -1ull) {
7642 nested_vmx_failInvalid(vcpu);
7643 skip_emulated_instruction(vcpu);
7644 return 0;
7645 }
7646 return 1;
7647}
7648
7649static int handle_vmread(struct kvm_vcpu *vcpu)
7650{
7651 unsigned long field;
7652 u64 field_value;
7653 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7654 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7655 gva_t gva = 0;
7656
7657 if (!nested_vmx_check_permission(vcpu) ||
7658 !nested_vmx_check_vmcs12(vcpu))
7659 return 1;
7660
7661 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007662 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007663 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007664 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007665 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7666 skip_emulated_instruction(vcpu);
7667 return 1;
7668 }
7669 /*
7670 * Now copy part of this value to register or memory, as requested.
7671 * Note that the number of bits actually copied is 32 or 64 depending
7672 * on the guest's mode (32 or 64 bit), not on the given field's length.
7673 */
7674 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007675 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007676 field_value);
7677 } else {
7678 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007679 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007680 return 1;
7681 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007682 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7683 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007684 }
7685
7686 nested_vmx_succeed(vcpu);
7687 skip_emulated_instruction(vcpu);
7688 return 1;
7689}
7690
7691
7692static int handle_vmwrite(struct kvm_vcpu *vcpu)
7693{
7694 unsigned long field;
7695 gva_t gva;
7696 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7697 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007698 /* The value to write might be 32 or 64 bits, depending on L1's long
7699 * mode, and eventually we need to write that into a field of several
7700 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007701 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007702 * bits into the vmcs12 field.
7703 */
7704 u64 field_value = 0;
7705 struct x86_exception e;
7706
7707 if (!nested_vmx_check_permission(vcpu) ||
7708 !nested_vmx_check_vmcs12(vcpu))
7709 return 1;
7710
7711 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007712 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007713 (((vmx_instruction_info) >> 3) & 0xf));
7714 else {
7715 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007716 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007717 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007718 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7719 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007720 kvm_inject_page_fault(vcpu, &e);
7721 return 1;
7722 }
7723 }
7724
7725
Nadav Amit27e6fb52014-06-18 17:19:26 +03007726 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007727 if (vmcs_field_readonly(field)) {
7728 nested_vmx_failValid(vcpu,
7729 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7730 skip_emulated_instruction(vcpu);
7731 return 1;
7732 }
7733
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007734 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007735 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7736 skip_emulated_instruction(vcpu);
7737 return 1;
7738 }
7739
7740 nested_vmx_succeed(vcpu);
7741 skip_emulated_instruction(vcpu);
7742 return 1;
7743}
7744
Nadav Har'El63846662011-05-25 23:07:29 +03007745/* Emulate the VMPTRLD instruction */
7746static int handle_vmptrld(struct kvm_vcpu *vcpu)
7747{
7748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007749 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007750
7751 if (!nested_vmx_check_permission(vcpu))
7752 return 1;
7753
Bandan Das4291b582014-05-06 02:19:18 -04007754 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007755 return 1;
7756
Nadav Har'El63846662011-05-25 23:07:29 +03007757 if (vmx->nested.current_vmptr != vmptr) {
7758 struct vmcs12 *new_vmcs12;
7759 struct page *page;
7760 page = nested_get_page(vcpu, vmptr);
7761 if (page == NULL) {
7762 nested_vmx_failInvalid(vcpu);
7763 skip_emulated_instruction(vcpu);
7764 return 1;
7765 }
7766 new_vmcs12 = kmap(page);
7767 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7768 kunmap(page);
7769 nested_release_page_clean(page);
7770 nested_vmx_failValid(vcpu,
7771 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7772 skip_emulated_instruction(vcpu);
7773 return 1;
7774 }
Nadav Har'El63846662011-05-25 23:07:29 +03007775
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007776 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007777 vmx->nested.current_vmptr = vmptr;
7778 vmx->nested.current_vmcs12 = new_vmcs12;
7779 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007780 /*
7781 * Load VMCS12 from guest memory since it is not already
7782 * cached.
7783 */
7784 memcpy(vmx->nested.cached_vmcs12,
7785 vmx->nested.current_vmcs12, VMCS12_SIZE);
7786
Abel Gordon012f83c2013-04-18 14:39:25 +03007787 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007788 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7789 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007790 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007791 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007792 vmx->nested.sync_shadow_vmcs = true;
7793 }
Nadav Har'El63846662011-05-25 23:07:29 +03007794 }
7795
7796 nested_vmx_succeed(vcpu);
7797 skip_emulated_instruction(vcpu);
7798 return 1;
7799}
7800
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007801/* Emulate the VMPTRST instruction */
7802static int handle_vmptrst(struct kvm_vcpu *vcpu)
7803{
7804 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7805 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7806 gva_t vmcs_gva;
7807 struct x86_exception e;
7808
7809 if (!nested_vmx_check_permission(vcpu))
7810 return 1;
7811
7812 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007813 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007814 return 1;
7815 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007816 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7817 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7818 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007819 kvm_inject_page_fault(vcpu, &e);
7820 return 1;
7821 }
7822 nested_vmx_succeed(vcpu);
7823 skip_emulated_instruction(vcpu);
7824 return 1;
7825}
7826
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007827/* Emulate the INVEPT instruction */
7828static int handle_invept(struct kvm_vcpu *vcpu)
7829{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007830 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007831 u32 vmx_instruction_info, types;
7832 unsigned long type;
7833 gva_t gva;
7834 struct x86_exception e;
7835 struct {
7836 u64 eptp, gpa;
7837 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007838
Wincy Vanb9c237b2015-02-03 23:56:30 +08007839 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7840 SECONDARY_EXEC_ENABLE_EPT) ||
7841 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007842 kvm_queue_exception(vcpu, UD_VECTOR);
7843 return 1;
7844 }
7845
7846 if (!nested_vmx_check_permission(vcpu))
7847 return 1;
7848
7849 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7850 kvm_queue_exception(vcpu, UD_VECTOR);
7851 return 1;
7852 }
7853
7854 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007855 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007856
Wincy Vanb9c237b2015-02-03 23:56:30 +08007857 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007858
Jim Mattson85c856b2016-10-26 08:38:38 -07007859 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007860 nested_vmx_failValid(vcpu,
7861 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007862 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007863 return 1;
7864 }
7865
7866 /* According to the Intel VMX instruction reference, the memory
7867 * operand is read even if it isn't needed (e.g., for type==global)
7868 */
7869 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007870 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007871 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007872 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007873 kvm_inject_page_fault(vcpu, &e);
7874 return 1;
7875 }
7876
7877 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007878 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007879 /*
7880 * TODO: track mappings and invalidate
7881 * single context requests appropriately
7882 */
7883 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007884 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007885 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007886 nested_vmx_succeed(vcpu);
7887 break;
7888 default:
7889 BUG_ON(1);
7890 break;
7891 }
7892
7893 skip_emulated_instruction(vcpu);
7894 return 1;
7895}
7896
Petr Matouseka642fc32014-09-23 20:22:30 +02007897static int handle_invvpid(struct kvm_vcpu *vcpu)
7898{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007899 struct vcpu_vmx *vmx = to_vmx(vcpu);
7900 u32 vmx_instruction_info;
7901 unsigned long type, types;
7902 gva_t gva;
7903 struct x86_exception e;
7904 int vpid;
7905
7906 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7907 SECONDARY_EXEC_ENABLE_VPID) ||
7908 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7909 kvm_queue_exception(vcpu, UD_VECTOR);
7910 return 1;
7911 }
7912
7913 if (!nested_vmx_check_permission(vcpu))
7914 return 1;
7915
7916 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7917 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7918
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007919 types = (vmx->nested.nested_vmx_vpid_caps &
7920 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007921
Jim Mattson85c856b2016-10-26 08:38:38 -07007922 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007923 nested_vmx_failValid(vcpu,
7924 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007925 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007926 return 1;
7927 }
7928
7929 /* according to the intel vmx instruction reference, the memory
7930 * operand is read even if it isn't needed (e.g., for type==global)
7931 */
7932 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7933 vmx_instruction_info, false, &gva))
7934 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007935 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007936 kvm_inject_page_fault(vcpu, &e);
7937 return 1;
7938 }
7939
7940 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007941 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007942 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007943 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7944 if (!vpid) {
7945 nested_vmx_failValid(vcpu,
7946 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7947 skip_emulated_instruction(vcpu);
7948 return 1;
7949 }
7950 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007951 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007952 break;
7953 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007954 WARN_ON_ONCE(1);
7955 skip_emulated_instruction(vcpu);
7956 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007957 }
7958
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007959 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7960 nested_vmx_succeed(vcpu);
7961
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007962 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007963 return 1;
7964}
7965
Kai Huang843e4332015-01-28 10:54:28 +08007966static int handle_pml_full(struct kvm_vcpu *vcpu)
7967{
7968 unsigned long exit_qualification;
7969
7970 trace_kvm_pml_full(vcpu->vcpu_id);
7971
7972 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7973
7974 /*
7975 * PML buffer FULL happened while executing iret from NMI,
7976 * "blocked by NMI" bit has to be set before next VM entry.
7977 */
7978 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7979 cpu_has_virtual_nmis() &&
7980 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7981 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7982 GUEST_INTR_STATE_NMI);
7983
7984 /*
7985 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7986 * here.., and there's no userspace involvement needed for PML.
7987 */
7988 return 1;
7989}
7990
Yunhong Jiang64672c92016-06-13 14:19:59 -07007991static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7992{
7993 kvm_lapic_expired_hv_timer(vcpu);
7994 return 1;
7995}
7996
Nadav Har'El0140cae2011-05-25 23:06:28 +03007997/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007998 * The exit handlers return 1 if the exit was handled fully and guest execution
7999 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8000 * to be done to userspace and return 0.
8001 */
Mathias Krause772e0312012-08-30 01:30:19 +02008002static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008003 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8004 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008005 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008006 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008007 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008008 [EXIT_REASON_CR_ACCESS] = handle_cr,
8009 [EXIT_REASON_DR_ACCESS] = handle_dr,
8010 [EXIT_REASON_CPUID] = handle_cpuid,
8011 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8012 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8013 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8014 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008015 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008016 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008017 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008018 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008019 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008020 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008021 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008022 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008023 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008024 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008025 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008026 [EXIT_REASON_VMOFF] = handle_vmoff,
8027 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008028 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8029 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008030 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008031 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008032 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008033 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008034 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008035 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008036 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8037 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008038 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008039 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008040 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008041 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008042 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008043 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008044 [EXIT_REASON_XSAVES] = handle_xsaves,
8045 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008046 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008047 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008048};
8049
8050static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008051 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008052
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008053static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8054 struct vmcs12 *vmcs12)
8055{
8056 unsigned long exit_qualification;
8057 gpa_t bitmap, last_bitmap;
8058 unsigned int port;
8059 int size;
8060 u8 b;
8061
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008062 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008063 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008064
8065 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8066
8067 port = exit_qualification >> 16;
8068 size = (exit_qualification & 7) + 1;
8069
8070 last_bitmap = (gpa_t)-1;
8071 b = -1;
8072
8073 while (size > 0) {
8074 if (port < 0x8000)
8075 bitmap = vmcs12->io_bitmap_a;
8076 else if (port < 0x10000)
8077 bitmap = vmcs12->io_bitmap_b;
8078 else
Joe Perches1d804d02015-03-30 16:46:09 -07008079 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008080 bitmap += (port & 0x7fff) / 8;
8081
8082 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008083 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008084 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008085 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008086 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008087
8088 port++;
8089 size--;
8090 last_bitmap = bitmap;
8091 }
8092
Joe Perches1d804d02015-03-30 16:46:09 -07008093 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008094}
8095
Nadav Har'El644d7112011-05-25 23:12:35 +03008096/*
8097 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8098 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8099 * disinterest in the current event (read or write a specific MSR) by using an
8100 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8101 */
8102static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8103 struct vmcs12 *vmcs12, u32 exit_reason)
8104{
8105 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8106 gpa_t bitmap;
8107
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008108 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008109 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008110
8111 /*
8112 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8113 * for the four combinations of read/write and low/high MSR numbers.
8114 * First we need to figure out which of the four to use:
8115 */
8116 bitmap = vmcs12->msr_bitmap;
8117 if (exit_reason == EXIT_REASON_MSR_WRITE)
8118 bitmap += 2048;
8119 if (msr_index >= 0xc0000000) {
8120 msr_index -= 0xc0000000;
8121 bitmap += 1024;
8122 }
8123
8124 /* Then read the msr_index'th bit from this bitmap: */
8125 if (msr_index < 1024*8) {
8126 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008127 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008128 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008129 return 1 & (b >> (msr_index & 7));
8130 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008131 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008132}
8133
8134/*
8135 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8136 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8137 * intercept (via guest_host_mask etc.) the current event.
8138 */
8139static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8140 struct vmcs12 *vmcs12)
8141{
8142 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8143 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008144 int reg;
8145 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008146
8147 switch ((exit_qualification >> 4) & 3) {
8148 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008149 reg = (exit_qualification >> 8) & 15;
8150 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008151 switch (cr) {
8152 case 0:
8153 if (vmcs12->cr0_guest_host_mask &
8154 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008155 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008156 break;
8157 case 3:
8158 if ((vmcs12->cr3_target_count >= 1 &&
8159 vmcs12->cr3_target_value0 == val) ||
8160 (vmcs12->cr3_target_count >= 2 &&
8161 vmcs12->cr3_target_value1 == val) ||
8162 (vmcs12->cr3_target_count >= 3 &&
8163 vmcs12->cr3_target_value2 == val) ||
8164 (vmcs12->cr3_target_count >= 4 &&
8165 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008166 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008167 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008168 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008169 break;
8170 case 4:
8171 if (vmcs12->cr4_guest_host_mask &
8172 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008173 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008174 break;
8175 case 8:
8176 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008177 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008178 break;
8179 }
8180 break;
8181 case 2: /* clts */
8182 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8183 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008184 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008185 break;
8186 case 1: /* mov from cr */
8187 switch (cr) {
8188 case 3:
8189 if (vmcs12->cpu_based_vm_exec_control &
8190 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008191 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008192 break;
8193 case 8:
8194 if (vmcs12->cpu_based_vm_exec_control &
8195 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008196 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008197 break;
8198 }
8199 break;
8200 case 3: /* lmsw */
8201 /*
8202 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8203 * cr0. Other attempted changes are ignored, with no exit.
8204 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008205 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008206 if (vmcs12->cr0_guest_host_mask & 0xe &
8207 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008208 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008209 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8210 !(vmcs12->cr0_read_shadow & 0x1) &&
8211 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008212 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008213 break;
8214 }
Joe Perches1d804d02015-03-30 16:46:09 -07008215 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008216}
8217
8218/*
8219 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8220 * should handle it ourselves in L0 (and then continue L2). Only call this
8221 * when in is_guest_mode (L2).
8222 */
8223static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8224{
Nadav Har'El644d7112011-05-25 23:12:35 +03008225 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8226 struct vcpu_vmx *vmx = to_vmx(vcpu);
8227 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008228 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008229
Jan Kiszka542060e2014-01-04 18:47:21 +01008230 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8231 vmcs_readl(EXIT_QUALIFICATION),
8232 vmx->idt_vectoring_info,
8233 intr_info,
8234 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8235 KVM_ISA_VMX);
8236
David Matlackb7649e12017-08-01 14:00:40 -07008237 /*
8238 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008239 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8240 * Page). The CPU may write to these pages via their host
8241 * physical address while L2 is running, bypassing any
8242 * address-translation-based dirty tracking (e.g. EPT write
8243 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008244 *
8245 * Mark them dirty on every exit from L2 to prevent them from
8246 * getting out of sync with dirty tracking.
8247 */
8248 nested_mark_vmcs12_pages_dirty(vcpu);
8249
Nadav Har'El644d7112011-05-25 23:12:35 +03008250 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008251 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008252
8253 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008254 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8255 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008256 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008257 }
8258
8259 switch (exit_reason) {
8260 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008261 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008262 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008263 else if (is_page_fault(intr_info))
8264 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008265 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008266 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008267 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008268 else if (is_debug(intr_info) &&
8269 vcpu->guest_debug &
8270 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8271 return false;
8272 else if (is_breakpoint(intr_info) &&
8273 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8274 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008275 return vmcs12->exception_bitmap &
8276 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8277 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008278 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008279 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008280 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008281 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008282 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008283 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008284 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008285 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008286 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008287 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008288 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008289 case EXIT_REASON_HLT:
8290 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8291 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008292 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008293 case EXIT_REASON_INVLPG:
8294 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8295 case EXIT_REASON_RDPMC:
8296 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008297 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008298 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8299 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8300 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8301 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8302 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8303 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008304 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008305 /*
8306 * VMX instructions trap unconditionally. This allows L1 to
8307 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8308 */
Joe Perches1d804d02015-03-30 16:46:09 -07008309 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008310 case EXIT_REASON_CR_ACCESS:
8311 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8312 case EXIT_REASON_DR_ACCESS:
8313 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8314 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008315 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008316 case EXIT_REASON_MSR_READ:
8317 case EXIT_REASON_MSR_WRITE:
8318 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8319 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008320 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008321 case EXIT_REASON_MWAIT_INSTRUCTION:
8322 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008323 case EXIT_REASON_MONITOR_TRAP_FLAG:
8324 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008325 case EXIT_REASON_MONITOR_INSTRUCTION:
8326 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8327 case EXIT_REASON_PAUSE_INSTRUCTION:
8328 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8329 nested_cpu_has2(vmcs12,
8330 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8331 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008332 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008333 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008334 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008335 case EXIT_REASON_APIC_ACCESS:
8336 return nested_cpu_has2(vmcs12,
8337 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008338 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008339 case EXIT_REASON_EOI_INDUCED:
8340 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008341 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008342 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008343 /*
8344 * L0 always deals with the EPT violation. If nested EPT is
8345 * used, and the nested mmu code discovers that the address is
8346 * missing in the guest EPT table (EPT12), the EPT violation
8347 * will be injected with nested_ept_inject_page_fault()
8348 */
Joe Perches1d804d02015-03-30 16:46:09 -07008349 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008350 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008351 /*
8352 * L2 never uses directly L1's EPT, but rather L0's own EPT
8353 * table (shadow on EPT) or a merged EPT table that L0 built
8354 * (EPT on EPT). So any problems with the structure of the
8355 * table is L0's fault.
8356 */
Joe Perches1d804d02015-03-30 16:46:09 -07008357 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008358 case EXIT_REASON_WBINVD:
8359 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8360 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008361 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008362 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8363 /*
8364 * This should never happen, since it is not possible to
8365 * set XSS to a non-zero value---neither in L1 nor in L2.
8366 * If if it were, XSS would have to be checked against
8367 * the XSS exit bitmap in vmcs12.
8368 */
8369 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008370 case EXIT_REASON_PREEMPTION_TIMER:
8371 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008372 case EXIT_REASON_PML_FULL:
8373 /* We don't expose PML support to L1. */
8374 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008375 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008376 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008377 }
8378}
8379
Avi Kivity586f9602010-11-18 13:09:54 +02008380static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8381{
8382 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8383 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8384}
8385
Kai Huanga3eaa862015-11-04 13:46:05 +08008386static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008387{
Kai Huanga3eaa862015-11-04 13:46:05 +08008388 if (vmx->pml_pg) {
8389 __free_page(vmx->pml_pg);
8390 vmx->pml_pg = NULL;
8391 }
Kai Huang843e4332015-01-28 10:54:28 +08008392}
8393
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008394static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008395{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008396 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008397 u64 *pml_buf;
8398 u16 pml_idx;
8399
8400 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8401
8402 /* Do nothing if PML buffer is empty */
8403 if (pml_idx == (PML_ENTITY_NUM - 1))
8404 return;
8405
8406 /* PML index always points to next available PML buffer entity */
8407 if (pml_idx >= PML_ENTITY_NUM)
8408 pml_idx = 0;
8409 else
8410 pml_idx++;
8411
8412 pml_buf = page_address(vmx->pml_pg);
8413 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8414 u64 gpa;
8415
8416 gpa = pml_buf[pml_idx];
8417 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008418 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008419 }
8420
8421 /* reset PML index */
8422 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8423}
8424
8425/*
8426 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8427 * Called before reporting dirty_bitmap to userspace.
8428 */
8429static void kvm_flush_pml_buffers(struct kvm *kvm)
8430{
8431 int i;
8432 struct kvm_vcpu *vcpu;
8433 /*
8434 * We only need to kick vcpu out of guest mode here, as PML buffer
8435 * is flushed at beginning of all VMEXITs, and it's obvious that only
8436 * vcpus running in guest are possible to have unflushed GPAs in PML
8437 * buffer.
8438 */
8439 kvm_for_each_vcpu(i, vcpu, kvm)
8440 kvm_vcpu_kick(vcpu);
8441}
8442
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008443static void vmx_dump_sel(char *name, uint32_t sel)
8444{
8445 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008446 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008447 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8448 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8449 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8450}
8451
8452static void vmx_dump_dtsel(char *name, uint32_t limit)
8453{
8454 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8455 name, vmcs_read32(limit),
8456 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8457}
8458
8459static void dump_vmcs(void)
8460{
8461 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8462 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8463 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8464 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8465 u32 secondary_exec_control = 0;
8466 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008467 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008468 int i, n;
8469
8470 if (cpu_has_secondary_exec_ctrls())
8471 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8472
8473 pr_err("*** Guest State ***\n");
8474 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8475 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8476 vmcs_readl(CR0_GUEST_HOST_MASK));
8477 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8478 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8479 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8480 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8481 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8482 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008483 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8484 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8485 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8486 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008487 }
8488 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8489 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8490 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8491 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8492 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8493 vmcs_readl(GUEST_SYSENTER_ESP),
8494 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8495 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8496 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8497 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8498 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8499 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8500 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8501 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8502 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8503 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8504 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8505 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8506 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008507 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8508 efer, vmcs_read64(GUEST_IA32_PAT));
8509 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8510 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008511 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8512 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008513 pr_err("PerfGlobCtl = 0x%016llx\n",
8514 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008515 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008516 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008517 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8518 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8519 vmcs_read32(GUEST_ACTIVITY_STATE));
8520 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8521 pr_err("InterruptStatus = %04x\n",
8522 vmcs_read16(GUEST_INTR_STATUS));
8523
8524 pr_err("*** Host State ***\n");
8525 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8526 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8527 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8528 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8529 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8530 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8531 vmcs_read16(HOST_TR_SELECTOR));
8532 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8533 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8534 vmcs_readl(HOST_TR_BASE));
8535 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8536 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8537 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8538 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8539 vmcs_readl(HOST_CR4));
8540 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8541 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8542 vmcs_read32(HOST_IA32_SYSENTER_CS),
8543 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8544 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008545 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8546 vmcs_read64(HOST_IA32_EFER),
8547 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008548 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008549 pr_err("PerfGlobCtl = 0x%016llx\n",
8550 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008551
8552 pr_err("*** Control State ***\n");
8553 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8554 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8555 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8556 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8557 vmcs_read32(EXCEPTION_BITMAP),
8558 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8559 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8560 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8561 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8562 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8563 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8564 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8565 vmcs_read32(VM_EXIT_INTR_INFO),
8566 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8567 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8568 pr_err(" reason=%08x qualification=%016lx\n",
8569 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8570 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8571 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8572 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008573 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008574 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008575 pr_err("TSC Multiplier = 0x%016llx\n",
8576 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008577 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8578 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8579 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8580 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8581 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008582 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008583 n = vmcs_read32(CR3_TARGET_COUNT);
8584 for (i = 0; i + 1 < n; i += 4)
8585 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8586 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8587 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8588 if (i < n)
8589 pr_err("CR3 target%u=%016lx\n",
8590 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8591 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8592 pr_err("PLE Gap=%08x Window=%08x\n",
8593 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8594 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8595 pr_err("Virtual processor ID = 0x%04x\n",
8596 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8597}
8598
Avi Kivity6aa8b732006-12-10 02:21:36 -08008599/*
8600 * The guest has exited. See if we can fix it or if we need userspace
8601 * assistance.
8602 */
Avi Kivity851ba692009-08-24 11:10:17 +03008603static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008604{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008605 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008606 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008607 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008608
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008609 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8610
Kai Huang843e4332015-01-28 10:54:28 +08008611 /*
8612 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8613 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8614 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8615 * mode as if vcpus is in root mode, the PML buffer must has been
8616 * flushed already.
8617 */
8618 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008619 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008620
Mohammed Gamal80ced182009-09-01 12:48:18 +02008621 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008622 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008623 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008624
Nadav Har'El644d7112011-05-25 23:12:35 +03008625 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008626 nested_vmx_vmexit(vcpu, exit_reason,
8627 vmcs_read32(VM_EXIT_INTR_INFO),
8628 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008629 return 1;
8630 }
8631
Mohammed Gamal51207022010-05-31 22:40:54 +03008632 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008633 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008634 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8635 vcpu->run->fail_entry.hardware_entry_failure_reason
8636 = exit_reason;
8637 return 0;
8638 }
8639
Avi Kivity29bd8a72007-09-10 17:27:03 +03008640 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008641 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8642 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008643 = vmcs_read32(VM_INSTRUCTION_ERROR);
8644 return 0;
8645 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008646
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008647 /*
8648 * Note:
8649 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8650 * delivery event since it indicates guest is accessing MMIO.
8651 * The vm-exit can be triggered again after return to guest that
8652 * will cause infinite loop.
8653 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008654 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008655 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008656 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008657 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008658 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8659 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8660 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8661 vcpu->run->internal.ndata = 2;
8662 vcpu->run->internal.data[0] = vectoring_info;
8663 vcpu->run->internal.data[1] = exit_reason;
8664 return 0;
8665 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008666
Nadav Har'El644d7112011-05-25 23:12:35 +03008667 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8668 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008669 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008670 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008671 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008672 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008673 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008674 /*
8675 * This CPU don't support us in finding the end of an
8676 * NMI-blocked window if the guest runs with IRQs
8677 * disabled. So we pull the trigger after 1 s of
8678 * futile waiting, but inform the user about this.
8679 */
8680 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8681 "state on VCPU %d after 1 s timeout\n",
8682 __func__, vcpu->vcpu_id);
8683 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008684 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008685 }
8686
Avi Kivity6aa8b732006-12-10 02:21:36 -08008687 if (exit_reason < kvm_vmx_max_exit_handlers
8688 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008689 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008690 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008691 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8692 kvm_queue_exception(vcpu, UD_VECTOR);
8693 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008694 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008695}
8696
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008697/*
8698 * Software based L1D cache flush which is used when microcode providing
8699 * the cache control MSR is not loaded.
8700 *
8701 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8702 * flush it is required to read in 64 KiB because the replacement algorithm
8703 * is not exactly LRU. This could be sized at runtime via topology
8704 * information but as all relevant affected CPUs have 32KiB L1D cache size
8705 * there is no point in doing so.
8706 */
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008707static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008708{
8709 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008710
8711 /*
Thomas Gleixner31282cf2018-07-13 16:23:17 +02008712 * This code is only executed when the the flush mode is 'cond' or
8713 * 'always'
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008714 */
Nicolai Stange936f5662018-07-21 22:25:00 +02008715 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stangee371c922018-07-27 13:22:16 +02008716 bool flush_l1d;
Nicolai Stange90bc3062018-07-21 22:35:28 +02008717
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008718 /*
Nicolai Stangee371c922018-07-27 13:22:16 +02008719 * Clear the per-vcpu flush bit, it gets set again
8720 * either from vcpu_run() or from one of the unsafe
8721 * VMEXIT handlers.
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008722 */
Nicolai Stangee371c922018-07-27 13:22:16 +02008723 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixnerdff09822018-07-13 16:23:20 +02008724 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stangee371c922018-07-27 13:22:16 +02008725
8726 /*
8727 * Clear the per-cpu flush bit, it gets set again from
8728 * the interrupt handlers.
8729 */
8730 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
8731 kvm_clear_cpu_l1tf_flush_l1d();
8732
Nicolai Stange90bc3062018-07-21 22:35:28 +02008733 if (!flush_l1d)
8734 return;
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008735 }
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008736
8737 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008738
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008739 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8740 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8741 return;
8742 }
8743
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008744 asm volatile(
8745 /* First ensure the pages are in the TLB */
8746 "xorl %%eax, %%eax\n"
8747 ".Lpopulate_tlb:\n\t"
Nicolai Stange587d4992018-07-18 19:07:38 +02008748 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008749 "addl $4096, %%eax\n\t"
8750 "cmpl %%eax, %[size]\n\t"
8751 "jne .Lpopulate_tlb\n\t"
8752 "xorl %%eax, %%eax\n\t"
8753 "cpuid\n\t"
8754 /* Now fill the cache */
8755 "xorl %%eax, %%eax\n"
8756 ".Lfill_cache:\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008757 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008758 "addl $64, %%eax\n\t"
8759 "cmpl %%eax, %[size]\n\t"
8760 "jne .Lfill_cache\n\t"
8761 "lfence\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008762 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008763 [size] "r" (size)
8764 : "eax", "ebx", "ecx", "edx");
8765}
8766
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008767static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008768{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008769 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8770
8771 if (is_guest_mode(vcpu) &&
8772 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8773 return;
8774
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008775 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008776 vmcs_write32(TPR_THRESHOLD, 0);
8777 return;
8778 }
8779
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008780 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008781}
8782
Yang Zhang8d146952013-01-25 10:18:50 +08008783static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8784{
8785 u32 sec_exec_control;
8786
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008787 /* Postpone execution until vmcs01 is the current VMCS. */
8788 if (is_guest_mode(vcpu)) {
8789 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8790 return;
8791 }
8792
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008793 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008794 return;
8795
Paolo Bonzini35754c92015-07-29 12:05:37 +02008796 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008797 return;
8798
8799 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8800
8801 if (set) {
8802 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8803 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8804 } else {
8805 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8806 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008807 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008808 }
8809 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8810
Paolo Bonzini6236b782018-01-16 16:51:18 +01008811 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008812}
8813
Tang Chen38b99172014-09-24 15:57:54 +08008814static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8815{
8816 struct vcpu_vmx *vmx = to_vmx(vcpu);
8817
8818 /*
8819 * Currently we do not handle the nested case where L2 has an
8820 * APIC access page of its own; that page is still pinned.
8821 * Hence, we skip the case where the VCPU is in guest mode _and_
8822 * L1 prepared an APIC access page for L2.
8823 *
8824 * For the case where L1 and L2 share the same APIC access page
8825 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8826 * in the vmcs12), this function will only update either the vmcs01
8827 * or the vmcs02. If the former, the vmcs02 will be updated by
8828 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8829 * the next L2->L1 exit.
8830 */
8831 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008832 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008833 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008834 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008835 vmx_flush_tlb_ept_only(vcpu);
8836 }
Tang Chen38b99172014-09-24 15:57:54 +08008837}
8838
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008839static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008840{
8841 u16 status;
8842 u8 old;
8843
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008844 if (max_isr == -1)
8845 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008846
8847 status = vmcs_read16(GUEST_INTR_STATUS);
8848 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008849 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008850 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008851 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008852 vmcs_write16(GUEST_INTR_STATUS, status);
8853 }
8854}
8855
8856static void vmx_set_rvi(int vector)
8857{
8858 u16 status;
8859 u8 old;
8860
Wei Wang4114c272014-11-05 10:53:43 +08008861 if (vector == -1)
8862 vector = 0;
8863
Yang Zhangc7c9c562013-01-25 10:18:51 +08008864 status = vmcs_read16(GUEST_INTR_STATUS);
8865 old = (u8)status & 0xff;
8866 if ((u8)vector != old) {
8867 status &= ~0xff;
8868 status |= (u8)vector;
8869 vmcs_write16(GUEST_INTR_STATUS, status);
8870 }
8871}
8872
8873static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8874{
Wanpeng Li963fee12014-07-17 19:03:00 +08008875 if (!is_guest_mode(vcpu)) {
8876 vmx_set_rvi(max_irr);
8877 return;
8878 }
8879
Wei Wang4114c272014-11-05 10:53:43 +08008880 if (max_irr == -1)
8881 return;
8882
Wanpeng Li963fee12014-07-17 19:03:00 +08008883 /*
Wei Wang4114c272014-11-05 10:53:43 +08008884 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8885 * handles it.
8886 */
8887 if (nested_exit_on_intr(vcpu))
8888 return;
8889
8890 /*
8891 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008892 * is run without virtual interrupt delivery.
8893 */
8894 if (!kvm_event_needs_reinjection(vcpu) &&
8895 vmx_interrupt_allowed(vcpu)) {
8896 kvm_queue_interrupt(vcpu, max_irr, false);
8897 vmx_inject_irq(vcpu);
8898 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008899}
8900
Andrey Smetanin63086302015-11-10 15:36:32 +03008901static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008902{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008903 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008904 return;
8905
Yang Zhangc7c9c562013-01-25 10:18:51 +08008906 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8907 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8908 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8909 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8910}
8911
Avi Kivity51aa01d2010-07-20 14:31:20 +03008912static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008913{
Avi Kivity00eba012011-03-07 17:24:54 +02008914 u32 exit_intr_info;
8915
8916 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8917 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8918 return;
8919
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008920 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008921 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008922
8923 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008924 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008925 kvm_machine_check();
8926
Gleb Natapov20f65982009-05-11 13:35:55 +03008927 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008928 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008929 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008930 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008931 kvm_after_handle_nmi(&vmx->vcpu);
8932 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008933}
Gleb Natapov20f65982009-05-11 13:35:55 +03008934
Yang Zhanga547c6d2013-04-11 19:25:10 +08008935static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8936{
8937 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008938 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008939
8940 /*
8941 * If external interrupt exists, IF bit is set in rflags/eflags on the
8942 * interrupt stack frame, and interrupt will be enabled on a return
8943 * from interrupt handler.
8944 */
8945 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8946 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8947 unsigned int vector;
8948 unsigned long entry;
8949 gate_desc *desc;
8950 struct vcpu_vmx *vmx = to_vmx(vcpu);
8951#ifdef CONFIG_X86_64
8952 unsigned long tmp;
8953#endif
8954
8955 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8956 desc = (gate_desc *)vmx->host_idt_base + vector;
8957 entry = gate_offset(*desc);
8958 asm volatile(
8959#ifdef CONFIG_X86_64
8960 "mov %%" _ASM_SP ", %[sp]\n\t"
8961 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8962 "push $%c[ss]\n\t"
8963 "push %[sp]\n\t"
8964#endif
8965 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008966 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008967 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008968 :
8969#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008970 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008971#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008972 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008973 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008974 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008975 [ss]"i"(__KERNEL_DS),
8976 [cs]"i"(__KERNEL_CS)
8977 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008978 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008979}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008980STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008981
Tom Lendackyb9655922018-05-10 22:06:39 +02008982static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008983{
Tom Lendackyb9655922018-05-10 22:06:39 +02008984 switch (index) {
8985 case MSR_IA32_SMBASE:
8986 /*
8987 * We cannot do SMM unless we can run the guest in big
8988 * real mode.
8989 */
8990 return enable_unrestricted_guest || emulate_invalid_guest_state;
8991 case MSR_AMD64_VIRT_SPEC_CTRL:
8992 /* This is AMD only. */
8993 return false;
8994 default:
8995 return true;
8996 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008997}
8998
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008999static bool vmx_mpx_supported(void)
9000{
9001 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9002 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9003}
9004
Wanpeng Li55412b22014-12-02 19:21:30 +08009005static bool vmx_xsaves_supported(void)
9006{
9007 return vmcs_config.cpu_based_2nd_exec_ctrl &
9008 SECONDARY_EXEC_XSAVES;
9009}
9010
Avi Kivity51aa01d2010-07-20 14:31:20 +03009011static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9012{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009013 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009014 bool unblock_nmi;
9015 u8 vector;
9016 bool idtv_info_valid;
9017
9018 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009019
Avi Kivitycf393f72008-07-01 16:20:21 +03009020 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02009021 if (vmx->nmi_known_unmasked)
9022 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009023 /*
9024 * Can't use vmx->exit_intr_info since we're not sure what
9025 * the exit reason is.
9026 */
9027 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03009028 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9029 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9030 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009031 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03009032 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9033 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009034 * SDM 3: 23.2.2 (September 2008)
9035 * Bit 12 is undefined in any of the following cases:
9036 * If the VM exit sets the valid bit in the IDT-vectoring
9037 * information field.
9038 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03009039 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009040 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9041 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03009042 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9043 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02009044 else
9045 vmx->nmi_known_unmasked =
9046 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9047 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009048 } else if (unlikely(vmx->soft_vnmi_blocked))
9049 vmx->vnmi_blocked_time +=
9050 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009051}
9052
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009053static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009054 u32 idt_vectoring_info,
9055 int instr_len_field,
9056 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009057{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009058 u8 vector;
9059 int type;
9060 bool idtv_info_valid;
9061
9062 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009063
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009064 vcpu->arch.nmi_injected = false;
9065 kvm_clear_exception_queue(vcpu);
9066 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009067
9068 if (!idtv_info_valid)
9069 return;
9070
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009071 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009072
Avi Kivity668f6122008-07-02 09:28:55 +03009073 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9074 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009075
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009076 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009077 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009078 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009079 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009080 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009081 * Clear bit "block by NMI" before VM entry if a NMI
9082 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009083 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009084 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009085 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009086 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009087 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009088 /* fall through */
9089 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009090 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009091 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009092 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009093 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009094 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009095 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009096 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009097 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009098 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009099 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009100 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009101 break;
9102 default:
9103 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009104 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009105}
9106
Avi Kivity83422e12010-07-20 14:43:23 +03009107static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9108{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009109 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009110 VM_EXIT_INSTRUCTION_LEN,
9111 IDT_VECTORING_ERROR_CODE);
9112}
9113
Avi Kivityb463a6f2010-07-20 15:06:17 +03009114static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9115{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009116 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009117 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9118 VM_ENTRY_INSTRUCTION_LEN,
9119 VM_ENTRY_EXCEPTION_ERROR_CODE);
9120
9121 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9122}
9123
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009124static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9125{
9126 int i, nr_msrs;
9127 struct perf_guest_switch_msr *msrs;
9128
9129 msrs = perf_guest_get_msrs(&nr_msrs);
9130
9131 if (!msrs)
9132 return;
9133
9134 for (i = 0; i < nr_msrs; i++)
9135 if (msrs[i].host == msrs[i].guest)
9136 clear_atomic_switch_msr(vmx, msrs[i].msr);
9137 else
9138 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04009139 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009140}
9141
Yunhong Jiang64672c92016-06-13 14:19:59 -07009142void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9143{
9144 struct vcpu_vmx *vmx = to_vmx(vcpu);
9145 u64 tscl;
9146 u32 delta_tsc;
9147
9148 if (vmx->hv_deadline_tsc == -1)
9149 return;
9150
9151 tscl = rdtsc();
9152 if (vmx->hv_deadline_tsc > tscl)
9153 /* sure to be 32 bit only because checked on set_hv_timer */
9154 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9155 cpu_preemption_timer_multi);
9156 else
9157 delta_tsc = 0;
9158
9159 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9160}
9161
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009162static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009163{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009164 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009165 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009166
9167 /* Record the guest's net vcpu time for enforced NMI injections. */
9168 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9169 vmx->entry_time = ktime_get();
9170
9171 /* Don't enter VMX if guest state is invalid, let the exit handler
9172 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009173 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009174 return;
9175
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009176 if (vmx->ple_window_dirty) {
9177 vmx->ple_window_dirty = false;
9178 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9179 }
9180
Abel Gordon012f83c2013-04-18 14:39:25 +03009181 if (vmx->nested.sync_shadow_vmcs) {
9182 copy_vmcs12_to_shadow(vmx);
9183 vmx->nested.sync_shadow_vmcs = false;
9184 }
9185
Avi Kivity104f2262010-11-18 13:12:52 +02009186 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9187 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9188 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9189 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9190
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009191 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009192 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9193 vmcs_writel(HOST_CR4, cr4);
9194 vmx->host_state.vmcs_host_cr4 = cr4;
9195 }
9196
Avi Kivity104f2262010-11-18 13:12:52 +02009197 /* When single-stepping over STI and MOV SS, we must clear the
9198 * corresponding interruptibility bits in the guest state. Otherwise
9199 * vmentry fails as it then expects bit 14 (BS) in pending debug
9200 * exceptions being set, but that's not correct for the guest debugging
9201 * case. */
9202 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9203 vmx_set_interrupt_shadow(vcpu, 0);
9204
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009205 if (vmx->guest_pkru_valid)
9206 __write_pkru(vmx->guest_pkru);
9207
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009208 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009209 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009210
Yunhong Jiang64672c92016-06-13 14:19:59 -07009211 vmx_arm_hv_timer(vcpu);
9212
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009213 /*
9214 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9215 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9216 * is no need to worry about the conditional branch over the wrmsr
9217 * being speculatively taken.
9218 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009219 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009220
Nadav Har'Eld462b812011-05-24 15:26:10 +03009221 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009222
Nicolai Stange90bc3062018-07-21 22:35:28 +02009223 if (static_branch_unlikely(&vmx_l1d_should_flush))
9224 vmx_l1d_flush(vcpu);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009225
Avi Kivity104f2262010-11-18 13:12:52 +02009226 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009227 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009228 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9229 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9230 "push %%" _ASM_CX " \n\t"
9231 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009232 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009233 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009234 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009235 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009236 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009237 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9238 "mov %%cr2, %%" _ASM_DX " \n\t"
9239 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009240 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009241 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009242 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009243 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009244 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009245 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009246 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9247 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9248 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9249 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9250 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9251 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009252#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009253 "mov %c[r8](%0), %%r8 \n\t"
9254 "mov %c[r9](%0), %%r9 \n\t"
9255 "mov %c[r10](%0), %%r10 \n\t"
9256 "mov %c[r11](%0), %%r11 \n\t"
9257 "mov %c[r12](%0), %%r12 \n\t"
9258 "mov %c[r13](%0), %%r13 \n\t"
9259 "mov %c[r14](%0), %%r14 \n\t"
9260 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009261#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009262 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009263
Avi Kivity6aa8b732006-12-10 02:21:36 -08009264 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009265 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009266 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009267 "jmp 2f \n\t"
9268 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9269 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009270 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009271 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009272 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009273 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009274 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9275 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9276 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9277 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9278 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9279 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9280 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009281#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009282 "mov %%r8, %c[r8](%0) \n\t"
9283 "mov %%r9, %c[r9](%0) \n\t"
9284 "mov %%r10, %c[r10](%0) \n\t"
9285 "mov %%r11, %c[r11](%0) \n\t"
9286 "mov %%r12, %c[r12](%0) \n\t"
9287 "mov %%r13, %c[r13](%0) \n\t"
9288 "mov %%r14, %c[r14](%0) \n\t"
9289 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009290 "xor %%r8d, %%r8d \n\t"
9291 "xor %%r9d, %%r9d \n\t"
9292 "xor %%r10d, %%r10d \n\t"
9293 "xor %%r11d, %%r11d \n\t"
9294 "xor %%r12d, %%r12d \n\t"
9295 "xor %%r13d, %%r13d \n\t"
9296 "xor %%r14d, %%r14d \n\t"
9297 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009298#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009299 "mov %%cr2, %%" _ASM_AX " \n\t"
9300 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009301
Jim Mattson491c0ca2018-01-03 14:31:38 -08009302 "xor %%eax, %%eax \n\t"
9303 "xor %%ebx, %%ebx \n\t"
9304 "xor %%esi, %%esi \n\t"
9305 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009306 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009307 ".pushsection .rodata \n\t"
9308 ".global vmx_return \n\t"
9309 "vmx_return: " _ASM_PTR " 2b \n\t"
9310 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009311 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009312 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009313 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009314 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009315 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9316 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9317 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9318 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9319 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9320 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9321 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009322#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009323 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9324 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9325 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9326 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9327 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9328 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9329 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9330 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009331#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009332 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9333 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009334 : "cc", "memory"
9335#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009336 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009337 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009338#else
9339 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009340#endif
9341 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009342
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009343 /*
9344 * We do not use IBRS in the kernel. If this vCPU has used the
9345 * SPEC_CTRL MSR it may have left it on; save the value and
9346 * turn it off. This is much more efficient than blindly adding
9347 * it to the atomic save/restore list. Especially as the former
9348 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9349 *
9350 * For non-nested case:
9351 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9352 * save it.
9353 *
9354 * For nested case:
9355 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9356 * save it.
9357 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009358 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009359 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009360
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009361 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009362
David Woodhousec1ddd992018-01-12 11:11:27 +00009363 /* Eliminate branch target predictions from guest mode */
9364 vmexit_fill_RSB();
9365
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009366 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9367 if (debugctlmsr)
9368 update_debugctlmsr(debugctlmsr);
9369
Avi Kivityaa67f602012-08-01 16:48:03 +03009370#ifndef CONFIG_X86_64
9371 /*
9372 * The sysexit path does not restore ds/es, so we must set them to
9373 * a reasonable value ourselves.
9374 *
9375 * We can't defer this to vmx_load_host_state() since that function
9376 * may be executed in interrupt context, which saves and restore segments
9377 * around it, nullifying its effect.
9378 */
9379 loadsegment(ds, __USER_DS);
9380 loadsegment(es, __USER_DS);
9381#endif
9382
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009383 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009384 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009385 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009386 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009387 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009388 vcpu->arch.regs_dirty = 0;
9389
Avi Kivity1155f762007-11-22 11:30:47 +02009390 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9391
Nadav Har'Eld462b812011-05-24 15:26:10 +03009392 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009393
Avi Kivity51aa01d2010-07-20 14:31:20 +03009394 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009395
Gleb Natapove0b890d2013-09-25 12:51:33 +03009396 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009397 * eager fpu is enabled if PKEY is supported and CR4 is switched
9398 * back on host, so it is safe to read guest PKRU from current
9399 * XSAVE.
9400 */
9401 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9402 vmx->guest_pkru = __read_pkru();
9403 if (vmx->guest_pkru != vmx->host_pkru) {
9404 vmx->guest_pkru_valid = true;
9405 __write_pkru(vmx->host_pkru);
9406 } else
9407 vmx->guest_pkru_valid = false;
9408 }
9409
9410 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009411 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9412 * we did not inject a still-pending event to L1 now because of
9413 * nested_run_pending, we need to re-enable this bit.
9414 */
9415 if (vmx->nested.nested_run_pending)
9416 kvm_make_request(KVM_REQ_EVENT, vcpu);
9417
9418 vmx->nested.nested_run_pending = 0;
9419
Avi Kivity51aa01d2010-07-20 14:31:20 +03009420 vmx_complete_atomic_exit(vmx);
9421 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009422 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009423}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009424STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009425
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009426static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9427{
9428 struct vcpu_vmx *vmx = to_vmx(vcpu);
9429 int cpu;
9430
9431 if (vmx->loaded_vmcs == &vmx->vmcs01)
9432 return;
9433
9434 cpu = get_cpu();
9435 vmx->loaded_vmcs = &vmx->vmcs01;
9436 vmx_vcpu_put(vcpu);
9437 vmx_vcpu_load(vcpu, cpu);
9438 vcpu->cpu = cpu;
9439 put_cpu();
9440}
9441
Jim Mattson2f1fe812016-07-08 15:36:06 -07009442/*
9443 * Ensure that the current vmcs of the logical processor is the
9444 * vmcs01 of the vcpu before calling free_nested().
9445 */
9446static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9447{
9448 struct vcpu_vmx *vmx = to_vmx(vcpu);
9449 int r;
9450
9451 r = vcpu_load(vcpu);
9452 BUG_ON(r);
9453 vmx_load_vmcs01(vcpu);
9454 free_nested(vmx);
9455 vcpu_put(vcpu);
9456}
9457
Avi Kivity6aa8b732006-12-10 02:21:36 -08009458static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9459{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009460 struct vcpu_vmx *vmx = to_vmx(vcpu);
9461
Kai Huang843e4332015-01-28 10:54:28 +08009462 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009463 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009464 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009465 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009466 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009467 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009468 kfree(vmx->guest_msrs);
9469 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009470 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009471}
9472
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009473static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009474{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009475 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009476 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009477 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009478 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009479
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009480 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009481 return ERR_PTR(-ENOMEM);
9482
Wanpeng Li991e7a02015-09-16 17:30:05 +08009483 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009484
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009485 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9486 if (err)
9487 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009488
Peter Feiner4e595162016-07-07 14:49:58 -07009489 err = -ENOMEM;
9490
9491 /*
9492 * If PML is turned on, failure on enabling PML just results in failure
9493 * of creating the vcpu, therefore we can simplify PML logic (by
9494 * avoiding dealing with cases, such as enabling PML partially on vcpus
9495 * for the guest, etc.
9496 */
9497 if (enable_pml) {
9498 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9499 if (!vmx->pml_pg)
9500 goto uninit_vcpu;
9501 }
9502
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009503 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009504 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9505 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009506
Peter Feiner4e595162016-07-07 14:49:58 -07009507 if (!vmx->guest_msrs)
9508 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009509
Nadav Har'Eld462b812011-05-24 15:26:10 +03009510 if (!vmm_exclusive)
9511 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009512 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009513 if (!vmm_exclusive)
9514 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009515 if (err < 0)
9516 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009517
Paolo Bonzini6236b782018-01-16 16:51:18 +01009518 msr_bitmap = vmx->vmcs01.msr_bitmap;
9519 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9520 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9521 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9522 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9523 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9524 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9525 vmx->msr_bitmap_mode = 0;
9526
Paolo Bonziniff546f92018-01-11 12:16:15 +01009527 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009528 cpu = get_cpu();
9529 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009530 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009531 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009532 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009533 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009534 if (err)
9535 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009536 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009537 err = alloc_apic_access_page(kvm);
9538 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009539 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009540 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009541
Sheng Yangb927a3c2009-07-21 10:42:48 +08009542 if (enable_ept) {
9543 if (!kvm->arch.ept_identity_map_addr)
9544 kvm->arch.ept_identity_map_addr =
9545 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009546 err = init_rmode_identity_map(kvm);
9547 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009548 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009549 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009550
Roman Kagan020a90f2018-07-19 21:59:07 +03009551 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009552 nested_vmx_setup_ctls_msrs(vmx);
9553
Wincy Van705699a2015-02-03 23:58:17 +08009554 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009555 vmx->nested.current_vmptr = -1ull;
9556 vmx->nested.current_vmcs12 = NULL;
9557
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009558 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9559
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009560 /*
9561 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9562 * or POSTED_INTR_WAKEUP_VECTOR.
9563 */
9564 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9565 vmx->pi_desc.sn = 1;
9566
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009567 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009568
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009569free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009570 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009571free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009572 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009573free_pml:
9574 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009575uninit_vcpu:
9576 kvm_vcpu_uninit(&vmx->vcpu);
9577free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009578 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009579 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009580 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009581}
9582
Jiri Kosina2decbf52018-07-13 16:23:25 +02009583#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
9584#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009585
9586static int vmx_vm_init(struct kvm *kvm)
9587{
Jiri Kosina2decbf52018-07-13 16:23:25 +02009588 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
9589 switch (l1tf_mitigation) {
9590 case L1TF_MITIGATION_OFF:
9591 case L1TF_MITIGATION_FLUSH_NOWARN:
9592 /* 'I explicitly don't care' is set */
9593 break;
9594 case L1TF_MITIGATION_FLUSH:
9595 case L1TF_MITIGATION_FLUSH_NOSMT:
9596 case L1TF_MITIGATION_FULL:
9597 /*
9598 * Warn upon starting the first VM in a potentially
9599 * insecure environment.
9600 */
9601 if (cpu_smt_control == CPU_SMT_ENABLED)
9602 pr_warn_once(L1TF_MSG_SMT);
9603 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
9604 pr_warn_once(L1TF_MSG_L1D);
9605 break;
9606 case L1TF_MITIGATION_FULL_FORCE:
9607 /* Flush is enforced */
9608 break;
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009609 }
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009610 }
9611 return 0;
9612}
9613
Yang, Sheng002c7f72007-07-31 14:23:01 +03009614static void __init vmx_check_processor_compat(void *rtn)
9615{
9616 struct vmcs_config vmcs_conf;
9617
9618 *(int *)rtn = 0;
9619 if (setup_vmcs_config(&vmcs_conf) < 0)
9620 *(int *)rtn = -EIO;
9621 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9622 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9623 smp_processor_id());
9624 *(int *)rtn = -EIO;
9625 }
9626}
9627
Sheng Yang67253af2008-04-25 10:20:22 +08009628static int get_ept_level(void)
9629{
9630 return VMX_EPT_DEFAULT_GAW + 1;
9631}
9632
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009633static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009634{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009635 u8 cache;
9636 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009637
Sheng Yang522c68c2009-04-27 20:35:43 +08009638 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009639 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009640 * 2. EPT with VT-d:
9641 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009642 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009643 * b. VT-d with snooping control feature: snooping control feature of
9644 * VT-d engine can guarantee the cache correctness. Just set it
9645 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009646 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009647 * consistent with host MTRR
9648 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009649 if (is_mmio) {
9650 cache = MTRR_TYPE_UNCACHABLE;
9651 goto exit;
9652 }
9653
9654 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009655 ipat = VMX_EPT_IPAT_BIT;
9656 cache = MTRR_TYPE_WRBACK;
9657 goto exit;
9658 }
9659
9660 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9661 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009662 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009663 cache = MTRR_TYPE_WRBACK;
9664 else
9665 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009666 goto exit;
9667 }
9668
Xiao Guangrongff536042015-06-15 16:55:22 +08009669 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009670
9671exit:
9672 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009673}
9674
Sheng Yang17cc3932010-01-05 19:02:27 +08009675static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009676{
Sheng Yang878403b2010-01-05 19:02:29 +08009677 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9678 return PT_DIRECTORY_LEVEL;
9679 else
9680 /* For shadow and EPT supported 1GB page */
9681 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009682}
9683
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009684static void vmcs_set_secondary_exec_control(u32 new_ctl)
9685{
9686 /*
9687 * These bits in the secondary execution controls field
9688 * are dynamic, the others are mostly based on the hypervisor
9689 * architecture and the guest's CPUID. Do not touch the
9690 * dynamic bits.
9691 */
9692 u32 mask =
9693 SECONDARY_EXEC_SHADOW_VMCS |
9694 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9695 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9696
9697 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9698
9699 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9700 (new_ctl & ~mask) | (cur_ctl & mask));
9701}
9702
Sheng Yang0e851882009-12-18 16:48:46 +08009703static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9704{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009705 struct kvm_cpuid_entry2 *best;
9706 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009707 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009708
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009709 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009710 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9711 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009712 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009713
Paolo Bonzini8b972652015-09-15 17:34:42 +02009714 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009715 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009716 vmx->nested.nested_vmx_secondary_ctls_high |=
9717 SECONDARY_EXEC_RDTSCP;
9718 else
9719 vmx->nested.nested_vmx_secondary_ctls_high &=
9720 ~SECONDARY_EXEC_RDTSCP;
9721 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009722 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009723
Mao, Junjiead756a12012-07-02 01:18:48 +00009724 /* Exposing INVPCID only when PCID is exposed */
9725 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9726 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009727 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9728 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009729 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009730
Mao, Junjiead756a12012-07-02 01:18:48 +00009731 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009732 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009733 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009734
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009735 if (cpu_has_secondary_exec_ctrls())
9736 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009737
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009738 if (nested_vmx_allowed(vcpu))
9739 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9740 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9741 else
9742 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9743 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009744}
9745
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009746static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9747{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009748 if (func == 1 && nested)
9749 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009750}
9751
Yang Zhang25d92082013-08-06 12:00:32 +03009752static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9753 struct x86_exception *fault)
9754{
Jan Kiszka533558b2014-01-04 18:47:20 +01009755 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9756 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009757
9758 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009759 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009760 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009761 exit_reason = EXIT_REASON_EPT_VIOLATION;
9762 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009763 vmcs12->guest_physical_address = fault->address;
9764}
9765
Nadav Har'El155a97a2013-08-05 11:07:16 +03009766/* Callbacks for nested_ept_init_mmu_context: */
9767
9768static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9769{
9770 /* return the page table to be shadowed - in our case, EPT12 */
9771 return get_vmcs12(vcpu)->ept_pointer;
9772}
9773
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009774static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009775{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009776 WARN_ON(mmu_is_nested(vcpu));
9777 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009778 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9779 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009780 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9781 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9782 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9783
9784 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009785}
9786
9787static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9788{
9789 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9790}
9791
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009792static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9793 u16 error_code)
9794{
9795 bool inequality, bit;
9796
9797 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9798 inequality =
9799 (error_code & vmcs12->page_fault_error_code_mask) !=
9800 vmcs12->page_fault_error_code_match;
9801 return inequality ^ bit;
9802}
9803
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009804static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9805 struct x86_exception *fault)
9806{
9807 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9808
9809 WARN_ON(!is_guest_mode(vcpu));
9810
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009811 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009812 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9813 vmcs_read32(VM_EXIT_INTR_INFO),
9814 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009815 else
9816 kvm_inject_page_fault(vcpu, fault);
9817}
9818
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009819static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9820 struct vmcs12 *vmcs12)
9821{
9822 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009823 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009824
9825 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009826 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9827 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009828 return false;
9829
9830 /*
9831 * Translate L1 physical address to host physical
9832 * address for vmcs02. Keep the page pinned, so this
9833 * physical address remains valid. We keep a reference
9834 * to it so we can release it later.
9835 */
9836 if (vmx->nested.apic_access_page) /* shouldn't happen */
9837 nested_release_page(vmx->nested.apic_access_page);
9838 vmx->nested.apic_access_page =
9839 nested_get_page(vcpu, vmcs12->apic_access_addr);
9840 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009841
9842 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009843 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9844 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009845 return false;
9846
9847 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9848 nested_release_page(vmx->nested.virtual_apic_page);
9849 vmx->nested.virtual_apic_page =
9850 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9851
9852 /*
9853 * Failing the vm entry is _not_ what the processor does
9854 * but it's basically the only possibility we have.
9855 * We could still enter the guest if CR8 load exits are
9856 * enabled, CR8 store exits are enabled, and virtualize APIC
9857 * access is disabled; in this case the processor would never
9858 * use the TPR shadow and we could simply clear the bit from
9859 * the execution control. But such a configuration is useless,
9860 * so let's keep the code simple.
9861 */
9862 if (!vmx->nested.virtual_apic_page)
9863 return false;
9864 }
9865
Wincy Van705699a2015-02-03 23:58:17 +08009866 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009867 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9868 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009869 return false;
9870
9871 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9872 kunmap(vmx->nested.pi_desc_page);
9873 nested_release_page(vmx->nested.pi_desc_page);
9874 }
9875 vmx->nested.pi_desc_page =
9876 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9877 if (!vmx->nested.pi_desc_page)
9878 return false;
9879
9880 vmx->nested.pi_desc =
9881 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9882 if (!vmx->nested.pi_desc) {
9883 nested_release_page_clean(vmx->nested.pi_desc_page);
9884 return false;
9885 }
9886 vmx->nested.pi_desc =
9887 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9888 (unsigned long)(vmcs12->posted_intr_desc_addr &
9889 (PAGE_SIZE - 1)));
9890 }
9891
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009892 return true;
9893}
9894
Jan Kiszkaf4124502014-03-07 20:03:13 +01009895static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9896{
9897 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9898 struct vcpu_vmx *vmx = to_vmx(vcpu);
9899
9900 if (vcpu->arch.virtual_tsc_khz == 0)
9901 return;
9902
9903 /* Make sure short timeouts reliably trigger an immediate vmexit.
9904 * hrtimer_start does not guarantee this. */
9905 if (preemption_timeout <= 1) {
9906 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9907 return;
9908 }
9909
9910 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9911 preemption_timeout *= 1000000;
9912 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9913 hrtimer_start(&vmx->nested.preemption_timer,
9914 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9915}
9916
Wincy Van3af18d92015-02-03 23:49:31 +08009917static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9918 struct vmcs12 *vmcs12)
9919{
9920 int maxphyaddr;
9921 u64 addr;
9922
9923 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9924 return 0;
9925
9926 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9927 WARN_ON(1);
9928 return -EINVAL;
9929 }
9930 maxphyaddr = cpuid_maxphyaddr(vcpu);
9931
9932 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9933 ((addr + PAGE_SIZE) >> maxphyaddr))
9934 return -EINVAL;
9935
9936 return 0;
9937}
9938
9939/*
9940 * Merge L0's and L1's MSR bitmap, return false to indicate that
9941 * we do not use the hardware.
9942 */
9943static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9944 struct vmcs12 *vmcs12)
9945{
Wincy Van82f0dd42015-02-03 23:57:18 +08009946 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009947 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009948 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009949 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009950 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009951 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009952 *
9953 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9954 * ensures that we do not accidentally generate an L02 MSR bitmap
9955 * from the L12 MSR bitmap that is too permissive.
9956 * 2. That L1 or L2s have actually used the MSR. This avoids
9957 * unnecessarily merging of the bitmap if the MSR is unused. This
9958 * works properly because we only update the L01 MSR bitmap lazily.
9959 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9960 * updated to reflect this when L1 (or its L2s) actually write to
9961 * the MSR.
9962 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009963 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9964 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009965
Ashok Raj70131292018-02-01 22:59:43 +01009966 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009967 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009968 return false;
9969
9970 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009971 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009972 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009973 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009974
Radim Krčmářd048c092016-08-08 20:16:22 +02009975 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9976
Wincy Vanf2b93282015-02-03 23:56:03 +08009977 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009978 if (nested_cpu_has_apic_reg_virt(vmcs12))
9979 for (msr = 0x800; msr <= 0x8ff; msr++)
9980 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009981 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009982 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009983
9984 nested_vmx_disable_intercept_for_msr(
9985 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009986 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9987 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009988
Wincy Van608406e2015-02-03 23:57:51 +08009989 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009990 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009991 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009992 APIC_BASE_MSR + (APIC_EOI >> 4),
9993 MSR_TYPE_W);
9994 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009995 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009996 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9997 MSR_TYPE_W);
9998 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009999 }
Ashok Raj70131292018-02-01 22:59:43 +010010000
KarimAllah Ahmede5a83412018-02-01 22:59:45 +010010001 if (spec_ctrl)
10002 nested_vmx_disable_intercept_for_msr(
10003 msr_bitmap_l1, msr_bitmap_l0,
10004 MSR_IA32_SPEC_CTRL,
10005 MSR_TYPE_R | MSR_TYPE_W);
10006
Ashok Raj70131292018-02-01 22:59:43 +010010007 if (pred_cmd)
10008 nested_vmx_disable_intercept_for_msr(
10009 msr_bitmap_l1, msr_bitmap_l0,
10010 MSR_IA32_PRED_CMD,
10011 MSR_TYPE_W);
10012
Wincy Vanf2b93282015-02-03 23:56:03 +080010013 kunmap(page);
10014 nested_release_page_clean(page);
10015
10016 return true;
10017}
10018
10019static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10020 struct vmcs12 *vmcs12)
10021{
Wincy Van82f0dd42015-02-03 23:57:18 +080010022 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010023 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010024 !nested_cpu_has_vid(vmcs12) &&
10025 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010026 return 0;
10027
10028 /*
10029 * If virtualize x2apic mode is enabled,
10030 * virtualize apic access must be disabled.
10031 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010032 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10033 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010034 return -EINVAL;
10035
Wincy Van608406e2015-02-03 23:57:51 +080010036 /*
10037 * If virtual interrupt delivery is enabled,
10038 * we must exit on external interrupts.
10039 */
10040 if (nested_cpu_has_vid(vmcs12) &&
10041 !nested_exit_on_intr(vcpu))
10042 return -EINVAL;
10043
Wincy Van705699a2015-02-03 23:58:17 +080010044 /*
10045 * bits 15:8 should be zero in posted_intr_nv,
10046 * the descriptor address has been already checked
10047 * in nested_get_vmcs12_pages.
10048 */
10049 if (nested_cpu_has_posted_intr(vmcs12) &&
10050 (!nested_cpu_has_vid(vmcs12) ||
10051 !nested_exit_intr_ack_set(vcpu) ||
10052 vmcs12->posted_intr_nv & 0xff00))
10053 return -EINVAL;
10054
Wincy Vanf2b93282015-02-03 23:56:03 +080010055 /* tpr shadow is needed by all apicv features. */
10056 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10057 return -EINVAL;
10058
10059 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010060}
10061
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010062static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10063 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010064 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010065{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010066 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010067 u64 count, addr;
10068
10069 if (vmcs12_read_any(vcpu, count_field, &count) ||
10070 vmcs12_read_any(vcpu, addr_field, &addr)) {
10071 WARN_ON(1);
10072 return -EINVAL;
10073 }
10074 if (count == 0)
10075 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010076 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010077 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10078 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010079 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010080 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10081 addr_field, maxphyaddr, count, addr);
10082 return -EINVAL;
10083 }
10084 return 0;
10085}
10086
10087static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10088 struct vmcs12 *vmcs12)
10089{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010090 if (vmcs12->vm_exit_msr_load_count == 0 &&
10091 vmcs12->vm_exit_msr_store_count == 0 &&
10092 vmcs12->vm_entry_msr_load_count == 0)
10093 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010094 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010095 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010096 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010097 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010098 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010099 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010100 return -EINVAL;
10101 return 0;
10102}
10103
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010104static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10105 struct vmx_msr_entry *e)
10106{
10107 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010108 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010109 return -EINVAL;
10110 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10111 e->index == MSR_IA32_UCODE_REV)
10112 return -EINVAL;
10113 if (e->reserved != 0)
10114 return -EINVAL;
10115 return 0;
10116}
10117
10118static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10119 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010120{
10121 if (e->index == MSR_FS_BASE ||
10122 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010123 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10124 nested_vmx_msr_check_common(vcpu, e))
10125 return -EINVAL;
10126 return 0;
10127}
10128
10129static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10130 struct vmx_msr_entry *e)
10131{
10132 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10133 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010134 return -EINVAL;
10135 return 0;
10136}
10137
10138/*
10139 * Load guest's/host's msr at nested entry/exit.
10140 * return 0 for success, entry index for failure.
10141 */
10142static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10143{
10144 u32 i;
10145 struct vmx_msr_entry e;
10146 struct msr_data msr;
10147
10148 msr.host_initiated = false;
10149 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010150 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10151 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010152 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010153 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10154 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010155 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010156 }
10157 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010158 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010159 "%s check failed (%u, 0x%x, 0x%x)\n",
10160 __func__, i, e.index, e.reserved);
10161 goto fail;
10162 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010163 msr.index = e.index;
10164 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010165 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010166 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010167 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10168 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010169 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010170 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010171 }
10172 return 0;
10173fail:
10174 return i + 1;
10175}
10176
10177static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10178{
10179 u32 i;
10180 struct vmx_msr_entry e;
10181
10182 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010183 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010184 if (kvm_vcpu_read_guest(vcpu,
10185 gpa + i * sizeof(e),
10186 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010187 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010188 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10189 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010190 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010191 }
10192 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010193 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010194 "%s check failed (%u, 0x%x, 0x%x)\n",
10195 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010196 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010197 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010198 msr_info.host_initiated = false;
10199 msr_info.index = e.index;
10200 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010201 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010202 "%s cannot read MSR (%u, 0x%x)\n",
10203 __func__, i, e.index);
10204 return -EINVAL;
10205 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010206 if (kvm_vcpu_write_guest(vcpu,
10207 gpa + i * sizeof(e) +
10208 offsetof(struct vmx_msr_entry, value),
10209 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010210 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010211 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010212 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010213 return -EINVAL;
10214 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010215 }
10216 return 0;
10217}
10218
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010219/*
10220 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10221 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010222 * 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 +030010223 * guest in a way that will both be appropriate to L1's requests, and our
10224 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10225 * function also has additional necessary side-effects, like setting various
10226 * vcpu->arch fields.
10227 */
10228static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10229{
10230 struct vcpu_vmx *vmx = to_vmx(vcpu);
10231 u32 exec_control;
10232
10233 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10234 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10235 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10236 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10237 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10238 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10239 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10240 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10241 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10242 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10243 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10244 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10245 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10246 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10247 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10248 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10249 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10250 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10251 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10252 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10253 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10254 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10255 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10256 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10257 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10258 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10259 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10260 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10261 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10262 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10263 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10264 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10265 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10266 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10267 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10268 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10269
Jan Kiszka2996fca2014-06-16 13:59:43 +020010270 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10271 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10272 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10273 } else {
10274 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10275 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10276 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010277 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10278 vmcs12->vm_entry_intr_info_field);
10279 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10280 vmcs12->vm_entry_exception_error_code);
10281 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10282 vmcs12->vm_entry_instruction_len);
10283 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10284 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010285 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010286 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010287 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10288 vmcs12->guest_pending_dbg_exceptions);
10289 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10290 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10291
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010292 if (nested_cpu_has_xsaves(vmcs12))
10293 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010294 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10295
Jan Kiszkaf4124502014-03-07 20:03:13 +010010296 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010297
Paolo Bonzini93140062016-07-06 13:23:51 +020010298 /* Preemption timer setting is only taken from vmcs01. */
10299 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10300 exec_control |= vmcs_config.pin_based_exec_ctrl;
10301 if (vmx->hv_deadline_tsc == -1)
10302 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10303
10304 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010305 if (nested_cpu_has_posted_intr(vmcs12)) {
10306 /*
10307 * Note that we use L0's vector here and in
10308 * vmx_deliver_nested_posted_interrupt.
10309 */
10310 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10311 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010312 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010313 vmcs_write64(POSTED_INTR_DESC_ADDR,
10314 page_to_phys(vmx->nested.pi_desc_page) +
10315 (unsigned long)(vmcs12->posted_intr_desc_addr &
10316 (PAGE_SIZE - 1)));
10317 } else
10318 exec_control &= ~PIN_BASED_POSTED_INTR;
10319
Jan Kiszkaf4124502014-03-07 20:03:13 +010010320 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010321
Jan Kiszkaf4124502014-03-07 20:03:13 +010010322 vmx->nested.preemption_timer_expired = false;
10323 if (nested_cpu_has_preemption_timer(vmcs12))
10324 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010325
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010326 /*
10327 * Whether page-faults are trapped is determined by a combination of
10328 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10329 * If enable_ept, L0 doesn't care about page faults and we should
10330 * set all of these to L1's desires. However, if !enable_ept, L0 does
10331 * care about (at least some) page faults, and because it is not easy
10332 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10333 * to exit on each and every L2 page fault. This is done by setting
10334 * MASK=MATCH=0 and (see below) EB.PF=1.
10335 * Note that below we don't need special code to set EB.PF beyond the
10336 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10337 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10338 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10339 *
10340 * A problem with this approach (when !enable_ept) is that L1 may be
10341 * injected with more page faults than it asked for. This could have
10342 * caused problems, but in practice existing hypervisors don't care.
10343 * To fix this, we will need to emulate the PFEC checking (on the L1
10344 * page tables), using walk_addr(), when injecting PFs to L1.
10345 */
10346 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10347 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10348 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10349 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10350
10351 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010352 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010353
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010354 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010355 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010356 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010357 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010358 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010359 if (nested_cpu_has(vmcs12,
10360 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10361 exec_control |= vmcs12->secondary_vm_exec_control;
10362
10363 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10364 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010365 * If translation failed, no matter: This feature asks
10366 * to exit when accessing the given address, and if it
10367 * can never be accessed, this feature won't do
10368 * anything anyway.
10369 */
10370 if (!vmx->nested.apic_access_page)
10371 exec_control &=
10372 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10373 else
10374 vmcs_write64(APIC_ACCESS_ADDR,
10375 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010376 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010377 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010378 exec_control |=
10379 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010380 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010381 }
10382
Wincy Van608406e2015-02-03 23:57:51 +080010383 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10384 vmcs_write64(EOI_EXIT_BITMAP0,
10385 vmcs12->eoi_exit_bitmap0);
10386 vmcs_write64(EOI_EXIT_BITMAP1,
10387 vmcs12->eoi_exit_bitmap1);
10388 vmcs_write64(EOI_EXIT_BITMAP2,
10389 vmcs12->eoi_exit_bitmap2);
10390 vmcs_write64(EOI_EXIT_BITMAP3,
10391 vmcs12->eoi_exit_bitmap3);
10392 vmcs_write16(GUEST_INTR_STATUS,
10393 vmcs12->guest_intr_status);
10394 }
10395
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010396 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10397 }
10398
10399
10400 /*
10401 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10402 * Some constant fields are set here by vmx_set_constant_host_state().
10403 * Other fields are different per CPU, and will be set later when
10404 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10405 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010406 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010407
10408 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010409 * Set the MSR load/store lists to match L0's settings.
10410 */
10411 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010412 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10413 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10414 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10415 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010416
10417 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010418 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10419 * entry, but only if the current (host) sp changed from the value
10420 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10421 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10422 * here we just force the write to happen on entry.
10423 */
10424 vmx->host_rsp = 0;
10425
10426 exec_control = vmx_exec_control(vmx); /* L0's desires */
10427 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10428 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10429 exec_control &= ~CPU_BASED_TPR_SHADOW;
10430 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010431
10432 if (exec_control & CPU_BASED_TPR_SHADOW) {
10433 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10434 page_to_phys(vmx->nested.virtual_apic_page));
10435 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010436 } else {
10437#ifdef CONFIG_X86_64
10438 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10439 CPU_BASED_CR8_STORE_EXITING;
10440#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010441 }
10442
Wincy Van3af18d92015-02-03 23:49:31 +080010443 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010444 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10445 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10446 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10447 else
Wincy Van3af18d92015-02-03 23:49:31 +080010448 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10449
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010450 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010451 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010452 * Rather, exit every time.
10453 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010454 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10455 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10456
10457 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10458
10459 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10460 * bitwise-or of what L1 wants to trap for L2, and what we want to
10461 * trap. Note that CR0.TS also needs updating - we do this later.
10462 */
10463 update_exception_bitmap(vcpu);
10464 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10465 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10466
Nadav Har'El8049d652013-08-05 11:07:06 +030010467 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10468 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10469 * bits are further modified by vmx_set_efer() below.
10470 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010471 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010472
10473 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10474 * emulated by vmx_set_efer(), below.
10475 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010476 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010477 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10478 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010479 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10480
Jan Kiszka44811c02013-08-04 17:17:27 +020010481 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010482 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010483 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10484 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010485 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10486
10487
10488 set_cr4_guest_host_mask(vmx);
10489
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010490 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10491 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10492
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010493 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10494 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010495 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010496 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010497 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010498 if (kvm_has_tsc_control)
10499 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010500
Paolo Bonzini6236b782018-01-16 16:51:18 +010010501 if (cpu_has_vmx_msr_bitmap())
10502 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10503
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010504 if (enable_vpid) {
10505 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010506 * There is no direct mapping between vpid02 and vpid12, the
10507 * vpid02 is per-vCPU for L0 and reused while the value of
10508 * vpid12 is changed w/ one invvpid during nested vmentry.
10509 * The vpid12 is allocated by L1 for L2, so it will not
10510 * influence global bitmap(for vpid01 and vpid02 allocation)
10511 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010512 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010513 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10514 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10515 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10516 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10517 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10518 }
10519 } else {
10520 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10521 vmx_flush_tlb(vcpu);
10522 }
10523
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010524 }
10525
Ladi Prosek560a9792017-04-04 14:18:53 +020010526 if (enable_pml) {
10527 /*
10528 * Conceptually we want to copy the PML address and index from
10529 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10530 * since we always flush the log on each vmexit, this happens
10531 * to be equivalent to simply resetting the fields in vmcs02.
10532 */
10533 ASSERT(vmx->pml_pg);
10534 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10535 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10536 }
10537
Nadav Har'El155a97a2013-08-05 11:07:16 +030010538 if (nested_cpu_has_ept(vmcs12)) {
10539 kvm_mmu_unload(vcpu);
10540 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010541 } else if (nested_cpu_has2(vmcs12,
10542 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10543 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010544 }
10545
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010546 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10547 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010548 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010549 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10550 else
10551 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10552 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10553 vmx_set_efer(vcpu, vcpu->arch.efer);
10554
10555 /*
10556 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10557 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10558 * The CR0_READ_SHADOW is what L2 should have expected to read given
10559 * the specifications by L1; It's not enough to take
10560 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10561 * have more bits than L1 expected.
10562 */
10563 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10564 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10565
10566 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10567 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10568
10569 /* shadow page tables on either EPT or shadow page tables */
10570 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10571 kvm_mmu_reset_context(vcpu);
10572
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010573 if (!enable_ept)
10574 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10575
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010576 /*
10577 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10578 */
10579 if (enable_ept) {
10580 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10581 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10582 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10583 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10584 }
10585
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010586 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10587 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10588}
10589
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010590/*
10591 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10592 * for running an L2 nested guest.
10593 */
10594static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10595{
10596 struct vmcs12 *vmcs12;
10597 struct vcpu_vmx *vmx = to_vmx(vcpu);
10598 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010599 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010600 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010601
10602 if (!nested_vmx_check_permission(vcpu) ||
10603 !nested_vmx_check_vmcs12(vcpu))
10604 return 1;
10605
10606 skip_emulated_instruction(vcpu);
10607 vmcs12 = get_vmcs12(vcpu);
10608
Abel Gordon012f83c2013-04-18 14:39:25 +030010609 if (enable_shadow_vmcs)
10610 copy_shadow_to_vmcs12(vmx);
10611
Nadav Har'El7c177932011-05-25 23:12:04 +030010612 /*
10613 * The nested entry process starts with enforcing various prerequisites
10614 * on vmcs12 as required by the Intel SDM, and act appropriately when
10615 * they fail: As the SDM explains, some conditions should cause the
10616 * instruction to fail, while others will cause the instruction to seem
10617 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10618 * To speed up the normal (success) code path, we should avoid checking
10619 * for misconfigurations which will anyway be caught by the processor
10620 * when using the merged vmcs02.
10621 */
10622 if (vmcs12->launch_state == launch) {
10623 nested_vmx_failValid(vcpu,
10624 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10625 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10626 return 1;
10627 }
10628
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010629 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10630 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010631 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10632 return 1;
10633 }
10634
Wincy Van3af18d92015-02-03 23:49:31 +080010635 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010636 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10637 return 1;
10638 }
10639
Wincy Van3af18d92015-02-03 23:49:31 +080010640 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010641 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10642 return 1;
10643 }
10644
Wincy Vanf2b93282015-02-03 23:56:03 +080010645 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10646 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10647 return 1;
10648 }
10649
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010650 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10651 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10652 return 1;
10653 }
10654
Nadav Har'El7c177932011-05-25 23:12:04 +030010655 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010656 vmx->nested.nested_vmx_true_procbased_ctls_low,
10657 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010658 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010659 vmx->nested.nested_vmx_secondary_ctls_low,
10660 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010661 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010662 vmx->nested.nested_vmx_pinbased_ctls_low,
10663 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010664 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010665 vmx->nested.nested_vmx_true_exit_ctls_low,
10666 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010667 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010668 vmx->nested.nested_vmx_true_entry_ctls_low,
10669 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010670 {
10671 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10672 return 1;
10673 }
10674
10675 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10676 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10677 nested_vmx_failValid(vcpu,
10678 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10679 return 1;
10680 }
10681
Wincy Vanb9c237b2015-02-03 23:56:30 +080010682 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010683 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10684 nested_vmx_entry_failure(vcpu, vmcs12,
10685 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10686 return 1;
10687 }
10688 if (vmcs12->vmcs_link_pointer != -1ull) {
10689 nested_vmx_entry_failure(vcpu, vmcs12,
10690 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10691 return 1;
10692 }
10693
10694 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010695 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010696 * are performed on the field for the IA32_EFER MSR:
10697 * - Bits reserved in the IA32_EFER MSR must be 0.
10698 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10699 * the IA-32e mode guest VM-exit control. It must also be identical
10700 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10701 * CR0.PG) is 1.
10702 */
10703 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10704 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10705 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10706 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10707 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10708 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10709 nested_vmx_entry_failure(vcpu, vmcs12,
10710 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10711 return 1;
10712 }
10713 }
10714
10715 /*
10716 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10717 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10718 * the values of the LMA and LME bits in the field must each be that of
10719 * the host address-space size VM-exit control.
10720 */
10721 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10722 ia32e = (vmcs12->vm_exit_controls &
10723 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10724 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10725 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10726 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10727 nested_vmx_entry_failure(vcpu, vmcs12,
10728 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10729 return 1;
10730 }
10731 }
10732
10733 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010734 * We're finally done with prerequisite checking, and can start with
10735 * the nested entry.
10736 */
10737
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010738 enter_guest_mode(vcpu);
10739
Jan Kiszka2996fca2014-06-16 13:59:43 +020010740 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10741 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10742
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010743 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010744 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010745 vmx_vcpu_put(vcpu);
10746 vmx_vcpu_load(vcpu, cpu);
10747 vcpu->cpu = cpu;
10748 put_cpu();
10749
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010750 vmx_segment_cache_clear(vmx);
10751
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010752 prepare_vmcs02(vcpu, vmcs12);
10753
Wincy Vanff651cb2014-12-11 08:52:58 +030010754 msr_entry_idx = nested_vmx_load_msr(vcpu,
10755 vmcs12->vm_entry_msr_load_addr,
10756 vmcs12->vm_entry_msr_load_count);
10757 if (msr_entry_idx) {
10758 leave_guest_mode(vcpu);
10759 vmx_load_vmcs01(vcpu);
10760 nested_vmx_entry_failure(vcpu, vmcs12,
10761 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10762 return 1;
10763 }
10764
10765 vmcs12->launch_state = 1;
10766
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010767 /* Hide L1D cache contents from the nested guest. */
10768 vmx->vcpu.arch.l1tf_flush_l1d = true;
10769
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010770 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010771 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010772
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010773 vmx->nested.nested_run_pending = 1;
10774
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010775 /*
10776 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10777 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10778 * returned as far as L1 is concerned. It will only return (and set
10779 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10780 */
10781 return 1;
10782}
10783
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010784/*
10785 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10786 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10787 * This function returns the new value we should put in vmcs12.guest_cr0.
10788 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10789 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10790 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10791 * didn't trap the bit, because if L1 did, so would L0).
10792 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10793 * been modified by L2, and L1 knows it. So just leave the old value of
10794 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10795 * isn't relevant, because if L0 traps this bit it can set it to anything.
10796 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10797 * changed these bits, and therefore they need to be updated, but L0
10798 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10799 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10800 */
10801static inline unsigned long
10802vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10803{
10804 return
10805 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10806 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10807 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10808 vcpu->arch.cr0_guest_owned_bits));
10809}
10810
10811static inline unsigned long
10812vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10813{
10814 return
10815 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10816 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10817 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10818 vcpu->arch.cr4_guest_owned_bits));
10819}
10820
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010821static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10822 struct vmcs12 *vmcs12)
10823{
10824 u32 idt_vectoring;
10825 unsigned int nr;
10826
Gleb Natapov851eb6672013-09-25 12:51:34 +030010827 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010828 nr = vcpu->arch.exception.nr;
10829 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10830
10831 if (kvm_exception_is_soft(nr)) {
10832 vmcs12->vm_exit_instruction_len =
10833 vcpu->arch.event_exit_inst_len;
10834 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10835 } else
10836 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10837
10838 if (vcpu->arch.exception.has_error_code) {
10839 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10840 vmcs12->idt_vectoring_error_code =
10841 vcpu->arch.exception.error_code;
10842 }
10843
10844 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010845 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010846 vmcs12->idt_vectoring_info_field =
10847 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10848 } else if (vcpu->arch.interrupt.pending) {
10849 nr = vcpu->arch.interrupt.nr;
10850 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10851
10852 if (vcpu->arch.interrupt.soft) {
10853 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10854 vmcs12->vm_entry_instruction_len =
10855 vcpu->arch.event_exit_inst_len;
10856 } else
10857 idt_vectoring |= INTR_TYPE_EXT_INTR;
10858
10859 vmcs12->idt_vectoring_info_field = idt_vectoring;
10860 }
10861}
10862
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010863static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10864{
10865 struct vcpu_vmx *vmx = to_vmx(vcpu);
10866
Jan Kiszkaf4124502014-03-07 20:03:13 +010010867 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10868 vmx->nested.preemption_timer_expired) {
10869 if (vmx->nested.nested_run_pending)
10870 return -EBUSY;
10871 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10872 return 0;
10873 }
10874
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010875 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010876 if (vmx->nested.nested_run_pending ||
10877 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010878 return -EBUSY;
10879 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10880 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10881 INTR_INFO_VALID_MASK, 0);
10882 /*
10883 * The NMI-triggered VM exit counts as injection:
10884 * clear this one and block further NMIs.
10885 */
10886 vcpu->arch.nmi_pending = 0;
10887 vmx_set_nmi_mask(vcpu, true);
10888 return 0;
10889 }
10890
10891 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10892 nested_exit_on_intr(vcpu)) {
10893 if (vmx->nested.nested_run_pending)
10894 return -EBUSY;
10895 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010896 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010897 }
10898
David Hildenbrand1edccf22017-01-25 11:58:58 +010010899 vmx_complete_nested_posted_interrupt(vcpu);
10900 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010901}
10902
Jan Kiszkaf4124502014-03-07 20:03:13 +010010903static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10904{
10905 ktime_t remaining =
10906 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10907 u64 value;
10908
10909 if (ktime_to_ns(remaining) <= 0)
10910 return 0;
10911
10912 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10913 do_div(value, 1000000);
10914 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10915}
10916
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010917/*
10918 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10919 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10920 * and this function updates it to reflect the changes to the guest state while
10921 * L2 was running (and perhaps made some exits which were handled directly by L0
10922 * without going back to L1), and to reflect the exit reason.
10923 * Note that we do not have to copy here all VMCS fields, just those that
10924 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10925 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10926 * which already writes to vmcs12 directly.
10927 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010928static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10929 u32 exit_reason, u32 exit_intr_info,
10930 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010931{
10932 /* update guest state fields: */
10933 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10934 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10935
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010936 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10937 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10938 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10939
10940 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10941 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10942 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10943 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10944 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10945 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10946 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10947 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10948 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10949 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10950 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10951 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10952 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10953 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10954 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10955 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10956 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10957 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10958 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10959 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10960 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10961 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10962 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10963 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10964 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10965 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10966 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10967 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10968 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10969 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10970 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10971 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10972 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10973 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10974 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10975 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10976
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010977 vmcs12->guest_interruptibility_info =
10978 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10979 vmcs12->guest_pending_dbg_exceptions =
10980 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010981 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10982 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10983 else
10984 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010985
Jan Kiszkaf4124502014-03-07 20:03:13 +010010986 if (nested_cpu_has_preemption_timer(vmcs12)) {
10987 if (vmcs12->vm_exit_controls &
10988 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10989 vmcs12->vmx_preemption_timer_value =
10990 vmx_get_preemption_timer_value(vcpu);
10991 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10992 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010993
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010994 /*
10995 * In some cases (usually, nested EPT), L2 is allowed to change its
10996 * own CR3 without exiting. If it has changed it, we must keep it.
10997 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10998 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10999 *
11000 * Additionally, restore L2's PDPTR to vmcs12.
11001 */
11002 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011003 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011004 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11005 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11006 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11007 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11008 }
11009
Jim Mattson4933e9f2017-06-01 12:44:46 -070011010 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011011
Wincy Van608406e2015-02-03 23:57:51 +080011012 if (nested_cpu_has_vid(vmcs12))
11013 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11014
Jan Kiszkac18911a2013-03-13 16:06:41 +010011015 vmcs12->vm_entry_controls =
11016 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011017 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011018
Jan Kiszka2996fca2014-06-16 13:59:43 +020011019 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11020 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11021 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11022 }
11023
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011024 /* TODO: These cannot have changed unless we have MSR bitmaps and
11025 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011026 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011027 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011028 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11029 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011030 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11031 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11032 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011033 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011034 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080011035 if (nested_cpu_has_xsaves(vmcs12))
11036 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011037
11038 /* update exit information fields: */
11039
Jan Kiszka533558b2014-01-04 18:47:20 +010011040 vmcs12->vm_exit_reason = exit_reason;
11041 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011042
Jan Kiszka533558b2014-01-04 18:47:20 +010011043 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020011044 if ((vmcs12->vm_exit_intr_info &
11045 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
11046 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
11047 vmcs12->vm_exit_intr_error_code =
11048 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011049 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011050 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11051 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11052
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011053 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11054 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11055 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011056 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011057
11058 /*
11059 * Transfer the event that L0 or L1 may wanted to inject into
11060 * L2 to IDT_VECTORING_INFO_FIELD.
11061 */
11062 vmcs12_save_pending_event(vcpu, vmcs12);
11063 }
11064
11065 /*
11066 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11067 * preserved above and would only end up incorrectly in L1.
11068 */
11069 vcpu->arch.nmi_injected = false;
11070 kvm_clear_exception_queue(vcpu);
11071 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011072}
11073
11074/*
11075 * A part of what we need to when the nested L2 guest exits and we want to
11076 * run its L1 parent, is to reset L1's guest state to the host state specified
11077 * in vmcs12.
11078 * This function is to be called not only on normal nested exit, but also on
11079 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11080 * Failures During or After Loading Guest State").
11081 * This function should be called when the active VMCS is L1's (vmcs01).
11082 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011083static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11084 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011085{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011086 struct kvm_segment seg;
11087
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011088 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11089 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011090 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011091 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11092 else
11093 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11094 vmx_set_efer(vcpu, vcpu->arch.efer);
11095
11096 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11097 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011098 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011099 /*
11100 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11101 * actually changed, because it depends on the current state of
11102 * fpu_active (which may have changed).
11103 * Note that vmx_set_cr0 refers to efer set above.
11104 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011105 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011106 /*
11107 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
11108 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
11109 * but we also need to update cr0_guest_host_mask and exception_bitmap.
11110 */
11111 update_exception_bitmap(vcpu);
11112 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
11113 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11114
11115 /*
11116 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
11117 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
11118 */
11119 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080011120 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011121
Jan Kiszka29bf08f2013-12-28 16:31:52 +010011122 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011123
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011124 kvm_set_cr3(vcpu, vmcs12->host_cr3);
11125 kvm_mmu_reset_context(vcpu);
11126
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011127 if (!enable_ept)
11128 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11129
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011130 if (enable_vpid) {
11131 /*
11132 * Trivially support vpid by letting L2s share their parent
11133 * L1's vpid. TODO: move to a more elaborate solution, giving
11134 * each L2 its own vpid and exposing the vpid feature to L1.
11135 */
11136 vmx_flush_tlb(vcpu);
11137 }
11138
11139
11140 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11141 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11142 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11143 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11144 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020011145 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11146 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011147
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011148 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11149 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11150 vmcs_write64(GUEST_BNDCFGS, 0);
11151
Jan Kiszka44811c02013-08-04 17:17:27 +020011152 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011153 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011154 vcpu->arch.pat = vmcs12->host_ia32_pat;
11155 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011156 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11157 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11158 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011159
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011160 /* Set L1 segment info according to Intel SDM
11161 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11162 seg = (struct kvm_segment) {
11163 .base = 0,
11164 .limit = 0xFFFFFFFF,
11165 .selector = vmcs12->host_cs_selector,
11166 .type = 11,
11167 .present = 1,
11168 .s = 1,
11169 .g = 1
11170 };
11171 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11172 seg.l = 1;
11173 else
11174 seg.db = 1;
11175 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11176 seg = (struct kvm_segment) {
11177 .base = 0,
11178 .limit = 0xFFFFFFFF,
11179 .type = 3,
11180 .present = 1,
11181 .s = 1,
11182 .db = 1,
11183 .g = 1
11184 };
11185 seg.selector = vmcs12->host_ds_selector;
11186 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11187 seg.selector = vmcs12->host_es_selector;
11188 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11189 seg.selector = vmcs12->host_ss_selector;
11190 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11191 seg.selector = vmcs12->host_fs_selector;
11192 seg.base = vmcs12->host_fs_base;
11193 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11194 seg.selector = vmcs12->host_gs_selector;
11195 seg.base = vmcs12->host_gs_base;
11196 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11197 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011198 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011199 .limit = 0x67,
11200 .selector = vmcs12->host_tr_selector,
11201 .type = 11,
11202 .present = 1
11203 };
11204 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11205
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011206 kvm_set_dr(vcpu, 7, 0x400);
11207 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011208
Wincy Van3af18d92015-02-03 23:49:31 +080011209 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011210 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011211
Wincy Vanff651cb2014-12-11 08:52:58 +030011212 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11213 vmcs12->vm_exit_msr_load_count))
11214 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011215}
11216
11217/*
11218 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11219 * and modify vmcs12 to make it see what it would expect to see there if
11220 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11221 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011222static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11223 u32 exit_intr_info,
11224 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011225{
11226 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011227 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11228
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011229 /* trying to cancel vmlaunch/vmresume is a bug */
11230 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11231
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011232 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011233 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11234 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011235
Wincy Vanff651cb2014-12-11 08:52:58 +030011236 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11237 vmcs12->vm_exit_msr_store_count))
11238 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11239
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011240 vmx_load_vmcs01(vcpu);
11241
Bandan Das77b0f5d2014-04-19 18:17:45 -040011242 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11243 && nested_exit_intr_ack_set(vcpu)) {
11244 int irq = kvm_cpu_get_interrupt(vcpu);
11245 WARN_ON(irq < 0);
11246 vmcs12->vm_exit_intr_info = irq |
11247 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11248 }
11249
Jan Kiszka542060e2014-01-04 18:47:21 +010011250 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11251 vmcs12->exit_qualification,
11252 vmcs12->idt_vectoring_info_field,
11253 vmcs12->vm_exit_intr_info,
11254 vmcs12->vm_exit_intr_error_code,
11255 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011256
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011257 vm_entry_controls_reset_shadow(vmx);
11258 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011259 vmx_segment_cache_clear(vmx);
11260
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011261 load_vmcs12_host_state(vcpu, vmcs12);
11262
Paolo Bonzini93140062016-07-06 13:23:51 +020011263 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011264 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11265 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011266 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011267 if (vmx->hv_deadline_tsc == -1)
11268 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11269 PIN_BASED_VMX_PREEMPTION_TIMER);
11270 else
11271 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11272 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011273 if (kvm_has_tsc_control)
11274 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011275
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011276 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11277 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11278 vmx_set_virtual_x2apic_mode(vcpu,
11279 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011280 } else if (!nested_cpu_has_ept(vmcs12) &&
11281 nested_cpu_has2(vmcs12,
11282 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11283 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011284 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011285
11286 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11287 vmx->host_rsp = 0;
11288
11289 /* Unpin physical memory we referred to in vmcs02 */
11290 if (vmx->nested.apic_access_page) {
11291 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011292 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011293 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011294 if (vmx->nested.virtual_apic_page) {
11295 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011296 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011297 }
Wincy Van705699a2015-02-03 23:58:17 +080011298 if (vmx->nested.pi_desc_page) {
11299 kunmap(vmx->nested.pi_desc_page);
11300 nested_release_page(vmx->nested.pi_desc_page);
11301 vmx->nested.pi_desc_page = NULL;
11302 vmx->nested.pi_desc = NULL;
11303 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011304
11305 /*
Tang Chen38b99172014-09-24 15:57:54 +080011306 * We are now running in L2, mmu_notifier will force to reload the
11307 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11308 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011309 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011310
11311 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011312 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11313 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11314 * success or failure flag accordingly.
11315 */
11316 if (unlikely(vmx->fail)) {
11317 vmx->fail = 0;
11318 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11319 } else
11320 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011321 if (enable_shadow_vmcs)
11322 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011323
11324 /* in case we halted in L2 */
11325 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011326}
11327
Nadav Har'El7c177932011-05-25 23:12:04 +030011328/*
Jan Kiszka42124922014-01-04 18:47:19 +010011329 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11330 */
11331static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11332{
Wanpeng Lic886f282017-03-06 04:03:28 -080011333 if (is_guest_mode(vcpu)) {
11334 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011335 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011336 }
Jan Kiszka42124922014-01-04 18:47:19 +010011337 free_nested(to_vmx(vcpu));
11338}
11339
11340/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011341 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11342 * 23.7 "VM-entry failures during or after loading guest state" (this also
11343 * lists the acceptable exit-reason and exit-qualification parameters).
11344 * It should only be called before L2 actually succeeded to run, and when
11345 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11346 */
11347static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11348 struct vmcs12 *vmcs12,
11349 u32 reason, unsigned long qualification)
11350{
11351 load_vmcs12_host_state(vcpu, vmcs12);
11352 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11353 vmcs12->exit_qualification = qualification;
11354 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011355 if (enable_shadow_vmcs)
11356 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011357}
11358
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011359static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11360 struct x86_instruction_info *info,
11361 enum x86_intercept_stage stage)
11362{
11363 return X86EMUL_CONTINUE;
11364}
11365
Yunhong Jiang64672c92016-06-13 14:19:59 -070011366#ifdef CONFIG_X86_64
11367/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11368static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11369 u64 divisor, u64 *result)
11370{
11371 u64 low = a << shift, high = a >> (64 - shift);
11372
11373 /* To avoid the overflow on divq */
11374 if (high >= divisor)
11375 return 1;
11376
11377 /* Low hold the result, high hold rem which is discarded */
11378 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11379 "rm" (divisor), "0" (low), "1" (high));
11380 *result = low;
11381
11382 return 0;
11383}
11384
11385static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11386{
11387 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011388 u64 tscl = rdtsc();
11389 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11390 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011391
11392 /* Convert to host delta tsc if tsc scaling is enabled */
11393 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11394 u64_shl_div_u64(delta_tsc,
11395 kvm_tsc_scaling_ratio_frac_bits,
11396 vcpu->arch.tsc_scaling_ratio,
11397 &delta_tsc))
11398 return -ERANGE;
11399
11400 /*
11401 * If the delta tsc can't fit in the 32 bit after the multi shift,
11402 * we can't use the preemption timer.
11403 * It's possible that it fits on later vmentries, but checking
11404 * on every vmentry is costly so we just use an hrtimer.
11405 */
11406 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11407 return -ERANGE;
11408
11409 vmx->hv_deadline_tsc = tscl + delta_tsc;
11410 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11411 PIN_BASED_VMX_PREEMPTION_TIMER);
11412 return 0;
11413}
11414
11415static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11416{
11417 struct vcpu_vmx *vmx = to_vmx(vcpu);
11418 vmx->hv_deadline_tsc = -1;
11419 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11420 PIN_BASED_VMX_PREEMPTION_TIMER);
11421}
11422#endif
11423
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011424static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011425{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011426 if (ple_gap)
11427 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011428}
11429
Kai Huang843e4332015-01-28 10:54:28 +080011430static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11431 struct kvm_memory_slot *slot)
11432{
11433 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11434 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11435}
11436
11437static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11438 struct kvm_memory_slot *slot)
11439{
11440 kvm_mmu_slot_set_dirty(kvm, slot);
11441}
11442
11443static void vmx_flush_log_dirty(struct kvm *kvm)
11444{
11445 kvm_flush_pml_buffers(kvm);
11446}
11447
11448static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11449 struct kvm_memory_slot *memslot,
11450 gfn_t offset, unsigned long mask)
11451{
11452 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11453}
11454
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011455static void __pi_post_block(struct kvm_vcpu *vcpu)
11456{
11457 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11458 struct pi_desc old, new;
11459 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011460
11461 do {
11462 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011463 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11464 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011465
11466 dest = cpu_physical_id(vcpu->cpu);
11467
11468 if (x2apic_enabled())
11469 new.ndst = dest;
11470 else
11471 new.ndst = (dest << 8) & 0xFF00;
11472
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011473 /* set 'NV' to 'notification vector' */
11474 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011475 } while (cmpxchg64(&pi_desc->control, old.control,
11476 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011477
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011478 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11479 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011480 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011481 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011482 vcpu->pre_pcpu = -1;
11483 }
11484}
11485
Feng Wuefc64402015-09-18 22:29:51 +080011486/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011487 * This routine does the following things for vCPU which is going
11488 * to be blocked if VT-d PI is enabled.
11489 * - Store the vCPU to the wakeup list, so when interrupts happen
11490 * we can find the right vCPU to wake up.
11491 * - Change the Posted-interrupt descriptor as below:
11492 * 'NDST' <-- vcpu->pre_pcpu
11493 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11494 * - If 'ON' is set during this process, which means at least one
11495 * interrupt is posted for this vCPU, we cannot block it, in
11496 * this case, return 1, otherwise, return 0.
11497 *
11498 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011499static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011500{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011501 unsigned int dest;
11502 struct pi_desc old, new;
11503 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11504
11505 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011506 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11507 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011508 return 0;
11509
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011510 WARN_ON(irqs_disabled());
11511 local_irq_disable();
11512 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11513 vcpu->pre_pcpu = vcpu->cpu;
11514 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11515 list_add_tail(&vcpu->blocked_vcpu_list,
11516 &per_cpu(blocked_vcpu_on_cpu,
11517 vcpu->pre_pcpu));
11518 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11519 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011520
11521 do {
11522 old.control = new.control = pi_desc->control;
11523
Feng Wubf9f6ac2015-09-18 22:29:55 +080011524 WARN((pi_desc->sn == 1),
11525 "Warning: SN field of posted-interrupts "
11526 "is set before blocking\n");
11527
11528 /*
11529 * Since vCPU can be preempted during this process,
11530 * vcpu->cpu could be different with pre_pcpu, we
11531 * need to set pre_pcpu as the destination of wakeup
11532 * notification event, then we can find the right vCPU
11533 * to wakeup in wakeup handler if interrupts happen
11534 * when the vCPU is in blocked state.
11535 */
11536 dest = cpu_physical_id(vcpu->pre_pcpu);
11537
11538 if (x2apic_enabled())
11539 new.ndst = dest;
11540 else
11541 new.ndst = (dest << 8) & 0xFF00;
11542
11543 /* set 'NV' to 'wakeup vector' */
11544 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011545 } while (cmpxchg64(&pi_desc->control, old.control,
11546 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011547
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011548 /* We should not block the vCPU if an interrupt is posted for it. */
11549 if (pi_test_on(pi_desc) == 1)
11550 __pi_post_block(vcpu);
11551
11552 local_irq_enable();
11553 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011554}
11555
Yunhong Jiangbc225122016-06-13 14:19:58 -070011556static int vmx_pre_block(struct kvm_vcpu *vcpu)
11557{
11558 if (pi_pre_block(vcpu))
11559 return 1;
11560
Yunhong Jiang64672c92016-06-13 14:19:59 -070011561 if (kvm_lapic_hv_timer_in_use(vcpu))
11562 kvm_lapic_switch_to_sw_timer(vcpu);
11563
Yunhong Jiangbc225122016-06-13 14:19:58 -070011564 return 0;
11565}
11566
11567static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011568{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011569 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011570 return;
11571
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011572 WARN_ON(irqs_disabled());
11573 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011574 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011575 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011576}
11577
Yunhong Jiangbc225122016-06-13 14:19:58 -070011578static void vmx_post_block(struct kvm_vcpu *vcpu)
11579{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011580 if (kvm_x86_ops->set_hv_timer)
11581 kvm_lapic_switch_to_hv_timer(vcpu);
11582
Yunhong Jiangbc225122016-06-13 14:19:58 -070011583 pi_post_block(vcpu);
11584}
11585
Feng Wubf9f6ac2015-09-18 22:29:55 +080011586/*
Feng Wuefc64402015-09-18 22:29:51 +080011587 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11588 *
11589 * @kvm: kvm
11590 * @host_irq: host irq of the interrupt
11591 * @guest_irq: gsi of the interrupt
11592 * @set: set or unset PI
11593 * returns 0 on success, < 0 on failure
11594 */
11595static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11596 uint32_t guest_irq, bool set)
11597{
11598 struct kvm_kernel_irq_routing_entry *e;
11599 struct kvm_irq_routing_table *irq_rt;
11600 struct kvm_lapic_irq irq;
11601 struct kvm_vcpu *vcpu;
11602 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011603 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011604
11605 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011606 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11607 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011608 return 0;
11609
11610 idx = srcu_read_lock(&kvm->irq_srcu);
11611 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011612 if (guest_irq >= irq_rt->nr_rt_entries ||
11613 hlist_empty(&irq_rt->map[guest_irq])) {
11614 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11615 guest_irq, irq_rt->nr_rt_entries);
11616 goto out;
11617 }
Feng Wuefc64402015-09-18 22:29:51 +080011618
11619 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11620 if (e->type != KVM_IRQ_ROUTING_MSI)
11621 continue;
11622 /*
11623 * VT-d PI cannot support posting multicast/broadcast
11624 * interrupts to a vCPU, we still use interrupt remapping
11625 * for these kind of interrupts.
11626 *
11627 * For lowest-priority interrupts, we only support
11628 * those with single CPU as the destination, e.g. user
11629 * configures the interrupts via /proc/irq or uses
11630 * irqbalance to make the interrupts single-CPU.
11631 *
11632 * We will support full lowest-priority interrupt later.
11633 */
11634
Radim Krčmář371313132016-07-12 22:09:27 +020011635 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011636 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11637 /*
11638 * Make sure the IRTE is in remapped mode if
11639 * we don't handle it in posted mode.
11640 */
11641 ret = irq_set_vcpu_affinity(host_irq, NULL);
11642 if (ret < 0) {
11643 printk(KERN_INFO
11644 "failed to back to remapped mode, irq: %u\n",
11645 host_irq);
11646 goto out;
11647 }
11648
Feng Wuefc64402015-09-18 22:29:51 +080011649 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011650 }
Feng Wuefc64402015-09-18 22:29:51 +080011651
11652 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11653 vcpu_info.vector = irq.vector;
11654
Feng Wub6ce9782016-01-25 16:53:35 +080011655 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011656 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11657
11658 if (set)
11659 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011660 else
Feng Wuefc64402015-09-18 22:29:51 +080011661 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011662
11663 if (ret < 0) {
11664 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11665 __func__);
11666 goto out;
11667 }
11668 }
11669
11670 ret = 0;
11671out:
11672 srcu_read_unlock(&kvm->irq_srcu, idx);
11673 return ret;
11674}
11675
Ashok Rajc45dcc72016-06-22 14:59:56 +080011676static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11677{
11678 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11679 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11680 FEATURE_CONTROL_LMCE;
11681 else
11682 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11683 ~FEATURE_CONTROL_LMCE;
11684}
11685
Kees Cook404f6aa2016-08-08 16:29:06 -070011686static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011687 .cpu_has_kvm_support = cpu_has_kvm_support,
11688 .disabled_by_bios = vmx_disabled_by_bios,
11689 .hardware_setup = hardware_setup,
11690 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011691 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011692 .hardware_enable = hardware_enable,
11693 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011694 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011695 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011696
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011697 .vm_init = vmx_vm_init,
11698
Avi Kivity6aa8b732006-12-10 02:21:36 -080011699 .vcpu_create = vmx_create_vcpu,
11700 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011701 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011702
Avi Kivity04d2cc72007-09-10 18:10:54 +030011703 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011704 .vcpu_load = vmx_vcpu_load,
11705 .vcpu_put = vmx_vcpu_put,
11706
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011707 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky62d88fc2018-02-21 13:39:51 -060011708 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011709 .get_msr = vmx_get_msr,
11710 .set_msr = vmx_set_msr,
11711 .get_segment_base = vmx_get_segment_base,
11712 .get_segment = vmx_get_segment,
11713 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011714 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011715 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011716 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011717 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011718 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011719 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011720 .set_cr3 = vmx_set_cr3,
11721 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011722 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011723 .get_idt = vmx_get_idt,
11724 .set_idt = vmx_set_idt,
11725 .get_gdt = vmx_get_gdt,
11726 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011727 .get_dr6 = vmx_get_dr6,
11728 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011729 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011730 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011731 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011732 .get_rflags = vmx_get_rflags,
11733 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011734
11735 .get_pkru = vmx_get_pkru,
11736
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011737 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011738 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011739
11740 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011741
Avi Kivity6aa8b732006-12-10 02:21:36 -080011742 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011743 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011744 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011745 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11746 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011747 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011748 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011749 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011750 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011751 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011752 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011753 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011754 .get_nmi_mask = vmx_get_nmi_mask,
11755 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011756 .enable_nmi_window = enable_nmi_window,
11757 .enable_irq_window = enable_irq_window,
11758 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011759 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011760 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011761 .get_enable_apicv = vmx_get_enable_apicv,
11762 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011763 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11764 .hwapic_irr_update = vmx_hwapic_irr_update,
11765 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011766 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11767 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011768
Izik Eiduscbc94022007-10-25 00:29:55 +020011769 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011770 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011771 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011772
Avi Kivity586f9602010-11-18 13:09:54 +020011773 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011774
Sheng Yang17cc3932010-01-05 19:02:27 +080011775 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011776
11777 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011778
11779 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011780 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011781
11782 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011783
11784 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011785
11786 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011787
11788 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011789
11790 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011791 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011792 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011793 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011794
11795 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011796
11797 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011798
11799 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11800 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11801 .flush_log_dirty = vmx_flush_log_dirty,
11802 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011803
Feng Wubf9f6ac2015-09-18 22:29:55 +080011804 .pre_block = vmx_pre_block,
11805 .post_block = vmx_post_block,
11806
Wei Huang25462f72015-06-19 15:45:05 +020011807 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011808
11809 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011810
11811#ifdef CONFIG_X86_64
11812 .set_hv_timer = vmx_set_hv_timer,
11813 .cancel_hv_timer = vmx_cancel_hv_timer,
11814#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011815
11816 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011817};
11818
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011819static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011820{
11821 if (vmx_l1d_flush_pages) {
11822 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11823 vmx_l1d_flush_pages = NULL;
11824 }
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011825 /* Restore state so sysfs ignores VMX */
11826 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011827}
11828
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011829
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011830static void vmx_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011831{
Dave Young2965faa2015-09-09 15:38:55 -070011832#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011833 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011834 synchronize_rcu();
11835#endif
11836
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011837 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011838
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011839 vmx_cleanup_l1d_flush();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011840}
Avi Kivity6aa8b732006-12-10 02:21:36 -080011841module_exit(vmx_exit)
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011842
11843static int __init vmx_init(void)
11844{
11845 int r;
11846
11847 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11848 __alignof__(struct vcpu_vmx), THIS_MODULE);
11849 if (r)
11850 return r;
11851
11852 /*
Thomas Gleixner641a2112018-07-13 16:23:19 +020011853 * Must be called after kvm_init() so enable_ept is properly set
11854 * up. Hand the parameter mitigation value in which was stored in
11855 * the pre module init parser. If no parameter was given, it will
11856 * contain 'auto' which will be turned into the default 'cond'
11857 * mitigation mode.
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011858 */
Thomas Gleixner641a2112018-07-13 16:23:19 +020011859 if (boot_cpu_has(X86_BUG_L1TF)) {
11860 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
11861 if (r) {
11862 vmx_exit();
11863 return r;
11864 }
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011865 }
11866
11867#ifdef CONFIG_KEXEC_CORE
11868 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11869 crash_vmclear_local_loaded_vmcss);
11870#endif
11871
11872 return 0;
11873}
11874module_init(vmx_init)