blob: 4596791dbb81c438b2109c07531c957522bf6646 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeuf935893a2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williamseb99bd62018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Paolo Bonzinia175d512018-02-22 16:43:17 +010053#include <asm/microcode.h>
Thomas Gleixner7a2d2352018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020057#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058
Avi Kivity4ecac3f2008-05-13 13:23:38 +030059#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040060#define __ex_clear(x, reg) \
61 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Josh Triplette9bda3b2012-03-20 23:33:51 -070066static const struct x86_cpu_id vmx_cpu_id[] = {
67 X86_FEATURE_MATCH(X86_FEATURE_VMX),
68 {}
69};
70MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
71
Rusty Russell476bc002012-01-13 09:32:18 +103072static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020073module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080074
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020077
Rusty Russell476bc002012-01-13 09:32:18 +103078static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020079module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080080
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070082module_param_named(unrestricted_guest,
83 enable_unrestricted_guest, bool, S_IRUGO);
84
Xudong Hao83c3a332012-05-28 19:33:35 +080085static bool __read_mostly enable_ept_ad_bits = 1;
86module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
87
Avi Kivitya27685c2012-06-12 20:30:18 +030088static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020089module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030090
Rusty Russell476bc002012-01-13 09:32:18 +103091static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080092module_param(vmm_exclusive, bool, S_IRUGO);
93
Rusty Russell476bc002012-01-13 09:32:18 +103094static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030095module_param(fasteoi, bool, S_IRUGO);
96
Yang Zhang5a717852013-04-11 19:25:16 +080097static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080098module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080099
Abel Gordonabc4fc52013-04-18 14:35:25 +0300100static bool __read_mostly enable_shadow_vmcs = 1;
101module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300102/*
103 * If nested=1, nested virtualization is supported, i.e., guests may use
104 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
105 * use VMX instructions.
106 */
Rusty Russell476bc002012-01-13 09:32:18 +1030107static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300108module_param(nested, bool, S_IRUGO);
109
Wanpeng Li20300092014-12-02 19:14:59 +0800110static u64 __read_mostly host_xss;
111
Kai Huang843e4332015-01-28 10:54:28 +0800112static bool __read_mostly enable_pml = 1;
113module_param_named(pml, enable_pml, bool, S_IRUGO);
114
Paolo Bonzini6236b782018-01-16 16:51:18 +0100115#define MSR_TYPE_R 1
116#define MSR_TYPE_W 2
117#define MSR_TYPE_RW 3
118
119#define MSR_BITMAP_MODE_X2APIC 1
120#define MSR_BITMAP_MODE_X2APIC_APICV 2
121#define MSR_BITMAP_MODE_LM 4
122
Haozhong Zhang64903d62015-10-20 15:39:09 +0800123#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
124
Yunhong Jiang64672c92016-06-13 14:19:59 -0700125/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
126static int __read_mostly cpu_preemption_timer_multi;
127static bool __read_mostly enable_preemption_timer = 1;
128#ifdef CONFIG_X86_64
129module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
130#endif
131
Gleb Natapov50378782013-02-04 16:00:28 +0200132#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
133#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200134#define KVM_VM_CR0_ALWAYS_ON \
135 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200136#define KVM_CR4_GUEST_OWNED_BITS \
137 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700138 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200139
Avi Kivitycdc0e242009-12-06 17:21:14 +0200140#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
141#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
142
Avi Kivity78ac8b42010-04-08 18:19:35 +0300143#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
144
Jan Kiszkaf4124502014-03-07 20:03:13 +0100145#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
146
Jan Dakinevich04c776e2018-02-23 11:42:18 +0100147#define VMX_VPID_EXTENT_SUPPORTED_MASK \
148 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
149 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
150 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
151 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153/*
154 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
155 * ple_gap: upper bound on the amount of time between two successive
156 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500157 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800158 * ple_window: upper bound on the amount of time a guest is allowed to execute
159 * in a PAUSE loop. Tests indicate that most spinlocks are held for
160 * less than 2^12 cycles
161 * Time is measured based on a counter that runs at the same rate as the TSC,
162 * refer SDM volume 3b section 21.6.13 & 22.1.3.
163 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200164#define KVM_VMX_DEFAULT_PLE_GAP 128
165#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
166#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
167#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
168#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
169 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
170
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800171static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
172module_param(ple_gap, int, S_IRUGO);
173
174static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, int, S_IRUGO);
176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
178static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
179module_param(ple_window_grow, int, S_IRUGO);
180
181/* Default resets per-vcpu window every exit to ple_window. */
182static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
183module_param(ple_window_shrink, int, S_IRUGO);
184
185/* Default is to compute the maximum so we can never overflow. */
186static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, int, S_IRUGO);
189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange936f5662018-07-21 22:25:00 +0200193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200194static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200195
Thomas Gleixner641a2112018-07-13 16:23:19 +0200196/* Storage for pre module init parameter parsing */
197static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200198
199static const struct {
200 const char *option;
201 enum vmx_l1d_flush_state cmd;
202} vmentry_l1d_param[] = {
Thomas Gleixner80e55b52018-07-13 16:23:16 +0200203 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200204 {"never", VMENTER_L1D_FLUSH_NEVER},
205 {"cond", VMENTER_L1D_FLUSH_COND},
206 {"always", VMENTER_L1D_FLUSH_ALWAYS},
207};
208
Thomas Gleixner641a2112018-07-13 16:23:19 +0200209#define L1D_CACHE_ORDER 4
210static void *vmx_l1d_flush_pages;
211
212static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
213{
214 struct page *page;
Nicolai Stange587d4992018-07-18 19:07:38 +0200215 unsigned int i;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200216
Thomas Gleixner641a2112018-07-13 16:23:19 +0200217 if (!enable_ept) {
218 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
219 return 0;
220 }
221
Jiri Kosina2decbf52018-07-13 16:23:25 +0200222 /* If set to auto use the default l1tf mitigation method */
223 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
224 switch (l1tf_mitigation) {
225 case L1TF_MITIGATION_OFF:
226 l1tf = VMENTER_L1D_FLUSH_NEVER;
227 break;
228 case L1TF_MITIGATION_FLUSH_NOWARN:
229 case L1TF_MITIGATION_FLUSH:
230 case L1TF_MITIGATION_FLUSH_NOSMT:
231 l1tf = VMENTER_L1D_FLUSH_COND;
232 break;
233 case L1TF_MITIGATION_FULL:
234 case L1TF_MITIGATION_FULL_FORCE:
235 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
236 break;
237 }
238 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
239 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
240 }
241
Thomas Gleixner641a2112018-07-13 16:23:19 +0200242 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
243 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
244 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
245 if (!page)
246 return -ENOMEM;
247 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange587d4992018-07-18 19:07:38 +0200248
249 /*
250 * Initialize each page with a different pattern in
251 * order to protect against KSM in the nested
252 * virtualization case.
253 */
254 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
255 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
256 PAGE_SIZE);
257 }
Thomas Gleixner641a2112018-07-13 16:23:19 +0200258 }
259
260 l1tf_vmx_mitigation = l1tf;
261
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200262 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
263 static_branch_enable(&vmx_l1d_should_flush);
264 else
265 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixnerdff09822018-07-13 16:23:20 +0200266
Nicolai Stange936f5662018-07-21 22:25:00 +0200267 if (l1tf == VMENTER_L1D_FLUSH_COND)
268 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200269 else
Nicolai Stange936f5662018-07-21 22:25:00 +0200270 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner641a2112018-07-13 16:23:19 +0200271 return 0;
272}
273
274static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200275{
276 unsigned int i;
277
Thomas Gleixner641a2112018-07-13 16:23:19 +0200278 if (s) {
279 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200280 if (sysfs_streq(s, vmentry_l1d_param[i].option))
Thomas Gleixner641a2112018-07-13 16:23:19 +0200281 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200282 }
283 }
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200284 return -EINVAL;
285}
286
Thomas Gleixner641a2112018-07-13 16:23:19 +0200287static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
288{
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200289 int l1tf, ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200290
291 if (!boot_cpu_has(X86_BUG_L1TF))
292 return 0;
293
294 l1tf = vmentry_l1d_flush_parse(s);
295 if (l1tf < 0)
296 return l1tf;
297
298 /*
299 * Has vmx_init() run already? If not then this is the pre init
300 * parameter parsing. In that case just store the value and let
301 * vmx_init() do the proper setup after enable_ept has been
302 * established.
303 */
304 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
305 vmentry_l1d_flush_param = l1tf;
306 return 0;
307 }
308
Thomas Gleixner6ccf6332018-07-13 16:23:21 +0200309 mutex_lock(&vmx_l1d_flush_mutex);
310 ret = vmx_setup_l1d_flush(l1tf);
311 mutex_unlock(&vmx_l1d_flush_mutex);
312 return ret;
Thomas Gleixner641a2112018-07-13 16:23:19 +0200313}
314
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200315static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
316{
Thomas Gleixner641a2112018-07-13 16:23:19 +0200317 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200318}
319
320static const struct kernel_param_ops vmentry_l1d_flush_ops = {
321 .set = vmentry_l1d_flush_set,
322 .get = vmentry_l1d_flush_get,
323};
Thomas Gleixner4797c2f2018-07-13 16:23:22 +0200324module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200325
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200326#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300327
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400328struct vmcs {
329 u32 revision_id;
330 u32 abort;
331 char data[0];
332};
333
Nadav Har'Eld462b812011-05-24 15:26:10 +0300334/*
335 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
336 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
337 * loaded on this CPU (so we can clear them if the CPU goes down).
338 */
339struct loaded_vmcs {
340 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700341 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300342 int cpu;
343 int launched;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100344 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300345 struct list_head loaded_vmcss_on_cpu_link;
346};
347
Avi Kivity26bb0982009-09-07 11:14:12 +0300348struct shared_msr_entry {
349 unsigned index;
350 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200351 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300352};
353
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300354/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300355 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
356 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
357 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
358 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
359 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
360 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson46e24df2017-11-27 17:22:25 -0600361 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300362 * underlying hardware which will be used to run L2.
363 * This structure is packed to ensure that its layout is identical across
364 * machines (necessary for live migration).
365 * If there are changes in this struct, VMCS12_REVISION must be changed.
366 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300367typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300368struct __packed vmcs12 {
369 /* According to the Intel spec, a VMCS region must start with the
370 * following two fields. Then follow implementation-specific data.
371 */
372 u32 revision_id;
373 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300374
Nadav Har'El27d6c862011-05-25 23:06:59 +0300375 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
376 u32 padding[7]; /* room for future expansion */
377
Nadav Har'El22bd0352011-05-25 23:05:57 +0300378 u64 io_bitmap_a;
379 u64 io_bitmap_b;
380 u64 msr_bitmap;
381 u64 vm_exit_msr_store_addr;
382 u64 vm_exit_msr_load_addr;
383 u64 vm_entry_msr_load_addr;
384 u64 tsc_offset;
385 u64 virtual_apic_page_addr;
386 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800387 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300388 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800389 u64 eoi_exit_bitmap0;
390 u64 eoi_exit_bitmap1;
391 u64 eoi_exit_bitmap2;
392 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800393 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300394 u64 guest_physical_address;
395 u64 vmcs_link_pointer;
396 u64 guest_ia32_debugctl;
397 u64 guest_ia32_pat;
398 u64 guest_ia32_efer;
399 u64 guest_ia32_perf_global_ctrl;
400 u64 guest_pdptr0;
401 u64 guest_pdptr1;
402 u64 guest_pdptr2;
403 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100404 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300405 u64 host_ia32_pat;
406 u64 host_ia32_efer;
407 u64 host_ia32_perf_global_ctrl;
408 u64 padding64[8]; /* room for future expansion */
409 /*
410 * To allow migration of L1 (complete with its L2 guests) between
411 * machines of different natural widths (32 or 64 bit), we cannot have
412 * unsigned long fields with no explict size. We use u64 (aliased
413 * natural_width) instead. Luckily, x86 is little-endian.
414 */
415 natural_width cr0_guest_host_mask;
416 natural_width cr4_guest_host_mask;
417 natural_width cr0_read_shadow;
418 natural_width cr4_read_shadow;
419 natural_width cr3_target_value0;
420 natural_width cr3_target_value1;
421 natural_width cr3_target_value2;
422 natural_width cr3_target_value3;
423 natural_width exit_qualification;
424 natural_width guest_linear_address;
425 natural_width guest_cr0;
426 natural_width guest_cr3;
427 natural_width guest_cr4;
428 natural_width guest_es_base;
429 natural_width guest_cs_base;
430 natural_width guest_ss_base;
431 natural_width guest_ds_base;
432 natural_width guest_fs_base;
433 natural_width guest_gs_base;
434 natural_width guest_ldtr_base;
435 natural_width guest_tr_base;
436 natural_width guest_gdtr_base;
437 natural_width guest_idtr_base;
438 natural_width guest_dr7;
439 natural_width guest_rsp;
440 natural_width guest_rip;
441 natural_width guest_rflags;
442 natural_width guest_pending_dbg_exceptions;
443 natural_width guest_sysenter_esp;
444 natural_width guest_sysenter_eip;
445 natural_width host_cr0;
446 natural_width host_cr3;
447 natural_width host_cr4;
448 natural_width host_fs_base;
449 natural_width host_gs_base;
450 natural_width host_tr_base;
451 natural_width host_gdtr_base;
452 natural_width host_idtr_base;
453 natural_width host_ia32_sysenter_esp;
454 natural_width host_ia32_sysenter_eip;
455 natural_width host_rsp;
456 natural_width host_rip;
457 natural_width paddingl[8]; /* room for future expansion */
458 u32 pin_based_vm_exec_control;
459 u32 cpu_based_vm_exec_control;
460 u32 exception_bitmap;
461 u32 page_fault_error_code_mask;
462 u32 page_fault_error_code_match;
463 u32 cr3_target_count;
464 u32 vm_exit_controls;
465 u32 vm_exit_msr_store_count;
466 u32 vm_exit_msr_load_count;
467 u32 vm_entry_controls;
468 u32 vm_entry_msr_load_count;
469 u32 vm_entry_intr_info_field;
470 u32 vm_entry_exception_error_code;
471 u32 vm_entry_instruction_len;
472 u32 tpr_threshold;
473 u32 secondary_vm_exec_control;
474 u32 vm_instruction_error;
475 u32 vm_exit_reason;
476 u32 vm_exit_intr_info;
477 u32 vm_exit_intr_error_code;
478 u32 idt_vectoring_info_field;
479 u32 idt_vectoring_error_code;
480 u32 vm_exit_instruction_len;
481 u32 vmx_instruction_info;
482 u32 guest_es_limit;
483 u32 guest_cs_limit;
484 u32 guest_ss_limit;
485 u32 guest_ds_limit;
486 u32 guest_fs_limit;
487 u32 guest_gs_limit;
488 u32 guest_ldtr_limit;
489 u32 guest_tr_limit;
490 u32 guest_gdtr_limit;
491 u32 guest_idtr_limit;
492 u32 guest_es_ar_bytes;
493 u32 guest_cs_ar_bytes;
494 u32 guest_ss_ar_bytes;
495 u32 guest_ds_ar_bytes;
496 u32 guest_fs_ar_bytes;
497 u32 guest_gs_ar_bytes;
498 u32 guest_ldtr_ar_bytes;
499 u32 guest_tr_ar_bytes;
500 u32 guest_interruptibility_info;
501 u32 guest_activity_state;
502 u32 guest_sysenter_cs;
503 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100504 u32 vmx_preemption_timer_value;
505 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300506 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800507 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300508 u16 guest_es_selector;
509 u16 guest_cs_selector;
510 u16 guest_ss_selector;
511 u16 guest_ds_selector;
512 u16 guest_fs_selector;
513 u16 guest_gs_selector;
514 u16 guest_ldtr_selector;
515 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800516 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300517 u16 host_es_selector;
518 u16 host_cs_selector;
519 u16 host_ss_selector;
520 u16 host_ds_selector;
521 u16 host_fs_selector;
522 u16 host_gs_selector;
523 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300524};
525
526/*
527 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
528 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
529 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
530 */
531#define VMCS12_REVISION 0x11e57ed0
532
533/*
534 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
535 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
536 * current implementation, 4K are reserved to avoid future complications.
537 */
538#define VMCS12_SIZE 0x1000
539
540/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300541 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
542 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
543 */
544struct nested_vmx {
545 /* Has the level1 guest done vmxon? */
546 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400547 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300548
549 /* The guest-physical address of the current VMCS L1 keeps for L2 */
550 gpa_t current_vmptr;
551 /* The host-usable pointer to the above */
552 struct page *current_vmcs12_page;
553 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700554 /*
555 * Cache of the guest's VMCS, existing outside of guest memory.
556 * Loaded from guest memory during VMPTRLD. Flushed to guest
557 * memory during VMXOFF, VMCLEAR, VMPTRLD.
558 */
559 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300560 /*
561 * Indicates if the shadow vmcs must be updated with the
562 * data hold by vmcs12
563 */
564 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300565
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200566 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300567 /* L2 must run next, and mustn't decide to exit to L1. */
568 bool nested_run_pending;
Jim Mattson46e24df2017-11-27 17:22:25 -0600569
570 struct loaded_vmcs vmcs02;
571
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300572 /*
Jim Mattson46e24df2017-11-27 17:22:25 -0600573 * Guest pages referred to in the vmcs02 with host-physical
574 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300575 */
576 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800577 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800578 struct page *pi_desc_page;
579 struct pi_desc *pi_desc;
580 bool pi_pending;
581 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100582
583 struct hrtimer preemption_timer;
584 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200585
586 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
587 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800588
Wanpeng Li5c614b32015-10-13 09:18:36 -0700589 u16 vpid02;
590 u16 last_vpid;
591
Wincy Vanb9c237b2015-02-03 23:56:30 +0800592 u32 nested_vmx_procbased_ctls_low;
593 u32 nested_vmx_procbased_ctls_high;
594 u32 nested_vmx_true_procbased_ctls_low;
595 u32 nested_vmx_secondary_ctls_low;
596 u32 nested_vmx_secondary_ctls_high;
597 u32 nested_vmx_pinbased_ctls_low;
598 u32 nested_vmx_pinbased_ctls_high;
599 u32 nested_vmx_exit_ctls_low;
600 u32 nested_vmx_exit_ctls_high;
601 u32 nested_vmx_true_exit_ctls_low;
602 u32 nested_vmx_entry_ctls_low;
603 u32 nested_vmx_entry_ctls_high;
604 u32 nested_vmx_true_entry_ctls_low;
605 u32 nested_vmx_misc_low;
606 u32 nested_vmx_misc_high;
607 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700608 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300609};
610
Yang Zhang01e439b2013-04-11 19:25:12 +0800611#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800612#define POSTED_INTR_SN 1
613
Yang Zhang01e439b2013-04-11 19:25:12 +0800614/* Posted-Interrupt Descriptor */
615struct pi_desc {
616 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800617 union {
618 struct {
619 /* bit 256 - Outstanding Notification */
620 u16 on : 1,
621 /* bit 257 - Suppress Notification */
622 sn : 1,
623 /* bit 271:258 - Reserved */
624 rsvd_1 : 14;
625 /* bit 279:272 - Notification Vector */
626 u8 nv;
627 /* bit 287:280 - Reserved */
628 u8 rsvd_2;
629 /* bit 319:288 - Notification Destination */
630 u32 ndst;
631 };
632 u64 control;
633 };
634 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800635} __aligned(64);
636
Yang Zhanga20ed542013-04-11 19:25:15 +0800637static bool pi_test_and_set_on(struct pi_desc *pi_desc)
638{
639 return test_and_set_bit(POSTED_INTR_ON,
640 (unsigned long *)&pi_desc->control);
641}
642
643static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
644{
645 return test_and_clear_bit(POSTED_INTR_ON,
646 (unsigned long *)&pi_desc->control);
647}
648
649static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
650{
651 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
652}
653
Feng Wuebbfc762015-09-18 22:29:46 +0800654static inline void pi_clear_sn(struct pi_desc *pi_desc)
655{
656 return clear_bit(POSTED_INTR_SN,
657 (unsigned long *)&pi_desc->control);
658}
659
660static inline void pi_set_sn(struct pi_desc *pi_desc)
661{
662 return set_bit(POSTED_INTR_SN,
663 (unsigned long *)&pi_desc->control);
664}
665
666static inline int pi_test_on(struct pi_desc *pi_desc)
667{
668 return test_bit(POSTED_INTR_ON,
669 (unsigned long *)&pi_desc->control);
670}
671
672static inline int pi_test_sn(struct pi_desc *pi_desc)
673{
674 return test_bit(POSTED_INTR_SN,
675 (unsigned long *)&pi_desc->control);
676}
677
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400678struct vmx_msrs {
679 unsigned int nr;
680 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
681};
682
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400683struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000684 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300685 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300686 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200687 bool nmi_known_unmasked;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100688 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300689 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200690 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200691 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300692 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400693 int nmsrs;
694 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800695 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400696#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300697 u64 msr_host_kernel_gs_base;
698 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400699#endif
Ashok Raj70131292018-02-01 22:59:43 +0100700
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100701 u64 arch_capabilities;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +0100702 u64 spec_ctrl;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100703
Gleb Natapov2961e8762013-11-25 15:37:13 +0200704 u32 vm_entry_controls_shadow;
705 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300706 /*
707 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
708 * non-nested (L1) guest, it always points to vmcs01. For a nested
709 * guest (L2), it points to a different VMCS.
710 */
711 struct loaded_vmcs vmcs01;
712 struct loaded_vmcs *loaded_vmcs;
713 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300714 struct msr_autoload {
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400715 struct vmx_msrs guest;
716 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300717 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400718 struct {
719 int loaded;
720 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300721#ifdef CONFIG_X86_64
722 u16 ds_sel, es_sel;
723#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200724 int gs_ldt_reload_needed;
725 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000726 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700727 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400728 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200729 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300730 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300731 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300732 struct kvm_segment segs[8];
733 } rmode;
734 struct {
735 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300736 struct kvm_save_segment {
737 u16 selector;
738 unsigned long base;
739 u32 limit;
740 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300741 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300742 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800743 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300744 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200745
746 /* Support for vnmi-less CPUs */
747 int soft_vnmi_blocked;
748 ktime_t entry_time;
749 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800750 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800751
Yang Zhang01e439b2013-04-11 19:25:12 +0800752 /* Posted interrupt descriptor */
753 struct pi_desc pi_desc;
754
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300755 /* Support for a guest hypervisor (nested VMX) */
756 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200757
758 /* Dynamic PLE window. */
759 int ple_window;
760 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800761
762 /* Support for PML */
763#define PML_ENTITY_NUM 512
764 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800765
Yunhong Jiang64672c92016-06-13 14:19:59 -0700766 /* apic deadline value in host tsc */
767 u64 hv_deadline_tsc;
768
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800769 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800770
771 bool guest_pkru_valid;
772 u32 guest_pkru;
773 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800774
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800775 /*
776 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
777 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
778 * in msr_ia32_feature_control_valid_bits.
779 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800780 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800781 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400782};
783
Avi Kivity2fb92db2011-04-27 19:42:18 +0300784enum segment_cache_field {
785 SEG_FIELD_SEL = 0,
786 SEG_FIELD_BASE = 1,
787 SEG_FIELD_LIMIT = 2,
788 SEG_FIELD_AR = 3,
789
790 SEG_FIELD_NR = 4
791};
792
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400793static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
794{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000795 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400796}
797
Feng Wuefc64402015-09-18 22:29:51 +0800798static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
799{
800 return &(to_vmx(vcpu)->pi_desc);
801}
802
Nadav Har'El22bd0352011-05-25 23:05:57 +0300803#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
804#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
805#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
806 [number##_HIGH] = VMCS12_OFFSET(name)+4
807
Abel Gordon4607c2d2013-04-18 14:35:55 +0300808
Bandan Dasfe2b2012014-04-21 15:20:14 -0400809static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300810 /*
811 * We do NOT shadow fields that are modified when L0
812 * traps and emulates any vmx instruction (e.g. VMPTRLD,
813 * VMXON...) executed by L1.
814 * For example, VM_INSTRUCTION_ERROR is read
815 * by L1 if a vmx instruction fails (part of the error path).
816 * Note the code assumes this logic. If for some reason
817 * we start shadowing these fields then we need to
818 * force a shadow sync when L0 emulates vmx instructions
819 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
820 * by nested_vmx_failValid)
821 */
822 VM_EXIT_REASON,
823 VM_EXIT_INTR_INFO,
824 VM_EXIT_INSTRUCTION_LEN,
825 IDT_VECTORING_INFO_FIELD,
826 IDT_VECTORING_ERROR_CODE,
827 VM_EXIT_INTR_ERROR_CODE,
828 EXIT_QUALIFICATION,
829 GUEST_LINEAR_ADDRESS,
830 GUEST_PHYSICAL_ADDRESS
831};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400832static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300833 ARRAY_SIZE(shadow_read_only_fields);
834
Bandan Dasfe2b2012014-04-21 15:20:14 -0400835static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800836 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300837 GUEST_RIP,
838 GUEST_RSP,
839 GUEST_CR0,
840 GUEST_CR3,
841 GUEST_CR4,
842 GUEST_INTERRUPTIBILITY_INFO,
843 GUEST_RFLAGS,
844 GUEST_CS_SELECTOR,
845 GUEST_CS_AR_BYTES,
846 GUEST_CS_LIMIT,
847 GUEST_CS_BASE,
848 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100849 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300850 CR0_GUEST_HOST_MASK,
851 CR0_READ_SHADOW,
852 CR4_READ_SHADOW,
853 TSC_OFFSET,
854 EXCEPTION_BITMAP,
855 CPU_BASED_VM_EXEC_CONTROL,
856 VM_ENTRY_EXCEPTION_ERROR_CODE,
857 VM_ENTRY_INTR_INFO_FIELD,
858 VM_ENTRY_INSTRUCTION_LEN,
859 VM_ENTRY_EXCEPTION_ERROR_CODE,
860 HOST_FS_BASE,
861 HOST_GS_BASE,
862 HOST_FS_SELECTOR,
863 HOST_GS_SELECTOR
864};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400865static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300866 ARRAY_SIZE(shadow_read_write_fields);
867
Mathias Krause772e0312012-08-30 01:30:19 +0200868static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300869 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800870 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300871 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
872 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
873 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
874 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
875 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
876 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
877 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
878 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800879 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300880 FIELD(HOST_ES_SELECTOR, host_es_selector),
881 FIELD(HOST_CS_SELECTOR, host_cs_selector),
882 FIELD(HOST_SS_SELECTOR, host_ss_selector),
883 FIELD(HOST_DS_SELECTOR, host_ds_selector),
884 FIELD(HOST_FS_SELECTOR, host_fs_selector),
885 FIELD(HOST_GS_SELECTOR, host_gs_selector),
886 FIELD(HOST_TR_SELECTOR, host_tr_selector),
887 FIELD64(IO_BITMAP_A, io_bitmap_a),
888 FIELD64(IO_BITMAP_B, io_bitmap_b),
889 FIELD64(MSR_BITMAP, msr_bitmap),
890 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
891 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
892 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
893 FIELD64(TSC_OFFSET, tsc_offset),
894 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
895 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800896 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300897 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800898 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
899 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
900 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
901 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800902 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300903 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
904 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
905 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
906 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
907 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
908 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
909 FIELD64(GUEST_PDPTR0, guest_pdptr0),
910 FIELD64(GUEST_PDPTR1, guest_pdptr1),
911 FIELD64(GUEST_PDPTR2, guest_pdptr2),
912 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100913 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300914 FIELD64(HOST_IA32_PAT, host_ia32_pat),
915 FIELD64(HOST_IA32_EFER, host_ia32_efer),
916 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
917 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
918 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
919 FIELD(EXCEPTION_BITMAP, exception_bitmap),
920 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
921 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
922 FIELD(CR3_TARGET_COUNT, cr3_target_count),
923 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
924 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
925 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
926 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
927 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
928 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
929 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
930 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
931 FIELD(TPR_THRESHOLD, tpr_threshold),
932 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
933 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
934 FIELD(VM_EXIT_REASON, vm_exit_reason),
935 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
936 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
937 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
938 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
939 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
940 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
941 FIELD(GUEST_ES_LIMIT, guest_es_limit),
942 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
943 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
944 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
945 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
946 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
947 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
948 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
949 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
950 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
951 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
952 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
953 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
954 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
955 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
956 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
957 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
958 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
959 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
960 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
961 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
962 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100963 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300964 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
965 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
966 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
967 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
968 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
969 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
970 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
971 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
972 FIELD(EXIT_QUALIFICATION, exit_qualification),
973 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
974 FIELD(GUEST_CR0, guest_cr0),
975 FIELD(GUEST_CR3, guest_cr3),
976 FIELD(GUEST_CR4, guest_cr4),
977 FIELD(GUEST_ES_BASE, guest_es_base),
978 FIELD(GUEST_CS_BASE, guest_cs_base),
979 FIELD(GUEST_SS_BASE, guest_ss_base),
980 FIELD(GUEST_DS_BASE, guest_ds_base),
981 FIELD(GUEST_FS_BASE, guest_fs_base),
982 FIELD(GUEST_GS_BASE, guest_gs_base),
983 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
984 FIELD(GUEST_TR_BASE, guest_tr_base),
985 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
986 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
987 FIELD(GUEST_DR7, guest_dr7),
988 FIELD(GUEST_RSP, guest_rsp),
989 FIELD(GUEST_RIP, guest_rip),
990 FIELD(GUEST_RFLAGS, guest_rflags),
991 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
992 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
993 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
994 FIELD(HOST_CR0, host_cr0),
995 FIELD(HOST_CR3, host_cr3),
996 FIELD(HOST_CR4, host_cr4),
997 FIELD(HOST_FS_BASE, host_fs_base),
998 FIELD(HOST_GS_BASE, host_gs_base),
999 FIELD(HOST_TR_BASE, host_tr_base),
1000 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1001 FIELD(HOST_IDTR_BASE, host_idtr_base),
1002 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1003 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1004 FIELD(HOST_RSP, host_rsp),
1005 FIELD(HOST_RIP, host_rip),
1006};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001007
1008static inline short vmcs_field_to_offset(unsigned long field)
1009{
Dan Williamseb99bd62018-01-31 17:47:03 -08001010 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1011 unsigned short offset;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001012
Dan Williamseb99bd62018-01-31 17:47:03 -08001013 BUILD_BUG_ON(size > SHRT_MAX);
1014 if (field >= size)
Andrew Honig012df712018-01-10 10:12:03 -08001015 return -ENOENT;
1016
Dan Williamseb99bd62018-01-31 17:47:03 -08001017 field = array_index_nospec(field, size);
1018 offset = vmcs_field_to_offset_table[field];
1019 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001020 return -ENOENT;
Dan Williamseb99bd62018-01-31 17:47:03 -08001021 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001022}
1023
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001024static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1025{
David Matlack4f2777b2016-07-13 17:16:37 -07001026 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001027}
1028
1029static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
1030{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001031 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +08001032 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001033 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +08001034
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001035 return page;
1036}
1037
1038static void nested_release_page(struct page *page)
1039{
1040 kvm_release_page_dirty(page);
1041}
1042
1043static void nested_release_page_clean(struct page *page)
1044{
1045 kvm_release_page_clean(page);
1046}
1047
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001048static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +08001049static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001050static void kvm_cpu_vmxon(u64 addr);
1051static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001052static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +02001053static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001054static void vmx_set_segment(struct kvm_vcpu *vcpu,
1055 struct kvm_segment *var, int seg);
1056static void vmx_get_segment(struct kvm_vcpu *vcpu,
1057 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001058static bool guest_state_valid(struct kvm_vcpu *vcpu);
1059static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +03001060static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +03001061static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +08001062static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzini6236b782018-01-16 16:51:18 +01001063static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj70131292018-02-01 22:59:43 +01001064static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1065 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001066
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1068static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001069/*
1070 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1071 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1072 */
1073static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +03001074static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075
Feng Wubf9f6ac2015-09-18 22:29:55 +08001076/*
1077 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1078 * can find which vCPU should be waken up.
1079 */
1080static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1081static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1082
Avi Kivity3e7c73e2009-02-24 21:46:19 +02001083static unsigned long *vmx_io_bitmap_a;
1084static unsigned long *vmx_io_bitmap_b;
Abel Gordon4607c2d2013-04-18 14:35:55 +03001085static unsigned long *vmx_vmread_bitmap;
1086static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +03001087
Avi Kivity110312c2010-12-21 12:54:20 +02001088static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001089static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001090
Sheng Yang2384d2b2008-01-17 15:14:33 +08001091static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1092static DEFINE_SPINLOCK(vmx_vpid_lock);
1093
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001094static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 int size;
1096 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001097 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001099 u32 pin_based_exec_ctrl;
1100 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001101 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001102 u32 vmexit_ctrl;
1103 u32 vmentry_ctrl;
1104} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105
Hannes Ederefff9e52008-11-28 17:02:06 +01001106static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001107 u32 ept;
1108 u32 vpid;
1109} vmx_capability;
1110
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111#define VMX_SEGMENT_FIELD(seg) \
1112 [VCPU_SREG_##seg] = { \
1113 .selector = GUEST_##seg##_SELECTOR, \
1114 .base = GUEST_##seg##_BASE, \
1115 .limit = GUEST_##seg##_LIMIT, \
1116 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1117 }
1118
Mathias Krause772e0312012-08-30 01:30:19 +02001119static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 unsigned selector;
1121 unsigned base;
1122 unsigned limit;
1123 unsigned ar_bytes;
1124} kvm_vmx_segment_fields[] = {
1125 VMX_SEGMENT_FIELD(CS),
1126 VMX_SEGMENT_FIELD(DS),
1127 VMX_SEGMENT_FIELD(ES),
1128 VMX_SEGMENT_FIELD(FS),
1129 VMX_SEGMENT_FIELD(GS),
1130 VMX_SEGMENT_FIELD(SS),
1131 VMX_SEGMENT_FIELD(TR),
1132 VMX_SEGMENT_FIELD(LDTR),
1133};
1134
Avi Kivity26bb0982009-09-07 11:14:12 +03001135static u64 host_efer;
1136
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001137static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1138
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001139/*
Brian Gerst8c065852010-07-17 09:03:26 -04001140 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001141 * away by decrementing the array size.
1142 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001144#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001145 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001147 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149
Jan Kiszka5bb16012016-02-09 20:14:21 +01001150static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151{
1152 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1153 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001154 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1155}
1156
Jan Kiszka6f054852016-02-09 20:15:18 +01001157static inline bool is_debug(u32 intr_info)
1158{
1159 return is_exception_n(intr_info, DB_VECTOR);
1160}
1161
1162static inline bool is_breakpoint(u32 intr_info)
1163{
1164 return is_exception_n(intr_info, BP_VECTOR);
1165}
1166
Jan Kiszka5bb16012016-02-09 20:14:21 +01001167static inline bool is_page_fault(u32 intr_info)
1168{
1169 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170}
1171
Gui Jianfeng31299942010-03-15 17:29:09 +08001172static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001173{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001174 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001175}
1176
Gui Jianfeng31299942010-03-15 17:29:09 +08001177static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001178{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001179 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001180}
1181
Gui Jianfeng31299942010-03-15 17:29:09 +08001182static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183{
1184 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1185 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1186}
1187
Gui Jianfeng31299942010-03-15 17:29:09 +08001188static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001189{
1190 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1191 INTR_INFO_VALID_MASK)) ==
1192 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1193}
1194
Linus Torvalds587da2b2018-03-20 12:16:59 -07001195/* Undocumented: icebp/int1 */
1196static inline bool is_icebp(u32 intr_info)
1197{
1198 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1199 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1200}
1201
Gui Jianfeng31299942010-03-15 17:29:09 +08001202static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001203{
Sheng Yang04547152009-04-01 15:52:31 +08001204 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001205}
1206
Gui Jianfeng31299942010-03-15 17:29:09 +08001207static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001208{
Sheng Yang04547152009-04-01 15:52:31 +08001209 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001210}
1211
Paolo Bonzini35754c92015-07-29 12:05:37 +02001212static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001213{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001214 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001215}
1216
Gui Jianfeng31299942010-03-15 17:29:09 +08001217static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001218{
Sheng Yang04547152009-04-01 15:52:31 +08001219 return vmcs_config.cpu_based_exec_ctrl &
1220 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001221}
1222
Avi Kivity774ead32007-12-26 13:57:04 +02001223static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001224{
Sheng Yang04547152009-04-01 15:52:31 +08001225 return vmcs_config.cpu_based_2nd_exec_ctrl &
1226 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1227}
1228
Yang Zhang8d146952013-01-25 10:18:50 +08001229static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1230{
1231 return vmcs_config.cpu_based_2nd_exec_ctrl &
1232 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1233}
1234
Yang Zhang83d4c282013-01-25 10:18:49 +08001235static inline bool cpu_has_vmx_apic_register_virt(void)
1236{
1237 return vmcs_config.cpu_based_2nd_exec_ctrl &
1238 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1239}
1240
Yang Zhangc7c9c562013-01-25 10:18:51 +08001241static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1242{
1243 return vmcs_config.cpu_based_2nd_exec_ctrl &
1244 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1245}
1246
Yunhong Jiang64672c92016-06-13 14:19:59 -07001247/*
1248 * Comment's format: document - errata name - stepping - processor name.
1249 * Refer from
1250 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1251 */
1252static u32 vmx_preemption_cpu_tfms[] = {
1253/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12540x000206E6,
1255/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1256/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1257/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12580x00020652,
1259/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12600x00020655,
1261/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1262/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1263/*
1264 * 320767.pdf - AAP86 - B1 -
1265 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1266 */
12670x000106E5,
1268/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
12690x000106A0,
1270/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
12710x000106A1,
1272/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
12730x000106A4,
1274 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1275 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1276 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
12770x000106A5,
1278};
1279
1280static inline bool cpu_has_broken_vmx_preemption_timer(void)
1281{
1282 u32 eax = cpuid_eax(0x00000001), i;
1283
1284 /* Clear the reserved bits */
1285 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001286 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001287 if (eax == vmx_preemption_cpu_tfms[i])
1288 return true;
1289
1290 return false;
1291}
1292
1293static inline bool cpu_has_vmx_preemption_timer(void)
1294{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001295 return vmcs_config.pin_based_exec_ctrl &
1296 PIN_BASED_VMX_PREEMPTION_TIMER;
1297}
1298
Yang Zhang01e439b2013-04-11 19:25:12 +08001299static inline bool cpu_has_vmx_posted_intr(void)
1300{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001301 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1302 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001303}
1304
1305static inline bool cpu_has_vmx_apicv(void)
1306{
1307 return cpu_has_vmx_apic_register_virt() &&
1308 cpu_has_vmx_virtual_intr_delivery() &&
1309 cpu_has_vmx_posted_intr();
1310}
1311
Sheng Yang04547152009-04-01 15:52:31 +08001312static inline bool cpu_has_vmx_flexpriority(void)
1313{
1314 return cpu_has_vmx_tpr_shadow() &&
1315 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001316}
1317
Marcelo Tosattie7997942009-06-11 12:07:40 -03001318static inline bool cpu_has_vmx_ept_execute_only(void)
1319{
Gui Jianfeng31299942010-03-15 17:29:09 +08001320 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001321}
1322
Marcelo Tosattie7997942009-06-11 12:07:40 -03001323static inline bool cpu_has_vmx_ept_2m_page(void)
1324{
Gui Jianfeng31299942010-03-15 17:29:09 +08001325 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001326}
1327
Sheng Yang878403b2010-01-05 19:02:29 +08001328static inline bool cpu_has_vmx_ept_1g_page(void)
1329{
Gui Jianfeng31299942010-03-15 17:29:09 +08001330 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001331}
1332
Sheng Yang4bc9b982010-06-02 14:05:24 +08001333static inline bool cpu_has_vmx_ept_4levels(void)
1334{
1335 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1336}
1337
Xudong Hao83c3a332012-05-28 19:33:35 +08001338static inline bool cpu_has_vmx_ept_ad_bits(void)
1339{
1340 return vmx_capability.ept & VMX_EPT_AD_BIT;
1341}
1342
Gui Jianfeng31299942010-03-15 17:29:09 +08001343static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001344{
Gui Jianfeng31299942010-03-15 17:29:09 +08001345 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001346}
1347
Gui Jianfeng31299942010-03-15 17:29:09 +08001348static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001349{
Gui Jianfeng31299942010-03-15 17:29:09 +08001350 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001351}
1352
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001353static inline bool cpu_has_vmx_invvpid_single(void)
1354{
1355 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1356}
1357
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001358static inline bool cpu_has_vmx_invvpid_global(void)
1359{
1360 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1361}
1362
Wanpeng Li2df19692017-03-23 05:30:08 -07001363static inline bool cpu_has_vmx_invvpid(void)
1364{
1365 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1366}
1367
Gui Jianfeng31299942010-03-15 17:29:09 +08001368static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001369{
Sheng Yang04547152009-04-01 15:52:31 +08001370 return vmcs_config.cpu_based_2nd_exec_ctrl &
1371 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001372}
1373
Gui Jianfeng31299942010-03-15 17:29:09 +08001374static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001375{
1376 return vmcs_config.cpu_based_2nd_exec_ctrl &
1377 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1378}
1379
Gui Jianfeng31299942010-03-15 17:29:09 +08001380static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001381{
1382 return vmcs_config.cpu_based_2nd_exec_ctrl &
1383 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1384}
1385
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001386static inline bool cpu_has_vmx_basic_inout(void)
1387{
1388 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1389}
1390
Paolo Bonzini35754c92015-07-29 12:05:37 +02001391static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001392{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001393 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001394}
1395
Gui Jianfeng31299942010-03-15 17:29:09 +08001396static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001397{
Sheng Yang04547152009-04-01 15:52:31 +08001398 return vmcs_config.cpu_based_2nd_exec_ctrl &
1399 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001400}
1401
Gui Jianfeng31299942010-03-15 17:29:09 +08001402static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001403{
1404 return vmcs_config.cpu_based_2nd_exec_ctrl &
1405 SECONDARY_EXEC_RDTSCP;
1406}
1407
Mao, Junjiead756a12012-07-02 01:18:48 +00001408static inline bool cpu_has_vmx_invpcid(void)
1409{
1410 return vmcs_config.cpu_based_2nd_exec_ctrl &
1411 SECONDARY_EXEC_ENABLE_INVPCID;
1412}
1413
Gui Jianfeng31299942010-03-15 17:29:09 +08001414static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001415{
1416 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1417}
1418
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001419static inline bool cpu_has_vmx_wbinvd_exit(void)
1420{
1421 return vmcs_config.cpu_based_2nd_exec_ctrl &
1422 SECONDARY_EXEC_WBINVD_EXITING;
1423}
1424
Abel Gordonabc4fc52013-04-18 14:35:25 +03001425static inline bool cpu_has_vmx_shadow_vmcs(void)
1426{
1427 u64 vmx_msr;
1428 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1429 /* check if the cpu supports writing r/o exit information fields */
1430 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1431 return false;
1432
1433 return vmcs_config.cpu_based_2nd_exec_ctrl &
1434 SECONDARY_EXEC_SHADOW_VMCS;
1435}
1436
Kai Huang843e4332015-01-28 10:54:28 +08001437static inline bool cpu_has_vmx_pml(void)
1438{
1439 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1440}
1441
Haozhong Zhang64903d62015-10-20 15:39:09 +08001442static inline bool cpu_has_vmx_tsc_scaling(void)
1443{
1444 return vmcs_config.cpu_based_2nd_exec_ctrl &
1445 SECONDARY_EXEC_TSC_SCALING;
1446}
1447
Sheng Yang04547152009-04-01 15:52:31 +08001448static inline bool report_flexpriority(void)
1449{
1450 return flexpriority_enabled;
1451}
1452
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001453static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1454{
1455 return vmcs12->cpu_based_vm_exec_control & bit;
1456}
1457
1458static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1459{
1460 return (vmcs12->cpu_based_vm_exec_control &
1461 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1462 (vmcs12->secondary_vm_exec_control & bit);
1463}
1464
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001465static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001466{
1467 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1468}
1469
Jan Kiszkaf4124502014-03-07 20:03:13 +01001470static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1471{
1472 return vmcs12->pin_based_vm_exec_control &
1473 PIN_BASED_VMX_PREEMPTION_TIMER;
1474}
1475
Nadav Har'El155a97a2013-08-05 11:07:16 +03001476static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1477{
1478 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1479}
1480
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001481static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1482{
1483 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1484 vmx_xsaves_supported();
1485}
1486
Wincy Vanf2b93282015-02-03 23:56:03 +08001487static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1488{
1489 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1490}
1491
Wanpeng Li5c614b32015-10-13 09:18:36 -07001492static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1493{
1494 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1495}
1496
Wincy Van82f0dd42015-02-03 23:57:18 +08001497static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1498{
1499 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1500}
1501
Wincy Van608406e2015-02-03 23:57:51 +08001502static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1503{
1504 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1505}
1506
Wincy Van705699a2015-02-03 23:58:17 +08001507static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1508{
1509 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1510}
1511
Jim Mattson3f618a02016-12-12 11:01:37 -08001512static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001513{
1514 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001515 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001516}
1517
Jan Kiszka533558b2014-01-04 18:47:20 +01001518static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1519 u32 exit_intr_info,
1520 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001521static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1522 struct vmcs12 *vmcs12,
1523 u32 reason, unsigned long qualification);
1524
Rusty Russell8b9cf982007-07-30 16:31:43 +10001525static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001526{
1527 int i;
1528
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001529 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001530 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001531 return i;
1532 return -1;
1533}
1534
Sheng Yang2384d2b2008-01-17 15:14:33 +08001535static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1536{
1537 struct {
1538 u64 vpid : 16;
1539 u64 rsvd : 48;
1540 u64 gva;
1541 } operand = { vpid, 0, gva };
1542
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001543 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001544 /* CF==1 or ZF==1 --> rc = -1 */
1545 "; ja 1f ; ud2 ; 1:"
1546 : : "a"(&operand), "c"(ext) : "cc", "memory");
1547}
1548
Sheng Yang14394422008-04-28 12:24:45 +08001549static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1550{
1551 struct {
1552 u64 eptp, gpa;
1553 } operand = {eptp, gpa};
1554
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001555 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001556 /* CF==1 or ZF==1 --> rc = -1 */
1557 "; ja 1f ; ud2 ; 1:\n"
1558 : : "a" (&operand), "c" (ext) : "cc", "memory");
1559}
1560
Avi Kivity26bb0982009-09-07 11:14:12 +03001561static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001562{
1563 int i;
1564
Rusty Russell8b9cf982007-07-30 16:31:43 +10001565 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001566 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001567 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001568 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001569}
1570
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571static void vmcs_clear(struct vmcs *vmcs)
1572{
1573 u64 phys_addr = __pa(vmcs);
1574 u8 error;
1575
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001576 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001577 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578 : "cc", "memory");
1579 if (error)
1580 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1581 vmcs, phys_addr);
1582}
1583
Nadav Har'Eld462b812011-05-24 15:26:10 +03001584static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1585{
1586 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001587 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1588 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001589 loaded_vmcs->cpu = -1;
1590 loaded_vmcs->launched = 0;
1591}
1592
Dongxiao Xu7725b892010-05-11 18:29:38 +08001593static void vmcs_load(struct vmcs *vmcs)
1594{
1595 u64 phys_addr = __pa(vmcs);
1596 u8 error;
1597
1598 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001599 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001600 : "cc", "memory");
1601 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001602 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001603 vmcs, phys_addr);
1604}
1605
Dave Young2965faa2015-09-09 15:38:55 -07001606#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001607/*
1608 * This bitmap is used to indicate whether the vmclear
1609 * operation is enabled on all cpus. All disabled by
1610 * default.
1611 */
1612static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1613
1614static inline void crash_enable_local_vmclear(int cpu)
1615{
1616 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1617}
1618
1619static inline void crash_disable_local_vmclear(int cpu)
1620{
1621 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1622}
1623
1624static inline int crash_local_vmclear_enabled(int cpu)
1625{
1626 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1627}
1628
1629static void crash_vmclear_local_loaded_vmcss(void)
1630{
1631 int cpu = raw_smp_processor_id();
1632 struct loaded_vmcs *v;
1633
1634 if (!crash_local_vmclear_enabled(cpu))
1635 return;
1636
1637 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1638 loaded_vmcss_on_cpu_link)
1639 vmcs_clear(v->vmcs);
1640}
1641#else
1642static inline void crash_enable_local_vmclear(int cpu) { }
1643static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001644#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001645
Nadav Har'Eld462b812011-05-24 15:26:10 +03001646static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001648 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001649 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650
Nadav Har'Eld462b812011-05-24 15:26:10 +03001651 if (loaded_vmcs->cpu != cpu)
1652 return; /* vcpu migration can race with cpu offline */
1653 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001655 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001656 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001657
1658 /*
1659 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1660 * is before setting loaded_vmcs->vcpu to -1 which is done in
1661 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1662 * then adds the vmcs into percpu list before it is deleted.
1663 */
1664 smp_wmb();
1665
Nadav Har'Eld462b812011-05-24 15:26:10 +03001666 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001667 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668}
1669
Nadav Har'Eld462b812011-05-24 15:26:10 +03001670static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001671{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001672 int cpu = loaded_vmcs->cpu;
1673
1674 if (cpu != -1)
1675 smp_call_function_single(cpu,
1676 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001677}
1678
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001679static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001680{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001681 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001682 return;
1683
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001684 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001685 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001686}
1687
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001688static inline void vpid_sync_vcpu_global(void)
1689{
1690 if (cpu_has_vmx_invvpid_global())
1691 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1692}
1693
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001694static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001695{
1696 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001697 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001698 else
1699 vpid_sync_vcpu_global();
1700}
1701
Sheng Yang14394422008-04-28 12:24:45 +08001702static inline void ept_sync_global(void)
1703{
1704 if (cpu_has_vmx_invept_global())
1705 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1706}
1707
1708static inline void ept_sync_context(u64 eptp)
1709{
Avi Kivity089d0342009-03-23 18:26:32 +02001710 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001711 if (cpu_has_vmx_invept_context())
1712 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1713 else
1714 ept_sync_global();
1715 }
1716}
1717
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001718static __always_inline void vmcs_check16(unsigned long field)
1719{
1720 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1721 "16-bit accessor invalid for 64-bit field");
1722 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1723 "16-bit accessor invalid for 64-bit high field");
1724 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1725 "16-bit accessor invalid for 32-bit high field");
1726 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1727 "16-bit accessor invalid for natural width field");
1728}
1729
1730static __always_inline void vmcs_check32(unsigned long field)
1731{
1732 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1733 "32-bit accessor invalid for 16-bit field");
1734 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1735 "32-bit accessor invalid for natural width field");
1736}
1737
1738static __always_inline void vmcs_check64(unsigned long field)
1739{
1740 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1741 "64-bit accessor invalid for 16-bit field");
1742 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1743 "64-bit accessor invalid for 64-bit high field");
1744 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1745 "64-bit accessor invalid for 32-bit field");
1746 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1747 "64-bit accessor invalid for natural width field");
1748}
1749
1750static __always_inline void vmcs_checkl(unsigned long field)
1751{
1752 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1753 "Natural width accessor invalid for 16-bit field");
1754 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1755 "Natural width accessor invalid for 64-bit field");
1756 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1757 "Natural width accessor invalid for 64-bit high field");
1758 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1759 "Natural width accessor invalid for 32-bit field");
1760}
1761
1762static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763{
Avi Kivity5e520e62011-05-15 10:13:12 -04001764 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765
Avi Kivity5e520e62011-05-15 10:13:12 -04001766 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1767 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001768 return value;
1769}
1770
Avi Kivity96304212011-05-15 10:13:13 -04001771static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001773 vmcs_check16(field);
1774 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775}
1776
Avi Kivity96304212011-05-15 10:13:13 -04001777static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001779 vmcs_check32(field);
1780 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781}
1782
Avi Kivity96304212011-05-15 10:13:13 -04001783static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001785 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001786#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001787 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001789 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790#endif
1791}
1792
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001793static __always_inline unsigned long vmcs_readl(unsigned long field)
1794{
1795 vmcs_checkl(field);
1796 return __vmcs_readl(field);
1797}
1798
Avi Kivitye52de1b2007-01-05 16:36:56 -08001799static noinline void vmwrite_error(unsigned long field, unsigned long value)
1800{
1801 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1802 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1803 dump_stack();
1804}
1805
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001806static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807{
1808 u8 error;
1809
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001810 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001811 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001812 if (unlikely(error))
1813 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814}
1815
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001816static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001818 vmcs_check16(field);
1819 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820}
1821
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001822static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001824 vmcs_check32(field);
1825 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826}
1827
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001828static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001830 vmcs_check64(field);
1831 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001832#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001834 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835#endif
1836}
1837
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001838static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001839{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001840 vmcs_checkl(field);
1841 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001842}
1843
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001844static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001845{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001846 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1847 "vmcs_clear_bits does not support 64-bit fields");
1848 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1849}
1850
1851static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1852{
1853 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1854 "vmcs_set_bits does not support 64-bit fields");
1855 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001856}
1857
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001858static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1859{
1860 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1861}
1862
Gleb Natapov2961e8762013-11-25 15:37:13 +02001863static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1864{
1865 vmcs_write32(VM_ENTRY_CONTROLS, val);
1866 vmx->vm_entry_controls_shadow = val;
1867}
1868
1869static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1870{
1871 if (vmx->vm_entry_controls_shadow != val)
1872 vm_entry_controls_init(vmx, val);
1873}
1874
1875static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1876{
1877 return vmx->vm_entry_controls_shadow;
1878}
1879
1880
1881static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1882{
1883 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1884}
1885
1886static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1887{
1888 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1889}
1890
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001891static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1892{
1893 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1894}
1895
Gleb Natapov2961e8762013-11-25 15:37:13 +02001896static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1897{
1898 vmcs_write32(VM_EXIT_CONTROLS, val);
1899 vmx->vm_exit_controls_shadow = val;
1900}
1901
1902static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1903{
1904 if (vmx->vm_exit_controls_shadow != val)
1905 vm_exit_controls_init(vmx, val);
1906}
1907
1908static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1909{
1910 return vmx->vm_exit_controls_shadow;
1911}
1912
1913
1914static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1915{
1916 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1917}
1918
1919static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1920{
1921 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1922}
1923
Avi Kivity2fb92db2011-04-27 19:42:18 +03001924static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1925{
1926 vmx->segment_cache.bitmask = 0;
1927}
1928
1929static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1930 unsigned field)
1931{
1932 bool ret;
1933 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1934
1935 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1936 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1937 vmx->segment_cache.bitmask = 0;
1938 }
1939 ret = vmx->segment_cache.bitmask & mask;
1940 vmx->segment_cache.bitmask |= mask;
1941 return ret;
1942}
1943
1944static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1945{
1946 u16 *p = &vmx->segment_cache.seg[seg].selector;
1947
1948 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1949 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1950 return *p;
1951}
1952
1953static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1954{
1955 ulong *p = &vmx->segment_cache.seg[seg].base;
1956
1957 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1958 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1959 return *p;
1960}
1961
1962static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1963{
1964 u32 *p = &vmx->segment_cache.seg[seg].limit;
1965
1966 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1967 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1968 return *p;
1969}
1970
1971static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1972{
1973 u32 *p = &vmx->segment_cache.seg[seg].ar;
1974
1975 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1976 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1977 return *p;
1978}
1979
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001980static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1981{
1982 u32 eb;
1983
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001984 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001985 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001986 if ((vcpu->guest_debug &
1987 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1988 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1989 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001990 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001991 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001992 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001993 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001994 if (vcpu->fpu_active)
1995 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001996
1997 /* When we are running a nested L2 guest and L1 specified for it a
1998 * certain exception bitmap, we must trap the same exceptions and pass
1999 * them to L1. When running L2, we will only handle the exceptions
2000 * specified above if L1 did not want them.
2001 */
2002 if (is_guest_mode(vcpu))
2003 eb |= get_vmcs12(vcpu)->exception_bitmap;
2004
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002005 vmcs_write32(EXCEPTION_BITMAP, eb);
2006}
2007
Ashok Raj70131292018-02-01 22:59:43 +01002008/*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01002009 * Check if MSR is intercepted for currently loaded MSR bitmap.
2010 */
2011static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2012{
2013 unsigned long *msr_bitmap;
2014 int f = sizeof(unsigned long);
2015
2016 if (!cpu_has_vmx_msr_bitmap())
2017 return true;
2018
2019 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2020
2021 if (msr <= 0x1fff) {
2022 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2023 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2024 msr &= 0x1fff;
2025 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2026 }
2027
2028 return true;
2029}
2030
2031/*
Ashok Raj70131292018-02-01 22:59:43 +01002032 * Check if MSR is intercepted for L01 MSR bitmap.
2033 */
2034static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2035{
2036 unsigned long *msr_bitmap;
2037 int f = sizeof(unsigned long);
2038
2039 if (!cpu_has_vmx_msr_bitmap())
2040 return true;
2041
2042 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2043
2044 if (msr <= 0x1fff) {
2045 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2046 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2047 msr &= 0x1fff;
2048 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2049 }
2050
2051 return true;
2052}
2053
Gleb Natapov2961e8762013-11-25 15:37:13 +02002054static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2055 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002056{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002057 vm_entry_controls_clearbit(vmx, entry);
2058 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002059}
2060
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002061static int find_msr(struct vmx_msrs *m, unsigned int msr)
2062{
2063 unsigned int i;
2064
2065 for (i = 0; i < m->nr; ++i) {
2066 if (m->val[i].index == msr)
2067 return i;
2068 }
2069 return -ENOENT;
2070}
2071
Avi Kivity61d2ef22010-04-28 16:40:38 +03002072static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2073{
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002074 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002075 struct msr_autoload *m = &vmx->msr_autoload;
2076
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002077 switch (msr) {
2078 case MSR_EFER:
2079 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002080 clear_atomic_switch_msr_special(vmx,
2081 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002082 VM_EXIT_LOAD_IA32_EFER);
2083 return;
2084 }
2085 break;
2086 case MSR_CORE_PERF_GLOBAL_CTRL:
2087 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002088 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002089 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2090 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2091 return;
2092 }
2093 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002094 }
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002095 i = find_msr(&m->guest, msr);
2096 if (i < 0)
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002097 goto skip_guest;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002098 --m->guest.nr;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002099 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002100 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002101
2102skip_guest:
2103 i = find_msr(&m->host, msr);
2104 if (i < 0)
2105 return;
2106
2107 --m->host.nr;
2108 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002109 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002110}
2111
Gleb Natapov2961e8762013-11-25 15:37:13 +02002112static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2113 unsigned long entry, unsigned long exit,
2114 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2115 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002116{
2117 vmcs_write64(guest_val_vmcs, guest_val);
2118 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002119 vm_entry_controls_setbit(vmx, entry);
2120 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002121}
2122
Avi Kivity61d2ef22010-04-28 16:40:38 +03002123static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002124 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002125{
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002126 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002127 struct msr_autoload *m = &vmx->msr_autoload;
2128
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002129 switch (msr) {
2130 case MSR_EFER:
2131 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002132 add_atomic_switch_msr_special(vmx,
2133 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002134 VM_EXIT_LOAD_IA32_EFER,
2135 GUEST_IA32_EFER,
2136 HOST_IA32_EFER,
2137 guest_val, host_val);
2138 return;
2139 }
2140 break;
2141 case MSR_CORE_PERF_GLOBAL_CTRL:
2142 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002143 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002144 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2145 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2146 GUEST_IA32_PERF_GLOBAL_CTRL,
2147 HOST_IA32_PERF_GLOBAL_CTRL,
2148 guest_val, host_val);
2149 return;
2150 }
2151 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002152 case MSR_IA32_PEBS_ENABLE:
2153 /* PEBS needs a quiescent period after being disabled (to write
2154 * a record). Disabling PEBS through VMX MSR swapping doesn't
2155 * provide that period, so a CPU could write host's record into
2156 * guest's memory.
2157 */
2158 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002159 }
2160
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002161 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002162 if (!entry_only)
2163 j = find_msr(&m->host, msr);
2164
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002165 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002166 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002167 "Can't add msr %x\n", msr);
2168 return;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002169 }
2170 if (i < 0) {
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002171 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002172 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002173 }
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002174 m->guest.val[i].index = msr;
2175 m->guest.val[i].value = guest_val;
2176
2177 if (entry_only)
2178 return;
2179
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002180 if (j < 0) {
2181 j = m->host.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002182 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002183 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002184 m->host.val[j].index = msr;
2185 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002186}
2187
Avi Kivity33ed6322007-05-02 16:54:03 +03002188static void reload_tss(void)
2189{
Avi Kivity33ed6322007-05-02 16:54:03 +03002190 /*
2191 * VT restores TR but not its size. Useless.
2192 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002193 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002194 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002195
Avi Kivityd3591922010-07-26 18:32:39 +03002196 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002197 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2198 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002199}
2200
Avi Kivity92c0d902009-10-29 11:00:16 +02002201static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002202{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002203 u64 guest_efer = vmx->vcpu.arch.efer;
2204 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002205
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002206 if (!enable_ept) {
2207 /*
2208 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2209 * host CPUID is more efficient than testing guest CPUID
2210 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2211 */
2212 if (boot_cpu_has(X86_FEATURE_SMEP))
2213 guest_efer |= EFER_NX;
2214 else if (!(guest_efer & EFER_NX))
2215 ignore_bits |= EFER_NX;
2216 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002217
Avi Kivity51c6cf62007-08-29 03:48:05 +03002218 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002219 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002220 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002221 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002222#ifdef CONFIG_X86_64
2223 ignore_bits |= EFER_LMA | EFER_LME;
2224 /* SCE is meaningful only in long mode on Intel */
2225 if (guest_efer & EFER_LMA)
2226 ignore_bits &= ~(u64)EFER_SCE;
2227#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002228
2229 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002230
2231 /*
2232 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2233 * On CPUs that support "load IA32_EFER", always switch EFER
2234 * atomically, since it's faster than switching it manually.
2235 */
2236 if (cpu_has_load_ia32_efer ||
2237 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002238 if (!(guest_efer & EFER_LMA))
2239 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002240 if (guest_efer != host_efer)
2241 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002242 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002243 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002244 } else {
2245 guest_efer &= ~ignore_bits;
2246 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002247
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002248 vmx->guest_msrs[efer_offset].data = guest_efer;
2249 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2250
2251 return true;
2252 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002253}
2254
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002255static unsigned long segment_base(u16 selector)
2256{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002257 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002258 struct desc_struct *d;
2259 unsigned long table_base;
2260 unsigned long v;
2261
2262 if (!(selector & ~3))
2263 return 0;
2264
Avi Kivityd3591922010-07-26 18:32:39 +03002265 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002266
2267 if (selector & 4) { /* from ldt */
2268 u16 ldt_selector = kvm_read_ldt();
2269
2270 if (!(ldt_selector & ~3))
2271 return 0;
2272
2273 table_base = segment_base(ldt_selector);
2274 }
2275 d = (struct desc_struct *)(table_base + (selector & ~7));
2276 v = get_desc_base(d);
2277#ifdef CONFIG_X86_64
2278 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2279 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2280#endif
2281 return v;
2282}
2283
2284static inline unsigned long kvm_read_tr_base(void)
2285{
2286 u16 tr;
2287 asm("str %0" : "=g"(tr));
2288 return segment_base(tr);
2289}
2290
Avi Kivity04d2cc72007-09-10 18:10:54 +03002291static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002292{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002293 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002294 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002295
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002296 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002297 return;
2298
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002299 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002300 /*
2301 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2302 * allow segment selectors with cpl > 0 or ti == 1.
2303 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002304 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002305 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002306 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002307 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002308 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002309 vmx->host_state.fs_reload_needed = 0;
2310 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002311 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002312 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002313 }
Avi Kivity9581d442010-10-19 16:46:55 +02002314 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002315 if (!(vmx->host_state.gs_sel & 7))
2316 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002317 else {
2318 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002319 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002320 }
2321
2322#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002323 savesegment(ds, vmx->host_state.ds_sel);
2324 savesegment(es, vmx->host_state.es_sel);
2325#endif
2326
2327#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002328 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2329 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2330#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002331 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2332 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002333#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002334
2335#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002336 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2337 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002338 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002339#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002340 if (boot_cpu_has(X86_FEATURE_MPX))
2341 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002342 for (i = 0; i < vmx->save_nmsrs; ++i)
2343 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002344 vmx->guest_msrs[i].data,
2345 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002346}
2347
Avi Kivitya9b21b62008-06-24 11:48:49 +03002348static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002349{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002350 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002351 return;
2352
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002353 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002354 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002355#ifdef CONFIG_X86_64
2356 if (is_long_mode(&vmx->vcpu))
2357 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2358#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002359 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002360 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002361#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002362 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002363#else
2364 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002365#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002366 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002367 if (vmx->host_state.fs_reload_needed)
2368 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002369#ifdef CONFIG_X86_64
2370 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2371 loadsegment(ds, vmx->host_state.ds_sel);
2372 loadsegment(es, vmx->host_state.es_sel);
2373 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002374#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002375 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002376#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002377 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002378#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002379 if (vmx->host_state.msr_host_bndcfgs)
2380 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002381 /*
2382 * If the FPU is not active (through the host task or
2383 * the guest vcpu), then restore the cr0.TS bit.
2384 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002385 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002386 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002387 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002388}
2389
Avi Kivitya9b21b62008-06-24 11:48:49 +03002390static void vmx_load_host_state(struct vcpu_vmx *vmx)
2391{
2392 preempt_disable();
2393 __vmx_load_host_state(vmx);
2394 preempt_enable();
2395}
2396
Feng Wu28b835d2015-09-18 22:29:54 +08002397static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2398{
2399 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2400 struct pi_desc old, new;
2401 unsigned int dest;
2402
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002403 /*
2404 * In case of hot-plug or hot-unplug, we may have to undo
2405 * vmx_vcpu_pi_put even if there is no assigned device. And we
2406 * always keep PI.NDST up to date for simplicity: it makes the
2407 * code easier, and CPU migration is not a fast path.
2408 */
2409 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002410 return;
2411
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002412 /*
2413 * First handle the simple case where no cmpxchg is necessary; just
2414 * allow posting non-urgent interrupts.
2415 *
2416 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2417 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2418 * expects the VCPU to be on the blocked_vcpu_list that matches
2419 * PI.NDST.
2420 */
2421 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2422 vcpu->cpu == cpu) {
2423 pi_clear_sn(pi_desc);
2424 return;
2425 }
2426
2427 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002428 do {
2429 old.control = new.control = pi_desc->control;
2430
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002431 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002432
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002433 if (x2apic_enabled())
2434 new.ndst = dest;
2435 else
2436 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002437
Feng Wu28b835d2015-09-18 22:29:54 +08002438 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002439 } while (cmpxchg64(&pi_desc->control, old.control,
2440 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002441}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002442
Peter Feinerc95ba922016-08-17 09:36:47 -07002443static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2444{
2445 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2446 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2447}
2448
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449/*
2450 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2451 * vcpu mutex is already taken.
2452 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002453static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002455 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002456 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002457 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002459 if (!vmm_exclusive)
2460 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002461 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002462 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002464 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002465 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002466 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002467
2468 /*
2469 * Read loaded_vmcs->cpu should be before fetching
2470 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2471 * See the comments in __loaded_vmcs_clear().
2472 */
2473 smp_rmb();
2474
Nadav Har'Eld462b812011-05-24 15:26:10 +03002475 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2476 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002477 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002478 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002479 }
2480
2481 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2482 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2483 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002484 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002485 }
2486
2487 if (!already_loaded) {
2488 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2489 unsigned long sysenter_esp;
2490
2491 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002492
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493 /*
2494 * Linux uses per-cpu TSS and GDT, so set these when switching
2495 * processors.
2496 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002497 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002498 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499
2500 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2501 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002502
Nadav Har'Eld462b812011-05-24 15:26:10 +03002503 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 }
Feng Wu28b835d2015-09-18 22:29:54 +08002505
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002506 /* Setup TSC multiplier */
2507 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002508 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2509 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002510
Feng Wu28b835d2015-09-18 22:29:54 +08002511 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002512 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002513}
2514
2515static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2516{
2517 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2518
2519 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002520 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2521 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002522 return;
2523
2524 /* Set SN when the vCPU is preempted */
2525 if (vcpu->preempted)
2526 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527}
2528
2529static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2530{
Feng Wu28b835d2015-09-18 22:29:54 +08002531 vmx_vcpu_pi_put(vcpu);
2532
Avi Kivitya9b21b62008-06-24 11:48:49 +03002533 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002534 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002535 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2536 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002537 kvm_cpu_vmxoff();
2538 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539}
2540
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002541static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2542{
Avi Kivity81231c62010-01-24 16:26:40 +02002543 ulong cr0;
2544
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002545 if (vcpu->fpu_active)
2546 return;
2547 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002548 cr0 = vmcs_readl(GUEST_CR0);
2549 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2550 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2551 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002552 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002553 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002554 if (is_guest_mode(vcpu))
2555 vcpu->arch.cr0_guest_owned_bits &=
2556 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002557 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002558}
2559
Avi Kivityedcafe32009-12-30 18:07:40 +02002560static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2561
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002562/*
2563 * Return the cr0 value that a nested guest would read. This is a combination
2564 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2565 * its hypervisor (cr0_read_shadow).
2566 */
2567static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2568{
2569 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2570 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2571}
2572static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2573{
2574 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2575 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2576}
2577
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002578static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2579{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002580 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2581 * set this *before* calling this function.
2582 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002583 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002584 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002585 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002586 vcpu->arch.cr0_guest_owned_bits = 0;
2587 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002588 if (is_guest_mode(vcpu)) {
2589 /*
2590 * L1's specified read shadow might not contain the TS bit,
2591 * so now that we turned on shadowing of this bit, we need to
2592 * set this bit of the shadow. Like in nested_vmx_run we need
2593 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2594 * up-to-date here because we just decached cr0.TS (and we'll
2595 * only update vmcs12->guest_cr0 on nested exit).
2596 */
2597 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2598 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2599 (vcpu->arch.cr0 & X86_CR0_TS);
2600 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2601 } else
2602 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002603}
2604
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2606{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002607 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002608
Avi Kivity6de12732011-03-07 12:51:22 +02002609 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2610 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2611 rflags = vmcs_readl(GUEST_RFLAGS);
2612 if (to_vmx(vcpu)->rmode.vm86_active) {
2613 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2614 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2615 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2616 }
2617 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002618 }
Avi Kivity6de12732011-03-07 12:51:22 +02002619 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620}
2621
2622static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2623{
Avi Kivity6de12732011-03-07 12:51:22 +02002624 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2625 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002626 if (to_vmx(vcpu)->rmode.vm86_active) {
2627 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002628 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002629 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 vmcs_writel(GUEST_RFLAGS, rflags);
2631}
2632
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002633static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2634{
2635 return to_vmx(vcpu)->guest_pkru;
2636}
2637
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002638static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002639{
2640 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2641 int ret = 0;
2642
2643 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002644 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002645 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002646 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002647
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002648 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002649}
2650
2651static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2652{
2653 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2654 u32 interruptibility = interruptibility_old;
2655
2656 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2657
Jan Kiszka48005f62010-02-19 19:38:07 +01002658 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002659 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002660 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002661 interruptibility |= GUEST_INTR_STATE_STI;
2662
2663 if ((interruptibility != interruptibility_old))
2664 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2665}
2666
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2668{
2669 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002671 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002673 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674
Glauber Costa2809f5d2009-05-12 16:21:05 -04002675 /* skipping an emulated instruction also counts */
2676 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677}
2678
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002679/*
2680 * KVM wants to inject page-faults which it got to the guest. This function
2681 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002682 */
Gleb Natapove011c662013-09-25 12:51:35 +03002683static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002684{
2685 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2686
Gleb Natapove011c662013-09-25 12:51:35 +03002687 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002688 return 0;
2689
Wanpeng Lia29fd272017-06-05 05:19:09 -07002690 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002691 vmcs_read32(VM_EXIT_INTR_INFO),
2692 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002693 return 1;
2694}
2695
Avi Kivity298101d2007-11-25 13:41:11 +02002696static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002697 bool has_error_code, u32 error_code,
2698 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002699{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002700 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002701 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002702
Gleb Natapove011c662013-09-25 12:51:35 +03002703 if (!reinject && is_guest_mode(vcpu) &&
2704 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002705 return;
2706
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002707 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002708 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002709 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2710 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002711
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002712 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002713 int inc_eip = 0;
2714 if (kvm_exception_is_soft(nr))
2715 inc_eip = vcpu->arch.event_exit_inst_len;
2716 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002717 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002718 return;
2719 }
2720
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002721 WARN_ON_ONCE(vmx->emulation_required);
2722
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002723 if (kvm_exception_is_soft(nr)) {
2724 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2725 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002726 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2727 } else
2728 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2729
2730 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002731}
2732
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002733static bool vmx_rdtscp_supported(void)
2734{
2735 return cpu_has_vmx_rdtscp();
2736}
2737
Mao, Junjiead756a12012-07-02 01:18:48 +00002738static bool vmx_invpcid_supported(void)
2739{
2740 return cpu_has_vmx_invpcid() && enable_ept;
2741}
2742
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743/*
Eddie Donga75beee2007-05-17 18:55:15 +03002744 * Swap MSR entry in host/guest MSR entry array.
2745 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002746static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002747{
Avi Kivity26bb0982009-09-07 11:14:12 +03002748 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002749
2750 tmp = vmx->guest_msrs[to];
2751 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2752 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002753}
2754
2755/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002756 * Set up the vmcs to automatically save and restore system
2757 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2758 * mode, as fiddling with msrs is very expensive.
2759 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002760static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002761{
Avi Kivity26bb0982009-09-07 11:14:12 +03002762 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002763
Eddie Donga75beee2007-05-17 18:55:15 +03002764 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002765#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002766 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002767 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002768 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002769 move_msr_up(vmx, index, save_nmsrs++);
2770 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002771 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002772 move_msr_up(vmx, index, save_nmsrs++);
2773 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002774 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002775 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002776 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002777 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002778 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002779 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002780 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002781 * if efer.sce is enabled.
2782 */
Brian Gerst8c065852010-07-17 09:03:26 -04002783 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002784 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002785 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002786 }
Eddie Donga75beee2007-05-17 18:55:15 +03002787#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002788 index = __find_msr_index(vmx, MSR_EFER);
2789 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002790 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002791
Avi Kivity26bb0982009-09-07 11:14:12 +03002792 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002793
Yang Zhang8d146952013-01-25 10:18:50 +08002794 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002795 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002796}
2797
2798/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002800 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2801 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002803static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804{
2805 u64 host_tsc, tsc_offset;
2806
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002807 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002809 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810}
2811
2812/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002813 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002815static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002817 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002818 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002819 * We're here if L1 chose not to trap WRMSR to TSC. According
2820 * to the spec, this should set L1's TSC; The offset that L1
2821 * set for L2 remains unchanged, and still needs to be added
2822 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002823 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002824 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002825 /* recalculate vmcs02.TSC_OFFSET: */
2826 vmcs12 = get_vmcs12(vcpu);
2827 vmcs_write64(TSC_OFFSET, offset +
2828 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2829 vmcs12->tsc_offset : 0));
2830 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002831 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2832 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002833 vmcs_write64(TSC_OFFSET, offset);
2834 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835}
2836
Nadav Har'El801d3422011-05-25 23:02:23 +03002837static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2838{
2839 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2840 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2841}
2842
2843/*
2844 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2845 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2846 * all guests if the "nested" module option is off, and can also be disabled
2847 * for a single guest by disabling its VMX cpuid bit.
2848 */
2849static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2850{
2851 return nested && guest_cpuid_has_vmx(vcpu);
2852}
2853
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002855 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2856 * returned for the various VMX controls MSRs when nested VMX is enabled.
2857 * The same values should also be used to verify that vmcs12 control fields are
2858 * valid during nested entry from L1 to L2.
2859 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2860 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2861 * bit in the high half is on if the corresponding bit in the control field
2862 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002863 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002864static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002865{
2866 /*
2867 * Note that as a general rule, the high half of the MSRs (bits in
2868 * the control fields which may be 1) should be initialized by the
2869 * intersection of the underlying hardware's MSR (i.e., features which
2870 * can be supported) and the list of features we want to expose -
2871 * because they are known to be properly supported in our code.
2872 * Also, usually, the low half of the MSRs (bits which must be 1) can
2873 * be set to 0, meaning that L1 may turn off any of these bits. The
2874 * reason is that if one of these bits is necessary, it will appear
2875 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2876 * fields of vmcs01 and vmcs02, will turn these bits off - and
2877 * nested_vmx_exit_handled() will not pass related exits to L1.
2878 * These rules have exceptions below.
2879 */
2880
2881 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002882 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002883 vmx->nested.nested_vmx_pinbased_ctls_low,
2884 vmx->nested.nested_vmx_pinbased_ctls_high);
2885 vmx->nested.nested_vmx_pinbased_ctls_low |=
2886 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2887 vmx->nested.nested_vmx_pinbased_ctls_high &=
2888 PIN_BASED_EXT_INTR_MASK |
2889 PIN_BASED_NMI_EXITING |
2890 PIN_BASED_VIRTUAL_NMIS;
2891 vmx->nested.nested_vmx_pinbased_ctls_high |=
2892 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002893 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002894 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002895 vmx->nested.nested_vmx_pinbased_ctls_high |=
2896 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002897
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002898 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002899 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002900 vmx->nested.nested_vmx_exit_ctls_low,
2901 vmx->nested.nested_vmx_exit_ctls_high);
2902 vmx->nested.nested_vmx_exit_ctls_low =
2903 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002904
Wincy Vanb9c237b2015-02-03 23:56:30 +08002905 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002906#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002907 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002908#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002909 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002910 vmx->nested.nested_vmx_exit_ctls_high |=
2911 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002912 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002913 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2914
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002915 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002916 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002917
Jan Kiszka2996fca2014-06-16 13:59:43 +02002918 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002919 vmx->nested.nested_vmx_true_exit_ctls_low =
2920 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002921 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2922
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002923 /* entry controls */
2924 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002925 vmx->nested.nested_vmx_entry_ctls_low,
2926 vmx->nested.nested_vmx_entry_ctls_high);
2927 vmx->nested.nested_vmx_entry_ctls_low =
2928 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2929 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002930#ifdef CONFIG_X86_64
2931 VM_ENTRY_IA32E_MODE |
2932#endif
2933 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002934 vmx->nested.nested_vmx_entry_ctls_high |=
2935 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002936 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002937 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002938
Jan Kiszka2996fca2014-06-16 13:59:43 +02002939 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002940 vmx->nested.nested_vmx_true_entry_ctls_low =
2941 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002942 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2943
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002944 /* cpu-based controls */
2945 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002946 vmx->nested.nested_vmx_procbased_ctls_low,
2947 vmx->nested.nested_vmx_procbased_ctls_high);
2948 vmx->nested.nested_vmx_procbased_ctls_low =
2949 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2950 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002951 CPU_BASED_VIRTUAL_INTR_PENDING |
2952 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002953 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2954 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2955 CPU_BASED_CR3_STORE_EXITING |
2956#ifdef CONFIG_X86_64
2957 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2958#endif
2959 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002960 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2961 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2962 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2963 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002964 /*
2965 * We can allow some features even when not supported by the
2966 * hardware. For example, L1 can specify an MSR bitmap - and we
2967 * can use it to avoid exits to L1 - even when L0 runs L2
2968 * without MSR bitmaps.
2969 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002970 vmx->nested.nested_vmx_procbased_ctls_high |=
2971 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002972 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002973
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002974 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002975 vmx->nested.nested_vmx_true_procbased_ctls_low =
2976 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002977 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2978
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002979 /* secondary cpu-based controls */
2980 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002981 vmx->nested.nested_vmx_secondary_ctls_low,
2982 vmx->nested.nested_vmx_secondary_ctls_high);
2983 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2984 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002985 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002986 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002987 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002988 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002989 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002990 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002991 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002992 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002993
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002994 if (enable_ept) {
2995 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002996 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002997 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002998 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002999 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
3000 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003001 if (cpu_has_vmx_ept_execute_only())
3002 vmx->nested.nested_vmx_ept_caps |=
3003 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003004 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04003005 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
3006 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003007 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08003008 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003009
Paolo Bonzinief697a72016-03-18 16:58:38 +01003010 /*
3011 * Old versions of KVM use the single-context version without
3012 * checking for support, so declare that it is supported even
3013 * though it is treated as global context. The alternative is
3014 * not failing the single-context invvpid, and it is worse.
3015 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07003016 if (enable_vpid)
3017 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01003018 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07003019 else
3020 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003021
Radim Krčmář0790ec12015-03-17 14:02:32 +01003022 if (enable_unrestricted_guest)
3023 vmx->nested.nested_vmx_secondary_ctls_high |=
3024 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3025
Jan Kiszkac18911a2013-03-13 16:06:41 +01003026 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003027 rdmsr(MSR_IA32_VMX_MISC,
3028 vmx->nested.nested_vmx_misc_low,
3029 vmx->nested.nested_vmx_misc_high);
3030 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
3031 vmx->nested.nested_vmx_misc_low |=
3032 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003033 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08003034 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003035}
3036
3037static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3038{
3039 /*
3040 * Bits 0 in high must be 0, and bits 1 in low must be 1.
3041 */
3042 return ((control & high) | low) == control;
3043}
3044
3045static inline u64 vmx_control_msr(u32 low, u32 high)
3046{
3047 return low | ((u64)high << 32);
3048}
3049
Jan Kiszkacae50132014-01-04 18:47:22 +01003050/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003051static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3052{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003053 struct vcpu_vmx *vmx = to_vmx(vcpu);
3054
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003055 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003056 case MSR_IA32_VMX_BASIC:
3057 /*
3058 * This MSR reports some information about VMX support. We
3059 * should return information about the VMX we emulate for the
3060 * guest, and the VMCS structure we give it - not about the
3061 * VMX support of the underlying hardware.
3062 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003063 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003064 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3065 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003066 if (cpu_has_vmx_basic_inout())
3067 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003068 break;
3069 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3070 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003071 *pdata = vmx_control_msr(
3072 vmx->nested.nested_vmx_pinbased_ctls_low,
3073 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003074 break;
3075 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003076 *pdata = vmx_control_msr(
3077 vmx->nested.nested_vmx_true_procbased_ctls_low,
3078 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02003079 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003080 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003081 *pdata = vmx_control_msr(
3082 vmx->nested.nested_vmx_procbased_ctls_low,
3083 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003084 break;
3085 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003086 *pdata = vmx_control_msr(
3087 vmx->nested.nested_vmx_true_exit_ctls_low,
3088 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003089 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003090 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003091 *pdata = vmx_control_msr(
3092 vmx->nested.nested_vmx_exit_ctls_low,
3093 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003094 break;
3095 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003096 *pdata = vmx_control_msr(
3097 vmx->nested.nested_vmx_true_entry_ctls_low,
3098 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003099 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003100 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003101 *pdata = vmx_control_msr(
3102 vmx->nested.nested_vmx_entry_ctls_low,
3103 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003104 break;
3105 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003106 *pdata = vmx_control_msr(
3107 vmx->nested.nested_vmx_misc_low,
3108 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003109 break;
3110 /*
3111 * These MSRs specify bits which the guest must keep fixed (on or off)
3112 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3113 * We picked the standard core2 setting.
3114 */
3115#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3116#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3117 case MSR_IA32_VMX_CR0_FIXED0:
3118 *pdata = VMXON_CR0_ALWAYSON;
3119 break;
3120 case MSR_IA32_VMX_CR0_FIXED1:
3121 *pdata = -1ULL;
3122 break;
3123 case MSR_IA32_VMX_CR4_FIXED0:
3124 *pdata = VMXON_CR4_ALWAYSON;
3125 break;
3126 case MSR_IA32_VMX_CR4_FIXED1:
3127 *pdata = -1ULL;
3128 break;
3129 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003130 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003131 break;
3132 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003133 *pdata = vmx_control_msr(
3134 vmx->nested.nested_vmx_secondary_ctls_low,
3135 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003136 break;
3137 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003138 *pdata = vmx->nested.nested_vmx_ept_caps |
3139 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003140 break;
3141 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003142 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003143 }
3144
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003145 return 0;
3146}
3147
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003148static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3149 uint64_t val)
3150{
3151 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3152
3153 return !(val & ~valid_bits);
3154}
3155
Tom Lendacky62d88fc2018-02-21 13:39:51 -06003156static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3157{
3158 return 1;
3159}
3160
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003161/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 * Reads an msr value (of 'msr_index') into 'pdata'.
3163 * Returns 0 on success, non-0 otherwise.
3164 * Assumes vcpu_load() was already called.
3165 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003166static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167{
Avi Kivity26bb0982009-09-07 11:14:12 +03003168 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003170 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003171#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003173 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 break;
3175 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003176 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003178 case MSR_KERNEL_GS_BASE:
3179 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003180 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003181 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003182#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003184 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303185 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003186 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003188 case MSR_IA32_SPEC_CTRL:
3189 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003190 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003191 return 1;
3192
3193 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3194 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003195 case MSR_IA32_ARCH_CAPABILITIES:
3196 if (!msr_info->host_initiated &&
3197 !guest_cpuid_has_arch_capabilities(vcpu))
3198 return 1;
3199 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3200 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003202 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 break;
3204 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003205 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 break;
3207 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003208 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003210 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003211 if (!kvm_mpx_supported() ||
3212 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003213 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003214 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003215 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003216 case MSR_IA32_MCG_EXT_CTL:
3217 if (!msr_info->host_initiated &&
3218 !(to_vmx(vcpu)->msr_ia32_feature_control &
3219 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003220 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003221 msr_info->data = vcpu->arch.mcg_ext_ctl;
3222 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003223 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003224 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003225 break;
3226 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3227 if (!nested_vmx_allowed(vcpu))
3228 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003229 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003230 case MSR_IA32_XSS:
3231 if (!vmx_xsaves_supported())
3232 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003233 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003234 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003235 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003236 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003237 return 1;
3238 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003240 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003241 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003242 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003243 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003245 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246 }
3247
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 return 0;
3249}
3250
Jan Kiszkacae50132014-01-04 18:47:22 +01003251static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3252
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253/*
3254 * Writes msr value into into the appropriate "register".
3255 * Returns 0 on success, non-0 otherwise.
3256 * Assumes vcpu_load() was already called.
3257 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003258static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003261 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003262 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003263 u32 msr_index = msr_info->index;
3264 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003265
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003267 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003268 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003269 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003270#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003272 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 vmcs_writel(GUEST_FS_BASE, data);
3274 break;
3275 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003276 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 vmcs_writel(GUEST_GS_BASE, data);
3278 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003279 case MSR_KERNEL_GS_BASE:
3280 vmx_load_host_state(vmx);
3281 vmx->msr_guest_kernel_gs_base = data;
3282 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283#endif
3284 case MSR_IA32_SYSENTER_CS:
3285 vmcs_write32(GUEST_SYSENTER_CS, data);
3286 break;
3287 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003288 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289 break;
3290 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003291 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003293 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003294 if (!kvm_mpx_supported() ||
3295 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003296 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003297 if (is_noncanonical_address(data & PAGE_MASK) ||
3298 (data & MSR_IA32_BNDCFGS_RSVD))
3299 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003300 vmcs_write64(GUEST_BNDCFGS, data);
3301 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303302 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003303 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003305 case MSR_IA32_SPEC_CTRL:
3306 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003307 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003308 return 1;
3309
3310 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003311 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003312 return 1;
3313
3314 vmx->spec_ctrl = data;
3315
3316 if (!data)
3317 break;
3318
3319 /*
3320 * For non-nested:
3321 * When it's written (to non-zero) for the first time, pass
3322 * it through.
3323 *
3324 * For nested:
3325 * The handling of the MSR bitmap for L2 guests is done in
3326 * nested_vmx_merge_msr_bitmap. We should not touch the
3327 * vmcs02.msr_bitmap here since it gets completely overwritten
3328 * in the merging. We update the vmcs01 here for L1 as well
3329 * since it will end up touching the MSR anyway now.
3330 */
3331 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3332 MSR_IA32_SPEC_CTRL,
3333 MSR_TYPE_RW);
3334 break;
Ashok Raj70131292018-02-01 22:59:43 +01003335 case MSR_IA32_PRED_CMD:
3336 if (!msr_info->host_initiated &&
3337 !guest_cpuid_has_ibpb(vcpu))
3338 return 1;
3339
3340 if (data & ~PRED_CMD_IBPB)
3341 return 1;
3342
3343 if (!data)
3344 break;
3345
3346 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3347
3348 /*
3349 * For non-nested:
3350 * When it's written (to non-zero) for the first time, pass
3351 * it through.
3352 *
3353 * For nested:
3354 * The handling of the MSR bitmap for L2 guests is done in
3355 * nested_vmx_merge_msr_bitmap. We should not touch the
3356 * vmcs02.msr_bitmap here since it gets completely overwritten
3357 * in the merging.
3358 */
3359 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3360 MSR_TYPE_W);
3361 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003362 case MSR_IA32_ARCH_CAPABILITIES:
3363 if (!msr_info->host_initiated)
3364 return 1;
3365 vmx->arch_capabilities = data;
3366 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003367 case MSR_IA32_CR_PAT:
3368 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003369 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3370 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003371 vmcs_write64(GUEST_IA32_PAT, data);
3372 vcpu->arch.pat = data;
3373 break;
3374 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003375 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003376 break;
Will Auldba904632012-11-29 12:42:50 -08003377 case MSR_IA32_TSC_ADJUST:
3378 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003379 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003380 case MSR_IA32_MCG_EXT_CTL:
3381 if ((!msr_info->host_initiated &&
3382 !(to_vmx(vcpu)->msr_ia32_feature_control &
3383 FEATURE_CONTROL_LMCE)) ||
3384 (data & ~MCG_EXT_CTL_LMCE_EN))
3385 return 1;
3386 vcpu->arch.mcg_ext_ctl = data;
3387 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003388 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003389 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003390 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003391 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3392 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003393 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003394 if (msr_info->host_initiated && data == 0)
3395 vmx_leave_nested(vcpu);
3396 break;
3397 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3398 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003399 case MSR_IA32_XSS:
3400 if (!vmx_xsaves_supported())
3401 return 1;
3402 /*
3403 * The only supported bit as of Skylake is bit 8, but
3404 * it is not supported on KVM.
3405 */
3406 if (data != 0)
3407 return 1;
3408 vcpu->arch.ia32_xss = data;
3409 if (vcpu->arch.ia32_xss != host_xss)
3410 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04003411 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08003412 else
3413 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3414 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003415 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003416 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003417 return 1;
3418 /* Check reserved bit, higher 32 bits should be zero */
3419 if ((data >> 32) != 0)
3420 return 1;
3421 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003423 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003424 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003425 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003426 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003427 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3428 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003429 ret = kvm_set_shared_msr(msr->index, msr->data,
3430 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003431 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003432 if (ret)
3433 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003434 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003435 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003437 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 }
3439
Eddie Dong2cc51562007-05-21 07:28:09 +03003440 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441}
3442
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003443static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003445 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3446 switch (reg) {
3447 case VCPU_REGS_RSP:
3448 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3449 break;
3450 case VCPU_REGS_RIP:
3451 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3452 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003453 case VCPU_EXREG_PDPTR:
3454 if (enable_ept)
3455 ept_save_pdptrs(vcpu);
3456 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003457 default:
3458 break;
3459 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460}
3461
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462static __init int cpu_has_kvm_support(void)
3463{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003464 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465}
3466
3467static __init int vmx_disabled_by_bios(void)
3468{
3469 u64 msr;
3470
3471 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003472 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003473 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003474 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3475 && tboot_enabled())
3476 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003477 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003478 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003479 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003480 && !tboot_enabled()) {
3481 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003482 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003483 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003484 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003485 /* launched w/o TXT and VMX disabled */
3486 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3487 && !tboot_enabled())
3488 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003489 }
3490
3491 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492}
3493
Dongxiao Xu7725b892010-05-11 18:29:38 +08003494static void kvm_cpu_vmxon(u64 addr)
3495{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003496 intel_pt_handle_vmx(1);
3497
Dongxiao Xu7725b892010-05-11 18:29:38 +08003498 asm volatile (ASM_VMX_VMXON_RAX
3499 : : "a"(&addr), "m"(addr)
3500 : "memory", "cc");
3501}
3502
Radim Krčmář13a34e02014-08-28 15:13:03 +02003503static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504{
3505 int cpu = raw_smp_processor_id();
3506 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003507 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003509 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003510 return -EBUSY;
3511
Nadav Har'Eld462b812011-05-24 15:26:10 +03003512 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003513 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3514 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003515
3516 /*
3517 * Now we can enable the vmclear operation in kdump
3518 * since the loaded_vmcss_on_cpu list on this cpu
3519 * has been initialized.
3520 *
3521 * Though the cpu is not in VMX operation now, there
3522 * is no problem to enable the vmclear operation
3523 * for the loaded_vmcss_on_cpu list is empty!
3524 */
3525 crash_enable_local_vmclear(cpu);
3526
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003528
3529 test_bits = FEATURE_CONTROL_LOCKED;
3530 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3531 if (tboot_enabled())
3532 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3533
3534 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003535 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003536 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3537 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003538 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003539
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003540 if (vmm_exclusive) {
3541 kvm_cpu_vmxon(phys_addr);
3542 ept_sync_global();
3543 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003544
Christoph Lameter89cbc762014-08-17 12:30:40 -05003545 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003546
Alexander Graf10474ae2009-09-15 11:37:46 +02003547 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548}
3549
Nadav Har'Eld462b812011-05-24 15:26:10 +03003550static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003551{
3552 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003553 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003554
Nadav Har'Eld462b812011-05-24 15:26:10 +03003555 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3556 loaded_vmcss_on_cpu_link)
3557 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003558}
3559
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003560
3561/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3562 * tricks.
3563 */
3564static void kvm_cpu_vmxoff(void)
3565{
3566 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003567
3568 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003569}
3570
Radim Krčmář13a34e02014-08-28 15:13:03 +02003571static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003573 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003574 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003575 kvm_cpu_vmxoff();
3576 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003577 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578}
3579
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003580static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003581 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003582{
3583 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003584 u32 ctl = ctl_min | ctl_opt;
3585
3586 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3587
3588 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3589 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3590
3591 /* Ensure minimum (required) set of control bits are supported. */
3592 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003593 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003594
3595 *result = ctl;
3596 return 0;
3597}
3598
Avi Kivity110312c2010-12-21 12:54:20 +02003599static __init bool allow_1_setting(u32 msr, u32 ctl)
3600{
3601 u32 vmx_msr_low, vmx_msr_high;
3602
3603 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3604 return vmx_msr_high & ctl;
3605}
3606
Yang, Sheng002c7f72007-07-31 14:23:01 +03003607static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003608{
3609 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003610 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003611 u32 _pin_based_exec_control = 0;
3612 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003613 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003614 u32 _vmexit_control = 0;
3615 u32 _vmentry_control = 0;
3616
Raghavendra K T10166742012-02-07 23:19:20 +05303617 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003618#ifdef CONFIG_X86_64
3619 CPU_BASED_CR8_LOAD_EXITING |
3620 CPU_BASED_CR8_STORE_EXITING |
3621#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003622 CPU_BASED_CR3_LOAD_EXITING |
3623 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003624 CPU_BASED_USE_IO_BITMAPS |
3625 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003626 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003627 CPU_BASED_MWAIT_EXITING |
3628 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003629 CPU_BASED_INVLPG_EXITING |
3630 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003631
Sheng Yangf78e0e22007-10-29 09:40:42 +08003632 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003633 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003634 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003635 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3636 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003637 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003638#ifdef CONFIG_X86_64
3639 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3640 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3641 ~CPU_BASED_CR8_STORE_EXITING;
3642#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003643 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003644 min2 = 0;
3645 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003646 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003647 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003648 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003649 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003650 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003651 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003652 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003653 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003654 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003655 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003656 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003657 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003658 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003659 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003660 if (adjust_vmx_controls(min2, opt2,
3661 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003662 &_cpu_based_2nd_exec_control) < 0)
3663 return -EIO;
3664 }
3665#ifndef CONFIG_X86_64
3666 if (!(_cpu_based_2nd_exec_control &
3667 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3668 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3669#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003670
3671 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3672 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003673 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003674 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3675 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003676
Sheng Yangd56f5462008-04-25 10:13:16 +08003677 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003678 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3679 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003680 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3681 CPU_BASED_CR3_STORE_EXITING |
3682 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003683 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3684 vmx_capability.ept, vmx_capability.vpid);
3685 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003686
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003687 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003688#ifdef CONFIG_X86_64
3689 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3690#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003691 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003692 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003693 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3694 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003695 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003696
Yang Zhang01e439b2013-04-11 19:25:12 +08003697 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003698 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3699 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003700 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3701 &_pin_based_exec_control) < 0)
3702 return -EIO;
3703
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003704 if (cpu_has_broken_vmx_preemption_timer())
3705 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003706 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003707 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003708 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3709
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003710 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003711 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003712 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3713 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003714 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003716 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003717
3718 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3719 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003720 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003721
3722#ifdef CONFIG_X86_64
3723 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3724 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003725 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003726#endif
3727
3728 /* Require Write-Back (WB) memory type for VMCS accesses. */
3729 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003730 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003731
Yang, Sheng002c7f72007-07-31 14:23:01 +03003732 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003733 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003734 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003735 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003736
Yang, Sheng002c7f72007-07-31 14:23:01 +03003737 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3738 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003739 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003740 vmcs_conf->vmexit_ctrl = _vmexit_control;
3741 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003742
Avi Kivity110312c2010-12-21 12:54:20 +02003743 cpu_has_load_ia32_efer =
3744 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3745 VM_ENTRY_LOAD_IA32_EFER)
3746 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3747 VM_EXIT_LOAD_IA32_EFER);
3748
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003749 cpu_has_load_perf_global_ctrl =
3750 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3751 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3752 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3753 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3754
3755 /*
3756 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003757 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003758 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3759 *
3760 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3761 *
3762 * AAK155 (model 26)
3763 * AAP115 (model 30)
3764 * AAT100 (model 37)
3765 * BC86,AAY89,BD102 (model 44)
3766 * BA97 (model 46)
3767 *
3768 */
3769 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3770 switch (boot_cpu_data.x86_model) {
3771 case 26:
3772 case 30:
3773 case 37:
3774 case 44:
3775 case 46:
3776 cpu_has_load_perf_global_ctrl = false;
3777 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3778 "does not work properly. Using workaround\n");
3779 break;
3780 default:
3781 break;
3782 }
3783 }
3784
Borislav Petkov782511b2016-04-04 22:25:03 +02003785 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003786 rdmsrl(MSR_IA32_XSS, host_xss);
3787
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003788 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003789}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003790
3791static struct vmcs *alloc_vmcs_cpu(int cpu)
3792{
3793 int node = cpu_to_node(cpu);
3794 struct page *pages;
3795 struct vmcs *vmcs;
3796
Vlastimil Babka96db8002015-09-08 15:03:50 -07003797 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798 if (!pages)
3799 return NULL;
3800 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003801 memset(vmcs, 0, vmcs_config.size);
3802 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803 return vmcs;
3804}
3805
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806static void free_vmcs(struct vmcs *vmcs)
3807{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003808 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003809}
3810
Nadav Har'Eld462b812011-05-24 15:26:10 +03003811/*
3812 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3813 */
3814static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3815{
3816 if (!loaded_vmcs->vmcs)
3817 return;
3818 loaded_vmcs_clear(loaded_vmcs);
3819 free_vmcs(loaded_vmcs->vmcs);
3820 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003821 if (loaded_vmcs->msr_bitmap)
3822 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003823 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003824}
3825
Paolo Bonziniff546f92018-01-11 12:16:15 +01003826static struct vmcs *alloc_vmcs(void)
3827{
3828 return alloc_vmcs_cpu(raw_smp_processor_id());
3829}
3830
3831static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3832{
3833 loaded_vmcs->vmcs = alloc_vmcs();
3834 if (!loaded_vmcs->vmcs)
3835 return -ENOMEM;
3836
3837 loaded_vmcs->shadow_vmcs = NULL;
3838 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003839
3840 if (cpu_has_vmx_msr_bitmap()) {
3841 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3842 if (!loaded_vmcs->msr_bitmap)
3843 goto out_vmcs;
3844 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3845 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003846 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003847
3848out_vmcs:
3849 free_loaded_vmcs(loaded_vmcs);
3850 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003851}
3852
Sam Ravnborg39959582007-06-01 00:47:13 -07003853static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854{
3855 int cpu;
3856
Zachary Amsden3230bb42009-09-29 11:38:37 -10003857 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003859 per_cpu(vmxarea, cpu) = NULL;
3860 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861}
3862
Bandan Dasfe2b2012014-04-21 15:20:14 -04003863static void init_vmcs_shadow_fields(void)
3864{
3865 int i, j;
3866
3867 /* No checks for read only fields yet */
3868
3869 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3870 switch (shadow_read_write_fields[i]) {
3871 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003872 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003873 continue;
3874 break;
3875 default:
3876 break;
3877 }
3878
3879 if (j < i)
3880 shadow_read_write_fields[j] =
3881 shadow_read_write_fields[i];
3882 j++;
3883 }
3884 max_shadow_read_write_fields = j;
3885
3886 /* shadowed fields guest access without vmexit */
3887 for (i = 0; i < max_shadow_read_write_fields; i++) {
3888 clear_bit(shadow_read_write_fields[i],
3889 vmx_vmwrite_bitmap);
3890 clear_bit(shadow_read_write_fields[i],
3891 vmx_vmread_bitmap);
3892 }
3893 for (i = 0; i < max_shadow_read_only_fields; i++)
3894 clear_bit(shadow_read_only_fields[i],
3895 vmx_vmread_bitmap);
3896}
3897
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898static __init int alloc_kvm_area(void)
3899{
3900 int cpu;
3901
Zachary Amsden3230bb42009-09-29 11:38:37 -10003902 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003903 struct vmcs *vmcs;
3904
3905 vmcs = alloc_vmcs_cpu(cpu);
3906 if (!vmcs) {
3907 free_kvm_area();
3908 return -ENOMEM;
3909 }
3910
3911 per_cpu(vmxarea, cpu) = vmcs;
3912 }
3913 return 0;
3914}
3915
Gleb Natapov14168782013-01-21 15:36:49 +02003916static bool emulation_required(struct kvm_vcpu *vcpu)
3917{
3918 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3919}
3920
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003921static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003922 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003924 if (!emulate_invalid_guest_state) {
3925 /*
3926 * CS and SS RPL should be equal during guest entry according
3927 * to VMX spec, but in reality it is not always so. Since vcpu
3928 * is in the middle of the transition from real mode to
3929 * protected mode it is safe to assume that RPL 0 is a good
3930 * default value.
3931 */
3932 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003933 save->selector &= ~SEGMENT_RPL_MASK;
3934 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003935 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003937 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938}
3939
3940static void enter_pmode(struct kvm_vcpu *vcpu)
3941{
3942 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003943 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944
Gleb Natapovd99e4152012-12-20 16:57:45 +02003945 /*
3946 * Update real mode segment cache. It may be not up-to-date if sement
3947 * register was written while vcpu was in a guest mode.
3948 */
3949 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3951 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3952 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3953 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3954 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3955
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003956 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957
Avi Kivity2fb92db2011-04-27 19:42:18 +03003958 vmx_segment_cache_clear(vmx);
3959
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003960 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961
3962 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003963 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3964 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965 vmcs_writel(GUEST_RFLAGS, flags);
3966
Rusty Russell66aee912007-07-17 23:34:16 +10003967 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3968 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969
3970 update_exception_bitmap(vcpu);
3971
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003972 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3973 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3974 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3975 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3976 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3977 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978}
3979
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003980static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981{
Mathias Krause772e0312012-08-30 01:30:19 +02003982 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003983 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984
Gleb Natapovd99e4152012-12-20 16:57:45 +02003985 var.dpl = 0x3;
3986 if (seg == VCPU_SREG_CS)
3987 var.type = 0x3;
3988
3989 if (!emulate_invalid_guest_state) {
3990 var.selector = var.base >> 4;
3991 var.base = var.base & 0xffff0;
3992 var.limit = 0xffff;
3993 var.g = 0;
3994 var.db = 0;
3995 var.present = 1;
3996 var.s = 1;
3997 var.l = 0;
3998 var.unusable = 0;
3999 var.type = 0x3;
4000 var.avl = 0;
4001 if (save->base & 0xf)
4002 printk_once(KERN_WARNING "kvm: segment base is not "
4003 "paragraph aligned when entering "
4004 "protected mode (seg=%d)", seg);
4005 }
4006
4007 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05004008 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004009 vmcs_write32(sf->limit, var.limit);
4010 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004011}
4012
4013static void enter_rmode(struct kvm_vcpu *vcpu)
4014{
4015 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004016 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004017
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4019 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4022 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004023 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4024 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004025
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004026 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004027
Gleb Natapov776e58e2011-03-13 12:34:27 +02004028 /*
4029 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004030 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004031 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004032 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004033 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4034 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004035
Avi Kivity2fb92db2011-04-27 19:42:18 +03004036 vmx_segment_cache_clear(vmx);
4037
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004038 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004039 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4041
4042 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004043 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004045 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046
4047 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004048 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 update_exception_bitmap(vcpu);
4050
Gleb Natapovd99e4152012-12-20 16:57:45 +02004051 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4052 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4053 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4054 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4055 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4056 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004057
Eddie Dong8668a3c2007-10-10 14:26:45 +08004058 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059}
4060
Amit Shah401d10d2009-02-20 22:53:37 +05304061static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4062{
4063 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004064 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4065
4066 if (!msr)
4067 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304068
Avi Kivity44ea2b12009-09-06 15:55:37 +03004069 /*
4070 * Force kernel_gs_base reloading before EFER changes, as control
4071 * of this msr depends on is_long_mode().
4072 */
4073 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004074 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304075 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004076 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304077 msr->data = efer;
4078 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004079 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304080
4081 msr->data = efer & ~EFER_LME;
4082 }
4083 setup_msrs(vmx);
4084}
4085
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004086#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004087
4088static void enter_lmode(struct kvm_vcpu *vcpu)
4089{
4090 u32 guest_tr_ar;
4091
Avi Kivity2fb92db2011-04-27 19:42:18 +03004092 vmx_segment_cache_clear(to_vmx(vcpu));
4093
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004095 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004096 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4097 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004099 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4100 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 }
Avi Kivityda38f432010-07-06 11:30:49 +03004102 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103}
4104
4105static void exit_lmode(struct kvm_vcpu *vcpu)
4106{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004107 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004108 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004109}
4110
4111#endif
4112
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004113static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004114{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004115 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004116 if (enable_ept) {
4117 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4118 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004119 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004120 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004121}
4122
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004123static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4124{
4125 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4126}
4127
Jim Mattson8386ff52017-03-16 13:53:59 -07004128static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4129{
4130 if (enable_ept)
4131 vmx_flush_tlb(vcpu);
4132}
4133
Avi Kivitye8467fd2009-12-29 18:43:06 +02004134static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4135{
4136 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4137
4138 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4139 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4140}
4141
Avi Kivityaff48ba2010-12-05 18:56:11 +02004142static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4143{
4144 if (enable_ept && is_paging(vcpu))
4145 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4146 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4147}
4148
Anthony Liguori25c4c272007-04-27 09:29:21 +03004149static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004150{
Avi Kivityfc78f512009-12-07 12:16:48 +02004151 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4152
4153 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4154 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004155}
4156
Sheng Yang14394422008-04-28 12:24:45 +08004157static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4158{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004159 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4160
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004161 if (!test_bit(VCPU_EXREG_PDPTR,
4162 (unsigned long *)&vcpu->arch.regs_dirty))
4163 return;
4164
Sheng Yang14394422008-04-28 12:24:45 +08004165 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004166 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4167 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4168 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4169 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004170 }
4171}
4172
Avi Kivity8f5d5492009-05-31 18:41:29 +03004173static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4174{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004175 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4176
Avi Kivity8f5d5492009-05-31 18:41:29 +03004177 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004178 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4179 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4180 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4181 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004182 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004183
4184 __set_bit(VCPU_EXREG_PDPTR,
4185 (unsigned long *)&vcpu->arch.regs_avail);
4186 __set_bit(VCPU_EXREG_PDPTR,
4187 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004188}
4189
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004190static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004191
4192static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4193 unsigned long cr0,
4194 struct kvm_vcpu *vcpu)
4195{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004196 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4197 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004198 if (!(cr0 & X86_CR0_PG)) {
4199 /* From paging/starting to nonpaging */
4200 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004201 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004202 (CPU_BASED_CR3_LOAD_EXITING |
4203 CPU_BASED_CR3_STORE_EXITING));
4204 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004205 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004206 } else if (!is_paging(vcpu)) {
4207 /* From nonpaging to paging */
4208 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004209 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004210 ~(CPU_BASED_CR3_LOAD_EXITING |
4211 CPU_BASED_CR3_STORE_EXITING));
4212 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004213 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004214 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004215
4216 if (!(cr0 & X86_CR0_WP))
4217 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004218}
4219
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4221{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004222 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004223 unsigned long hw_cr0;
4224
Gleb Natapov50378782013-02-04 16:00:28 +02004225 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004226 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004227 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004228 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004229 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004230
Gleb Natapov218e7632013-01-21 15:36:45 +02004231 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4232 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004233
Gleb Natapov218e7632013-01-21 15:36:45 +02004234 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4235 enter_rmode(vcpu);
4236 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004238#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004239 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10004240 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004241 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10004242 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243 exit_lmode(vcpu);
4244 }
4245#endif
4246
Avi Kivity089d0342009-03-23 18:26:32 +02004247 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004248 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4249
Avi Kivity02daab22009-12-30 12:40:26 +02004250 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004251 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004252
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004254 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004255 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004256
4257 /* depends on vcpu->arch.cr0 to be set to a new value */
4258 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004259}
4260
Sheng Yang14394422008-04-28 12:24:45 +08004261static u64 construct_eptp(unsigned long root_hpa)
4262{
4263 u64 eptp;
4264
4265 /* TODO write the value reading from MSR */
4266 eptp = VMX_EPT_DEFAULT_MT |
4267 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004268 if (enable_ept_ad_bits)
4269 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004270 eptp |= (root_hpa & PAGE_MASK);
4271
4272 return eptp;
4273}
4274
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4276{
Sheng Yang14394422008-04-28 12:24:45 +08004277 unsigned long guest_cr3;
4278 u64 eptp;
4279
4280 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004281 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004282 eptp = construct_eptp(cr3);
4283 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004284 if (is_paging(vcpu) || is_guest_mode(vcpu))
4285 guest_cr3 = kvm_read_cr3(vcpu);
4286 else
4287 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004288 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004289 }
4290
Sheng Yang2384d2b2008-01-17 15:14:33 +08004291 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004292 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293}
4294
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004295static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004296{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004297 /*
4298 * Pass through host's Machine Check Enable value to hw_cr4, which
4299 * is in force while we are in guest mode. Do not let guests control
4300 * this bit, even if host CR4.MCE == 0.
4301 */
4302 unsigned long hw_cr4 =
4303 (cr4_read_shadow() & X86_CR4_MCE) |
4304 (cr4 & ~X86_CR4_MCE) |
4305 (to_vmx(vcpu)->rmode.vm86_active ?
4306 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004307
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004308 if (cr4 & X86_CR4_VMXE) {
4309 /*
4310 * To use VMXON (and later other VMX instructions), a guest
4311 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4312 * So basically the check on whether to allow nested VMX
4313 * is here.
4314 */
4315 if (!nested_vmx_allowed(vcpu))
4316 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004317 }
4318 if (to_vmx(vcpu)->nested.vmxon &&
4319 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004320 return 1;
4321
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004322 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004323 if (enable_ept) {
4324 if (!is_paging(vcpu)) {
4325 hw_cr4 &= ~X86_CR4_PAE;
4326 hw_cr4 |= X86_CR4_PSE;
4327 } else if (!(cr4 & X86_CR4_PAE)) {
4328 hw_cr4 &= ~X86_CR4_PAE;
4329 }
4330 }
Sheng Yang14394422008-04-28 12:24:45 +08004331
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004332 if (!enable_unrestricted_guest && !is_paging(vcpu))
4333 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004334 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4335 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4336 * to be manually disabled when guest switches to non-paging
4337 * mode.
4338 *
4339 * If !enable_unrestricted_guest, the CPU is always running
4340 * with CR0.PG=1 and CR4 needs to be modified.
4341 * If enable_unrestricted_guest, the CPU automatically
4342 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004343 */
Huaitong Handdba2622016-03-22 16:51:15 +08004344 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004345
Sheng Yang14394422008-04-28 12:24:45 +08004346 vmcs_writel(CR4_READ_SHADOW, cr4);
4347 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004348 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004349}
4350
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351static void vmx_get_segment(struct kvm_vcpu *vcpu,
4352 struct kvm_segment *var, int seg)
4353{
Avi Kivitya9179492011-01-03 14:28:52 +02004354 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004355 u32 ar;
4356
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004357 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004358 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004359 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004360 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004361 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004362 var->base = vmx_read_guest_seg_base(vmx, seg);
4363 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4364 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004365 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004366 var->base = vmx_read_guest_seg_base(vmx, seg);
4367 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4368 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4369 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004370 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371 var->type = ar & 15;
4372 var->s = (ar >> 4) & 1;
4373 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004374 /*
4375 * Some userspaces do not preserve unusable property. Since usable
4376 * segment has to be present according to VMX spec we can use present
4377 * property to amend userspace bug by making unusable segment always
4378 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4379 * segment as unusable.
4380 */
4381 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004382 var->avl = (ar >> 12) & 1;
4383 var->l = (ar >> 13) & 1;
4384 var->db = (ar >> 14) & 1;
4385 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386}
4387
Avi Kivitya9179492011-01-03 14:28:52 +02004388static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4389{
Avi Kivitya9179492011-01-03 14:28:52 +02004390 struct kvm_segment s;
4391
4392 if (to_vmx(vcpu)->rmode.vm86_active) {
4393 vmx_get_segment(vcpu, &s, seg);
4394 return s.base;
4395 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004396 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004397}
4398
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004399static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004400{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004401 struct vcpu_vmx *vmx = to_vmx(vcpu);
4402
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004403 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004404 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004405 else {
4406 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004407 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004408 }
Avi Kivity69c73022011-03-07 15:26:44 +02004409}
4410
Avi Kivity653e3102007-05-07 10:55:37 +03004411static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413 u32 ar;
4414
Avi Kivityf0495f92012-06-07 17:06:10 +03004415 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004416 ar = 1 << 16;
4417 else {
4418 ar = var->type & 15;
4419 ar |= (var->s & 1) << 4;
4420 ar |= (var->dpl & 3) << 5;
4421 ar |= (var->present & 1) << 7;
4422 ar |= (var->avl & 1) << 12;
4423 ar |= (var->l & 1) << 13;
4424 ar |= (var->db & 1) << 14;
4425 ar |= (var->g & 1) << 15;
4426 }
Avi Kivity653e3102007-05-07 10:55:37 +03004427
4428 return ar;
4429}
4430
4431static void vmx_set_segment(struct kvm_vcpu *vcpu,
4432 struct kvm_segment *var, int seg)
4433{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004434 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004435 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004436
Avi Kivity2fb92db2011-04-27 19:42:18 +03004437 vmx_segment_cache_clear(vmx);
4438
Gleb Natapov1ecd50a92012-12-12 19:10:54 +02004439 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4440 vmx->rmode.segs[seg] = *var;
4441 if (seg == VCPU_SREG_TR)
4442 vmcs_write16(sf->selector, var->selector);
4443 else if (var->s)
4444 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004445 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004446 }
Gleb Natapov1ecd50a92012-12-12 19:10:54 +02004447
Avi Kivity653e3102007-05-07 10:55:37 +03004448 vmcs_writel(sf->base, var->base);
4449 vmcs_write32(sf->limit, var->limit);
4450 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004451
4452 /*
4453 * Fix the "Accessed" bit in AR field of segment registers for older
4454 * qemu binaries.
4455 * IA32 arch specifies that at the time of processor reset the
4456 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004457 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004458 * state vmexit when "unrestricted guest" mode is turned on.
4459 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4460 * tree. Newer qemu binaries with that qemu fix would not need this
4461 * kvm hack.
4462 */
4463 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004464 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004465
Gleb Natapovf924d662012-12-12 19:10:55 +02004466 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004467
4468out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004469 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470}
4471
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4473{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004474 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475
4476 *db = (ar >> 14) & 1;
4477 *l = (ar >> 13) & 1;
4478}
4479
Gleb Natapov89a27f42010-02-16 10:51:48 +02004480static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004481{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004482 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4483 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484}
4485
Gleb Natapov89a27f42010-02-16 10:51:48 +02004486static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004488 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4489 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004490}
4491
Gleb Natapov89a27f42010-02-16 10:51:48 +02004492static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004494 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4495 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496}
4497
Gleb Natapov89a27f42010-02-16 10:51:48 +02004498static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004500 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4501 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004502}
4503
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004504static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4505{
4506 struct kvm_segment var;
4507 u32 ar;
4508
4509 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004510 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004511 if (seg == VCPU_SREG_CS)
4512 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004513 ar = vmx_segment_access_rights(&var);
4514
4515 if (var.base != (var.selector << 4))
4516 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004517 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004518 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004519 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004520 return false;
4521
4522 return true;
4523}
4524
4525static bool code_segment_valid(struct kvm_vcpu *vcpu)
4526{
4527 struct kvm_segment cs;
4528 unsigned int cs_rpl;
4529
4530 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004531 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004532
Avi Kivity1872a3f2009-01-04 23:26:52 +02004533 if (cs.unusable)
4534 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004535 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004536 return false;
4537 if (!cs.s)
4538 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004539 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004540 if (cs.dpl > cs_rpl)
4541 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004542 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004543 if (cs.dpl != cs_rpl)
4544 return false;
4545 }
4546 if (!cs.present)
4547 return false;
4548
4549 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4550 return true;
4551}
4552
4553static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4554{
4555 struct kvm_segment ss;
4556 unsigned int ss_rpl;
4557
4558 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004559 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004560
Avi Kivity1872a3f2009-01-04 23:26:52 +02004561 if (ss.unusable)
4562 return true;
4563 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004564 return false;
4565 if (!ss.s)
4566 return false;
4567 if (ss.dpl != ss_rpl) /* DPL != RPL */
4568 return false;
4569 if (!ss.present)
4570 return false;
4571
4572 return true;
4573}
4574
4575static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4576{
4577 struct kvm_segment var;
4578 unsigned int rpl;
4579
4580 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004581 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004582
Avi Kivity1872a3f2009-01-04 23:26:52 +02004583 if (var.unusable)
4584 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004585 if (!var.s)
4586 return false;
4587 if (!var.present)
4588 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004589 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004590 if (var.dpl < rpl) /* DPL < RPL */
4591 return false;
4592 }
4593
4594 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4595 * rights flags
4596 */
4597 return true;
4598}
4599
4600static bool tr_valid(struct kvm_vcpu *vcpu)
4601{
4602 struct kvm_segment tr;
4603
4604 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4605
Avi Kivity1872a3f2009-01-04 23:26:52 +02004606 if (tr.unusable)
4607 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004608 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004609 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004610 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004611 return false;
4612 if (!tr.present)
4613 return false;
4614
4615 return true;
4616}
4617
4618static bool ldtr_valid(struct kvm_vcpu *vcpu)
4619{
4620 struct kvm_segment ldtr;
4621
4622 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4623
Avi Kivity1872a3f2009-01-04 23:26:52 +02004624 if (ldtr.unusable)
4625 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004626 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004627 return false;
4628 if (ldtr.type != 2)
4629 return false;
4630 if (!ldtr.present)
4631 return false;
4632
4633 return true;
4634}
4635
4636static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4637{
4638 struct kvm_segment cs, ss;
4639
4640 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4641 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4642
Nadav Amitb32a9912015-03-29 16:33:04 +03004643 return ((cs.selector & SEGMENT_RPL_MASK) ==
4644 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004645}
4646
4647/*
4648 * Check if guest state is valid. Returns true if valid, false if
4649 * not.
4650 * We assume that registers are always usable
4651 */
4652static bool guest_state_valid(struct kvm_vcpu *vcpu)
4653{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004654 if (enable_unrestricted_guest)
4655 return true;
4656
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004657 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004658 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004659 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4660 return false;
4661 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4662 return false;
4663 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4664 return false;
4665 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4666 return false;
4667 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4668 return false;
4669 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4670 return false;
4671 } else {
4672 /* protected mode guest state checks */
4673 if (!cs_ss_rpl_check(vcpu))
4674 return false;
4675 if (!code_segment_valid(vcpu))
4676 return false;
4677 if (!stack_segment_valid(vcpu))
4678 return false;
4679 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4680 return false;
4681 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4682 return false;
4683 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4684 return false;
4685 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4686 return false;
4687 if (!tr_valid(vcpu))
4688 return false;
4689 if (!ldtr_valid(vcpu))
4690 return false;
4691 }
4692 /* TODO:
4693 * - Add checks on RIP
4694 * - Add checks on RFLAGS
4695 */
4696
4697 return true;
4698}
4699
Mike Dayd77c26f2007-10-08 09:02:08 -04004700static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004702 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004703 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004704 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004705
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004706 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004707 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004708 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4709 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004710 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004711 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004712 r = kvm_write_guest_page(kvm, fn++, &data,
4713 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004714 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004715 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004716 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4717 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004718 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004719 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4720 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004721 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004722 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004723 r = kvm_write_guest_page(kvm, fn, &data,
4724 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4725 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004726out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004727 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004728 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729}
4730
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004731static int init_rmode_identity_map(struct kvm *kvm)
4732{
Tang Chenf51770e2014-09-16 18:41:59 +08004733 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004734 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004735 u32 tmp;
4736
Avi Kivity089d0342009-03-23 18:26:32 +02004737 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004738 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004739
4740 /* Protect kvm->arch.ept_identity_pagetable_done. */
4741 mutex_lock(&kvm->slots_lock);
4742
Tang Chenf51770e2014-09-16 18:41:59 +08004743 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004744 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004745
Sheng Yangb927a3c2009-07-21 10:42:48 +08004746 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004747
4748 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004749 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004750 goto out2;
4751
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004752 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004753 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4754 if (r < 0)
4755 goto out;
4756 /* Set up identity-mapping pagetable for EPT in real mode */
4757 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4758 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4759 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4760 r = kvm_write_guest_page(kvm, identity_map_pfn,
4761 &tmp, i * sizeof(tmp), sizeof(tmp));
4762 if (r < 0)
4763 goto out;
4764 }
4765 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004766
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004767out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004768 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004769
4770out2:
4771 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004772 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004773}
4774
Avi Kivity6aa8b732006-12-10 02:21:36 -08004775static void seg_setup(int seg)
4776{
Mathias Krause772e0312012-08-30 01:30:19 +02004777 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004778 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004779
4780 vmcs_write16(sf->selector, 0);
4781 vmcs_writel(sf->base, 0);
4782 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004783 ar = 0x93;
4784 if (seg == VCPU_SREG_CS)
4785 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004786
4787 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788}
4789
Sheng Yangf78e0e22007-10-29 09:40:42 +08004790static int alloc_apic_access_page(struct kvm *kvm)
4791{
Xiao Guangrong44841412012-09-07 14:14:20 +08004792 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004793 int r = 0;
4794
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004795 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004796 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004797 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004798 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4799 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004800 if (r)
4801 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004802
Tang Chen73a6d942014-09-11 13:38:00 +08004803 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004804 if (is_error_page(page)) {
4805 r = -EFAULT;
4806 goto out;
4807 }
4808
Tang Chenc24ae0d2014-09-24 15:57:58 +08004809 /*
4810 * Do not pin the page in memory, so that memory hot-unplug
4811 * is able to migrate it.
4812 */
4813 put_page(page);
4814 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004815out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004816 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004817 return r;
4818}
4819
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004820static int alloc_identity_pagetable(struct kvm *kvm)
4821{
Tang Chena255d472014-09-16 18:41:58 +08004822 /* Called with kvm->slots_lock held. */
4823
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004824 int r = 0;
4825
Tang Chena255d472014-09-16 18:41:58 +08004826 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4827
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004828 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4829 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004830
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004831 return r;
4832}
4833
Wanpeng Li991e7a02015-09-16 17:30:05 +08004834static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004835{
4836 int vpid;
4837
Avi Kivity919818a2009-03-23 18:01:29 +02004838 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004839 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004840 spin_lock(&vmx_vpid_lock);
4841 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004842 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004843 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004844 else
4845 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004846 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004847 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004848}
4849
Wanpeng Li991e7a02015-09-16 17:30:05 +08004850static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004851{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004852 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004853 return;
4854 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004855 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004856 spin_unlock(&vmx_vpid_lock);
4857}
4858
Paolo Bonzini6236b782018-01-16 16:51:18 +01004859static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4860 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004861{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004862 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004863
4864 if (!cpu_has_vmx_msr_bitmap())
4865 return;
4866
4867 /*
4868 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4869 * have the write-low and read-high bitmap offsets the wrong way round.
4870 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4871 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004872 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004873 if (type & MSR_TYPE_R)
4874 /* read-low */
4875 __clear_bit(msr, msr_bitmap + 0x000 / f);
4876
4877 if (type & MSR_TYPE_W)
4878 /* write-low */
4879 __clear_bit(msr, msr_bitmap + 0x800 / f);
4880
Sheng Yang25c5f222008-03-28 13:18:56 +08004881 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4882 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004883 if (type & MSR_TYPE_R)
4884 /* read-high */
4885 __clear_bit(msr, msr_bitmap + 0x400 / f);
4886
4887 if (type & MSR_TYPE_W)
4888 /* write-high */
4889 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4890
4891 }
4892}
4893
Paolo Bonzini6236b782018-01-16 16:51:18 +01004894static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4895 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004896{
4897 int f = sizeof(unsigned long);
4898
4899 if (!cpu_has_vmx_msr_bitmap())
4900 return;
4901
4902 /*
4903 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4904 * have the write-low and read-high bitmap offsets the wrong way round.
4905 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4906 */
4907 if (msr <= 0x1fff) {
4908 if (type & MSR_TYPE_R)
4909 /* read-low */
4910 __set_bit(msr, msr_bitmap + 0x000 / f);
4911
4912 if (type & MSR_TYPE_W)
4913 /* write-low */
4914 __set_bit(msr, msr_bitmap + 0x800 / f);
4915
4916 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4917 msr &= 0x1fff;
4918 if (type & MSR_TYPE_R)
4919 /* read-high */
4920 __set_bit(msr, msr_bitmap + 0x400 / f);
4921
4922 if (type & MSR_TYPE_W)
4923 /* write-high */
4924 __set_bit(msr, msr_bitmap + 0xc00 / f);
4925
Sheng Yang25c5f222008-03-28 13:18:56 +08004926 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004927}
4928
Paolo Bonzini6236b782018-01-16 16:51:18 +01004929static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4930 u32 msr, int type, bool value)
4931{
4932 if (value)
4933 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4934 else
4935 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4936}
4937
Wincy Vanf2b93282015-02-03 23:56:03 +08004938/*
4939 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4940 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4941 */
4942static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4943 unsigned long *msr_bitmap_nested,
4944 u32 msr, int type)
4945{
4946 int f = sizeof(unsigned long);
4947
4948 if (!cpu_has_vmx_msr_bitmap()) {
4949 WARN_ON(1);
4950 return;
4951 }
4952
4953 /*
4954 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4955 * have the write-low and read-high bitmap offsets the wrong way round.
4956 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4957 */
4958 if (msr <= 0x1fff) {
4959 if (type & MSR_TYPE_R &&
4960 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4961 /* read-low */
4962 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4963
4964 if (type & MSR_TYPE_W &&
4965 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4966 /* write-low */
4967 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4968
4969 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4970 msr &= 0x1fff;
4971 if (type & MSR_TYPE_R &&
4972 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4973 /* read-high */
4974 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4975
4976 if (type & MSR_TYPE_W &&
4977 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4978 /* write-high */
4979 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4980
4981 }
4982}
4983
Paolo Bonzini6236b782018-01-16 16:51:18 +01004984static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004985{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004986 u8 mode = 0;
4987
4988 if (cpu_has_secondary_exec_ctrls() &&
4989 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4990 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4991 mode |= MSR_BITMAP_MODE_X2APIC;
4992 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4993 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4994 }
4995
4996 if (is_long_mode(vcpu))
4997 mode |= MSR_BITMAP_MODE_LM;
4998
4999 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005000}
5001
Paolo Bonzini6236b782018-01-16 16:51:18 +01005002#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5003
5004static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5005 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005006{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005007 int msr;
5008
5009 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5010 unsigned word = msr / BITS_PER_LONG;
5011 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5012 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5013 }
5014
5015 if (mode & MSR_BITMAP_MODE_X2APIC) {
5016 /*
5017 * TPR reads and writes can be virtualized even if virtual interrupt
5018 * delivery is not in use.
5019 */
5020 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5021 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5022 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5023 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5024 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5025 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005026 }
Yang Zhang8d146952013-01-25 10:18:50 +08005027}
5028
Paolo Bonzini6236b782018-01-16 16:51:18 +01005029static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005030{
Paolo Bonzini6236b782018-01-16 16:51:18 +01005031 struct vcpu_vmx *vmx = to_vmx(vcpu);
5032 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5033 u8 mode = vmx_msr_bitmap_mode(vcpu);
5034 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005035
Paolo Bonzini6236b782018-01-16 16:51:18 +01005036 if (!changed)
5037 return;
5038
5039 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5040 !(mode & MSR_BITMAP_MODE_LM));
5041
5042 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5043 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5044
5045 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005046}
5047
Andrey Smetanind62caab2015-11-10 15:36:33 +03005048static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005049{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005050 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005051}
5052
David Matlackb7649e12017-08-01 14:00:40 -07005053static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5054{
5055 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5056 gfn_t gfn;
5057
5058 /*
5059 * Don't need to mark the APIC access page dirty; it is never
5060 * written to by the CPU during APIC virtualization.
5061 */
5062
5063 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5064 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5065 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5066 }
5067
5068 if (nested_cpu_has_posted_intr(vmcs12)) {
5069 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5070 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5071 }
5072}
5073
5074
David Hildenbrand1edccf22017-01-25 11:58:58 +01005075static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005076{
5077 struct vcpu_vmx *vmx = to_vmx(vcpu);
5078 int max_irr;
5079 void *vapic_page;
5080 u16 status;
5081
David Matlackb7649e12017-08-01 14:00:40 -07005082 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5083 return;
Wincy Van705699a2015-02-03 23:58:17 +08005084
David Matlackb7649e12017-08-01 14:00:40 -07005085 vmx->nested.pi_pending = false;
5086 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5087 return;
Wincy Van705699a2015-02-03 23:58:17 +08005088
David Matlackb7649e12017-08-01 14:00:40 -07005089 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5090 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005091 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005092 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5093 kunmap(vmx->nested.virtual_apic_page);
5094
5095 status = vmcs_read16(GUEST_INTR_STATUS);
5096 if ((u8)max_irr > ((u8)status & 0xff)) {
5097 status &= ~0xff;
5098 status |= (u8)max_irr;
5099 vmcs_write16(GUEST_INTR_STATUS, status);
5100 }
5101 }
David Matlackb7649e12017-08-01 14:00:40 -07005102
5103 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005104}
5105
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005106static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5107{
5108#ifdef CONFIG_SMP
5109 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005110 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005111 * The vector of interrupt to be delivered to vcpu had
5112 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005113 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005114 * Following cases will be reached in this block, and
5115 * we always send a notification event in all cases as
5116 * explained below.
5117 *
5118 * Case 1: vcpu keeps in non-root mode. Sending a
5119 * notification event posts the interrupt to vcpu.
5120 *
5121 * Case 2: vcpu exits to root mode and is still
5122 * runnable. PIR will be synced to vIRR before the
5123 * next vcpu entry. Sending a notification event in
5124 * this case has no effect, as vcpu is not in root
5125 * mode.
5126 *
5127 * Case 3: vcpu exits to root mode and is blocked.
5128 * vcpu_block() has already synced PIR to vIRR and
5129 * never blocks vcpu if vIRR is not cleared. Therefore,
5130 * a blocked vcpu here does not wait for any requested
5131 * interrupts in PIR, and sending a notification event
5132 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005133 */
Feng Wu28b835d2015-09-18 22:29:54 +08005134
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005135 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5136 POSTED_INTR_VECTOR);
5137 return true;
5138 }
5139#endif
5140 return false;
5141}
5142
Wincy Van705699a2015-02-03 23:58:17 +08005143static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5144 int vector)
5145{
5146 struct vcpu_vmx *vmx = to_vmx(vcpu);
5147
5148 if (is_guest_mode(vcpu) &&
5149 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005150 /*
5151 * If a posted intr is not recognized by hardware,
5152 * we will accomplish it in the next vmentry.
5153 */
5154 vmx->nested.pi_pending = true;
5155 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005156 /* the PIR and ON have been set by L1. */
5157 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5158 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005159 return 0;
5160 }
5161 return -1;
5162}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005163/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005164 * Send interrupt to vcpu via posted interrupt way.
5165 * 1. If target vcpu is running(non-root mode), send posted interrupt
5166 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5167 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5168 * interrupt from PIR in next vmentry.
5169 */
5170static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5171{
5172 struct vcpu_vmx *vmx = to_vmx(vcpu);
5173 int r;
5174
Wincy Van705699a2015-02-03 23:58:17 +08005175 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5176 if (!r)
5177 return;
5178
Yang Zhanga20ed542013-04-11 19:25:15 +08005179 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5180 return;
5181
5182 r = pi_test_and_set_on(&vmx->pi_desc);
5183 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005184 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005185 kvm_vcpu_kick(vcpu);
5186}
5187
5188static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5189{
5190 struct vcpu_vmx *vmx = to_vmx(vcpu);
5191
5192 if (!pi_test_and_clear_on(&vmx->pi_desc))
5193 return;
5194
5195 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5196}
5197
Avi Kivity6aa8b732006-12-10 02:21:36 -08005198/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005199 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5200 * will not change in the lifetime of the guest.
5201 * Note that host-state that does change is set elsewhere. E.g., host-state
5202 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5203 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005204static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005205{
5206 u32 low32, high32;
5207 unsigned long tmpl;
5208 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005209 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005210
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005211 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005212 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5213
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005214 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005215 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005216 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5217 vmx->host_state.vmcs_host_cr4 = cr4;
5218
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005219 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005220#ifdef CONFIG_X86_64
5221 /*
5222 * Load null selectors, so we can avoid reloading them in
5223 * __vmx_load_host_state(), in case userspace uses the null selectors
5224 * too (the expected case).
5225 */
5226 vmcs_write16(HOST_DS_SELECTOR, 0);
5227 vmcs_write16(HOST_ES_SELECTOR, 0);
5228#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005229 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5230 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005231#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005232 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5233 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5234
5235 native_store_idt(&dt);
5236 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005237 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005238
Avi Kivity83287ea422012-09-16 15:10:57 +03005239 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005240
5241 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5242 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5243 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5244 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5245
5246 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5247 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5248 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5249 }
5250}
5251
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005252static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5253{
5254 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5255 if (enable_ept)
5256 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005257 if (is_guest_mode(&vmx->vcpu))
5258 vmx->vcpu.arch.cr4_guest_owned_bits &=
5259 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005260 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5261}
5262
Yang Zhang01e439b2013-04-11 19:25:12 +08005263static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5264{
5265 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5266
Andrey Smetanind62caab2015-11-10 15:36:33 +03005267 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005268 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005269 /* Enable the preemption timer dynamically */
5270 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005271 return pin_based_exec_ctrl;
5272}
5273
Andrey Smetanind62caab2015-11-10 15:36:33 +03005274static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5275{
5276 struct vcpu_vmx *vmx = to_vmx(vcpu);
5277
5278 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005279 if (cpu_has_secondary_exec_ctrls()) {
5280 if (kvm_vcpu_apicv_active(vcpu))
5281 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5282 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5283 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5284 else
5285 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5286 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5287 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5288 }
5289
5290 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005291 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005292}
5293
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005294static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5295{
5296 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005297
5298 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5299 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5300
Paolo Bonzini35754c92015-07-29 12:05:37 +02005301 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005302 exec_control &= ~CPU_BASED_TPR_SHADOW;
5303#ifdef CONFIG_X86_64
5304 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5305 CPU_BASED_CR8_LOAD_EXITING;
5306#endif
5307 }
5308 if (!enable_ept)
5309 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5310 CPU_BASED_CR3_LOAD_EXITING |
5311 CPU_BASED_INVLPG_EXITING;
5312 return exec_control;
5313}
5314
5315static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5316{
5317 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005318 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005319 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5320 if (vmx->vpid == 0)
5321 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5322 if (!enable_ept) {
5323 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5324 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005325 /* Enable INVPCID for non-ept guests may cause performance regression. */
5326 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005327 }
5328 if (!enable_unrestricted_guest)
5329 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5330 if (!ple_gap)
5331 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005332 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005333 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5334 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005335 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005336 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5337 (handle_vmptrld).
5338 We can NOT enable shadow_vmcs here because we don't have yet
5339 a current VMCS12
5340 */
5341 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005342
5343 if (!enable_pml)
5344 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005345
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005346 return exec_control;
5347}
5348
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005349static void ept_set_mmio_spte_mask(void)
5350{
5351 /*
5352 * EPT Misconfigurations can be generated if the value of bits 2:0
5353 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005354 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005355 * spte.
5356 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005357 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005358}
5359
Wanpeng Lif53cd632014-12-02 19:14:58 +08005360#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005361/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005362 * Sets up the vmcs for emulated real mode.
5363 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005364static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005365{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005366#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005367 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005368#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005369 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005370
Avi Kivity6aa8b732006-12-10 02:21:36 -08005371 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005372 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5373 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005374
Abel Gordon4607c2d2013-04-18 14:35:55 +03005375 if (enable_shadow_vmcs) {
5376 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5377 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5378 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005379 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005380 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005381
Avi Kivity6aa8b732006-12-10 02:21:36 -08005382 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5383
Avi Kivity6aa8b732006-12-10 02:21:36 -08005384 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005385 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005386 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005387
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005388 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005389
Dan Williamsdfa169b2016-06-02 11:17:24 -07005390 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005391 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5392 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005393 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005394
Andrey Smetanind62caab2015-11-10 15:36:33 +03005395 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005396 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5397 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5398 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5399 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5400
5401 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005402
Li RongQing0bcf2612015-12-03 13:29:34 +08005403 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005404 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005405 }
5406
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005407 if (ple_gap) {
5408 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005409 vmx->ple_window = ple_window;
5410 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005411 }
5412
Xiao Guangrongc3707952011-07-12 03:28:04 +08005413 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5414 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005415 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5416
Avi Kivity9581d442010-10-19 16:46:55 +02005417 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5418 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005419 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005420#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005421 rdmsrl(MSR_FS_BASE, a);
5422 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5423 rdmsrl(MSR_GS_BASE, a);
5424 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5425#else
5426 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5427 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5428#endif
5429
Eddie Dong2cc51562007-05-21 07:28:09 +03005430 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5431 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005432 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005433 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005434 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005435
Radim Krčmář74545702015-04-27 15:11:25 +02005436 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5437 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005438
Paolo Bonzini03916db2014-07-24 14:21:57 +02005439 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005440 u32 index = vmx_msr_index[i];
5441 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005442 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005443
5444 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5445 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005446 if (wrmsr_safe(index, data_low, data_high) < 0)
5447 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005448 vmx->guest_msrs[j].index = i;
5449 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005450 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005451 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005453
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01005454 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5455 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005456
5457 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005458
5459 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005460 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005461
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005462 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005463 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005464
Wanpeng Lif53cd632014-12-02 19:14:58 +08005465 if (vmx_xsaves_supported())
5466 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5467
Peter Feiner4e595162016-07-07 14:49:58 -07005468 if (enable_pml) {
5469 ASSERT(vmx->pml_pg);
5470 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5471 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5472 }
5473
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005474 return 0;
5475}
5476
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005477static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005478{
5479 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005480 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005481 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005482
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005483 vmx->rmode.vm86_active = 0;
Wanpeng Li7a1eac82018-02-28 14:03:31 +08005484 vcpu->arch.microcode_version = 0x100000000ULL;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005485 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005486
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005487 vmx->soft_vnmi_blocked = 0;
5488
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005489 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005490 kvm_set_cr8(vcpu, 0);
5491
5492 if (!init_event) {
5493 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5494 MSR_IA32_APICBASE_ENABLE;
5495 if (kvm_vcpu_is_reset_bsp(vcpu))
5496 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5497 apic_base_msr.host_initiated = true;
5498 kvm_set_apic_base(vcpu, &apic_base_msr);
5499 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005500
Avi Kivity2fb92db2011-04-27 19:42:18 +03005501 vmx_segment_cache_clear(vmx);
5502
Avi Kivity5706be02008-08-20 15:07:31 +03005503 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005504 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005505 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005506
5507 seg_setup(VCPU_SREG_DS);
5508 seg_setup(VCPU_SREG_ES);
5509 seg_setup(VCPU_SREG_FS);
5510 seg_setup(VCPU_SREG_GS);
5511 seg_setup(VCPU_SREG_SS);
5512
5513 vmcs_write16(GUEST_TR_SELECTOR, 0);
5514 vmcs_writel(GUEST_TR_BASE, 0);
5515 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5516 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5517
5518 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5519 vmcs_writel(GUEST_LDTR_BASE, 0);
5520 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5521 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5522
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005523 if (!init_event) {
5524 vmcs_write32(GUEST_SYSENTER_CS, 0);
5525 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5526 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5527 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5528 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005529
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005530 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005531 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005532
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005533 vmcs_writel(GUEST_GDTR_BASE, 0);
5534 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5535
5536 vmcs_writel(GUEST_IDTR_BASE, 0);
5537 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5538
Anthony Liguori443381a2010-12-06 10:53:38 -06005539 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005540 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005541 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005542
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005543 setup_msrs(vmx);
5544
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5546
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005547 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005548 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005549 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005550 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005551 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005552 vmcs_write32(TPR_THRESHOLD, 0);
5553 }
5554
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005555 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556
Andrey Smetanind62caab2015-11-10 15:36:33 +03005557 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005558 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5559
Sheng Yang2384d2b2008-01-17 15:14:33 +08005560 if (vmx->vpid != 0)
5561 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5562
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005563 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005564 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005565 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005566 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005567 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005568 vmx_fpu_activate(vcpu);
5569 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005571 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005572}
5573
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005574/*
5575 * In nested virtualization, check if L1 asked to exit on external interrupts.
5576 * For most existing hypervisors, this will always return true.
5577 */
5578static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5579{
5580 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5581 PIN_BASED_EXT_INTR_MASK;
5582}
5583
Bandan Das77b0f5d2014-04-19 18:17:45 -04005584/*
5585 * In nested virtualization, check if L1 has set
5586 * VM_EXIT_ACK_INTR_ON_EXIT
5587 */
5588static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5589{
5590 return get_vmcs12(vcpu)->vm_exit_controls &
5591 VM_EXIT_ACK_INTR_ON_EXIT;
5592}
5593
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005594static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5595{
5596 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5597 PIN_BASED_NMI_EXITING;
5598}
5599
Jan Kiszkac9a79532014-03-07 20:03:15 +01005600static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005601{
5602 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005603
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005604 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5605 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5606 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5607}
5608
Jan Kiszkac9a79532014-03-07 20:03:15 +01005609static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005610{
5611 u32 cpu_based_vm_exec_control;
5612
Jan Kiszkac9a79532014-03-07 20:03:15 +01005613 if (!cpu_has_virtual_nmis() ||
5614 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5615 enable_irq_window(vcpu);
5616 return;
5617 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005618
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005619 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5620 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5621 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5622}
5623
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005624static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005625{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005626 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005627 uint32_t intr;
5628 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005629
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005630 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005631
Avi Kivityfa89a812008-09-01 15:57:51 +03005632 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005633 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005634 int inc_eip = 0;
5635 if (vcpu->arch.interrupt.soft)
5636 inc_eip = vcpu->arch.event_exit_inst_len;
5637 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005638 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005639 return;
5640 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005641 intr = irq | INTR_INFO_VALID_MASK;
5642 if (vcpu->arch.interrupt.soft) {
5643 intr |= INTR_TYPE_SOFT_INTR;
5644 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5645 vmx->vcpu.arch.event_exit_inst_len);
5646 } else
5647 intr |= INTR_TYPE_EXT_INTR;
5648 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005649}
5650
Sheng Yangf08864b2008-05-15 18:23:25 +08005651static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5652{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005653 struct vcpu_vmx *vmx = to_vmx(vcpu);
5654
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005655 if (!is_guest_mode(vcpu)) {
5656 if (!cpu_has_virtual_nmis()) {
5657 /*
5658 * Tracking the NMI-blocked state in software is built upon
5659 * finding the next open IRQ window. This, in turn, depends on
5660 * well-behaving guests: They have to keep IRQs disabled at
5661 * least as long as the NMI handler runs. Otherwise we may
5662 * cause NMI nesting, maybe breaking the guest. But as this is
5663 * highly unlikely, we can live with the residual risk.
5664 */
5665 vmx->soft_vnmi_blocked = 1;
5666 vmx->vnmi_blocked_time = 0;
5667 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005668
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005669 ++vcpu->stat.nmi_injections;
5670 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005671 }
5672
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005673 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005674 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005675 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005676 return;
5677 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005678
Sheng Yangf08864b2008-05-15 18:23:25 +08005679 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5680 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005681}
5682
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005683static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5684{
5685 if (!cpu_has_virtual_nmis())
5686 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005687 if (to_vmx(vcpu)->nmi_known_unmasked)
5688 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005689 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005690}
5691
5692static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5693{
5694 struct vcpu_vmx *vmx = to_vmx(vcpu);
5695
5696 if (!cpu_has_virtual_nmis()) {
5697 if (vmx->soft_vnmi_blocked != masked) {
5698 vmx->soft_vnmi_blocked = masked;
5699 vmx->vnmi_blocked_time = 0;
5700 }
5701 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005702 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005703 if (masked)
5704 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5705 GUEST_INTR_STATE_NMI);
5706 else
5707 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5708 GUEST_INTR_STATE_NMI);
5709 }
5710}
5711
Jan Kiszka2505dc92013-04-14 12:12:47 +02005712static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5713{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005714 if (to_vmx(vcpu)->nested.nested_run_pending)
5715 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005716
Jan Kiszka2505dc92013-04-14 12:12:47 +02005717 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5718 return 0;
5719
5720 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5721 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5722 | GUEST_INTR_STATE_NMI));
5723}
5724
Gleb Natapov78646122009-03-23 12:12:11 +02005725static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5726{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005727 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5728 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005729 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5730 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005731}
5732
Izik Eiduscbc94022007-10-25 00:29:55 +02005733static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5734{
5735 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005736
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005737 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5738 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005739 if (ret)
5740 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005741 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005742 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005743}
5744
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005745static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005746{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005747 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005748 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005749 /*
5750 * Update instruction length as we may reinject the exception
5751 * from user space while in guest debugging mode.
5752 */
5753 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5754 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005755 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005756 return false;
5757 /* fall through */
5758 case DB_VECTOR:
5759 if (vcpu->guest_debug &
5760 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5761 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005762 /* fall through */
5763 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005764 case OF_VECTOR:
5765 case BR_VECTOR:
5766 case UD_VECTOR:
5767 case DF_VECTOR:
5768 case SS_VECTOR:
5769 case GP_VECTOR:
5770 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005771 return true;
5772 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005773 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005774 return false;
5775}
5776
5777static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5778 int vec, u32 err_code)
5779{
5780 /*
5781 * Instruction with address size override prefix opcode 0x67
5782 * Cause the #SS fault with 0 error code in VM86 mode.
5783 */
5784 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5785 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5786 if (vcpu->arch.halt_request) {
5787 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005788 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005789 }
5790 return 1;
5791 }
5792 return 0;
5793 }
5794
5795 /*
5796 * Forward all other exceptions that are valid in real mode.
5797 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5798 * the required debugging infrastructure rework.
5799 */
5800 kvm_queue_exception(vcpu, vec);
5801 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005802}
5803
Andi Kleena0861c02009-06-08 17:37:09 +08005804/*
5805 * Trigger machine check on the host. We assume all the MSRs are already set up
5806 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5807 * We pass a fake environment to the machine check handler because we want
5808 * the guest to be always treated like user space, no matter what context
5809 * it used internally.
5810 */
5811static void kvm_machine_check(void)
5812{
5813#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5814 struct pt_regs regs = {
5815 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5816 .flags = X86_EFLAGS_IF,
5817 };
5818
5819 do_machine_check(&regs, 0);
5820#endif
5821}
5822
Avi Kivity851ba692009-08-24 11:10:17 +03005823static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005824{
5825 /* already handled by vcpu_run */
5826 return 1;
5827}
5828
Avi Kivity851ba692009-08-24 11:10:17 +03005829static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005830{
Avi Kivity1155f762007-11-22 11:30:47 +02005831 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005832 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005833 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005834 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005835 u32 vect_info;
5836 enum emulation_result er;
5837
Avi Kivity1155f762007-11-22 11:30:47 +02005838 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005839 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005840
Andi Kleena0861c02009-06-08 17:37:09 +08005841 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005842 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005843
Jim Mattson3f618a02016-12-12 11:01:37 -08005844 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005845 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005846
5847 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005848 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005849 return 1;
5850 }
5851
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005852 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005853 if (is_guest_mode(vcpu)) {
5854 kvm_queue_exception(vcpu, UD_VECTOR);
5855 return 1;
5856 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005857 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005858 if (er == EMULATE_USER_EXIT)
5859 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005860 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005861 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005862 return 1;
5863 }
5864
Avi Kivity6aa8b732006-12-10 02:21:36 -08005865 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005866 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005867 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005868
5869 /*
5870 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5871 * MMIO, it is better to report an internal error.
5872 * See the comments in vmx_handle_exit.
5873 */
5874 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5875 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5876 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5877 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005878 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005879 vcpu->run->internal.data[0] = vect_info;
5880 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005881 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005882 return 0;
5883 }
5884
Avi Kivity6aa8b732006-12-10 02:21:36 -08005885 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005886 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005887 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005888 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005889 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005890 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005891
Gleb Natapov3298b752009-05-11 13:35:46 +03005892 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005893 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005894 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005895 }
5896
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005897 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005898
5899 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5900 return handle_rmode_exception(vcpu, ex_no, error_code);
5901
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005902 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005903 case AC_VECTOR:
5904 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5905 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005906 case DB_VECTOR:
5907 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5908 if (!(vcpu->guest_debug &
5909 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005910 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005911 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005912 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005913 skip_emulated_instruction(vcpu);
5914
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005915 kvm_queue_exception(vcpu, DB_VECTOR);
5916 return 1;
5917 }
5918 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5919 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5920 /* fall through */
5921 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005922 /*
5923 * Update instruction length as we may reinject #BP from
5924 * user space while in guest debugging mode. Reading it for
5925 * #DB as well causes no harm, it is not used in that case.
5926 */
5927 vmx->vcpu.arch.event_exit_inst_len =
5928 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005929 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005930 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005931 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5932 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005933 break;
5934 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005935 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5936 kvm_run->ex.exception = ex_no;
5937 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005938 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005939 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005940 return 0;
5941}
5942
Avi Kivity851ba692009-08-24 11:10:17 +03005943static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005944{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005945 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005946 return 1;
5947}
5948
Avi Kivity851ba692009-08-24 11:10:17 +03005949static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005950{
Avi Kivity851ba692009-08-24 11:10:17 +03005951 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005952 return 0;
5953}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954
Avi Kivity851ba692009-08-24 11:10:17 +03005955static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005956{
He, Qingbfdaab02007-09-12 14:18:28 +08005957 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005958 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005959 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005960
He, Qingbfdaab02007-09-12 14:18:28 +08005961 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005962 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005963 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005964
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005965 ++vcpu->stat.io_exits;
5966
5967 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005968 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005969
5970 port = exit_qualification >> 16;
5971 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005972 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005973
5974 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005975}
5976
Ingo Molnar102d8322007-02-19 14:37:47 +02005977static void
5978vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5979{
5980 /*
5981 * Patch in the VMCALL instruction:
5982 */
5983 hypercall[0] = 0x0f;
5984 hypercall[1] = 0x01;
5985 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005986}
5987
Wincy Vanb9c237b2015-02-03 23:56:30 +08005988static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005989{
5990 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005991 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005992
Wincy Vanb9c237b2015-02-03 23:56:30 +08005993 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005994 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5995 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5996 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5997 return (val & always_on) == always_on;
5998}
5999
Guo Chao0fa06072012-06-28 15:16:19 +08006000/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006001static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6002{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006003 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006004 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6005 unsigned long orig_val = val;
6006
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006007 /*
6008 * We get here when L2 changed cr0 in a way that did not change
6009 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006010 * but did change L0 shadowed bits. So we first calculate the
6011 * effective cr0 value that L1 would like to write into the
6012 * hardware. It consists of the L2-owned bits from the new
6013 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006014 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006015 val = (val & ~vmcs12->cr0_guest_host_mask) |
6016 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6017
Wincy Vanb9c237b2015-02-03 23:56:30 +08006018 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006019 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006020
6021 if (kvm_set_cr0(vcpu, val))
6022 return 1;
6023 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006024 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006025 } else {
6026 if (to_vmx(vcpu)->nested.vmxon &&
6027 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
6028 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006029 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006030 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006031}
6032
6033static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6034{
6035 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006036 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6037 unsigned long orig_val = val;
6038
6039 /* analogously to handle_set_cr0 */
6040 val = (val & ~vmcs12->cr4_guest_host_mask) |
6041 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6042 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006043 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006044 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006045 return 0;
6046 } else
6047 return kvm_set_cr4(vcpu, val);
6048}
6049
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08006050/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006051static void handle_clts(struct kvm_vcpu *vcpu)
6052{
6053 if (is_guest_mode(vcpu)) {
6054 /*
6055 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
6056 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
6057 * just pretend it's off (also in arch.cr0 for fpu_activate).
6058 */
6059 vmcs_writel(CR0_READ_SHADOW,
6060 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
6061 vcpu->arch.cr0 &= ~X86_CR0_TS;
6062 } else
6063 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
6064}
6065
Avi Kivity851ba692009-08-24 11:10:17 +03006066static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006067{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006068 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006069 int cr;
6070 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006071 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006072
He, Qingbfdaab02007-09-12 14:18:28 +08006073 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006074 cr = exit_qualification & 15;
6075 reg = (exit_qualification >> 8) & 15;
6076 switch ((exit_qualification >> 4) & 3) {
6077 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006078 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006079 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006080 switch (cr) {
6081 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006082 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006083 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006084 return 1;
6085 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03006086 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006087 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006088 return 1;
6089 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006090 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006091 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006092 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006093 case 8: {
6094 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006095 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006096 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006097 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006098 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006099 return 1;
6100 if (cr8_prev <= cr8)
6101 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03006102 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006103 return 0;
6104 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006105 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006106 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006107 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006108 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006109 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006110 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006111 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006112 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006113 case 1: /*mov from cr*/
6114 switch (cr) {
6115 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006116 val = kvm_read_cr3(vcpu);
6117 kvm_register_write(vcpu, reg, val);
6118 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006119 skip_emulated_instruction(vcpu);
6120 return 1;
6121 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006122 val = kvm_get_cr8(vcpu);
6123 kvm_register_write(vcpu, reg, val);
6124 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006125 skip_emulated_instruction(vcpu);
6126 return 1;
6127 }
6128 break;
6129 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006130 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006131 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006132 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006133
6134 skip_emulated_instruction(vcpu);
6135 return 1;
6136 default:
6137 break;
6138 }
Avi Kivity851ba692009-08-24 11:10:17 +03006139 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006140 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006141 (int)(exit_qualification >> 4) & 3, cr);
6142 return 0;
6143}
6144
Avi Kivity851ba692009-08-24 11:10:17 +03006145static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006146{
He, Qingbfdaab02007-09-12 14:18:28 +08006147 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006148 int dr, dr7, reg;
6149
6150 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6151 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6152
6153 /* First, if DR does not exist, trigger UD */
6154 if (!kvm_require_dr(vcpu, dr))
6155 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006156
Jan Kiszkaf2483412010-01-20 18:20:20 +01006157 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006158 if (!kvm_require_cpl(vcpu, 0))
6159 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006160 dr7 = vmcs_readl(GUEST_DR7);
6161 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006162 /*
6163 * As the vm-exit takes precedence over the debug trap, we
6164 * need to emulate the latter, either for the host or the
6165 * guest debugging itself.
6166 */
6167 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006168 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006169 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006170 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006171 vcpu->run->debug.arch.exception = DB_VECTOR;
6172 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006173 return 0;
6174 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006175 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006176 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006177 kvm_queue_exception(vcpu, DB_VECTOR);
6178 return 1;
6179 }
6180 }
6181
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006182 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006183 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6184 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006185
6186 /*
6187 * No more DR vmexits; force a reload of the debug registers
6188 * and reenter on this instruction. The next vmexit will
6189 * retrieve the full state of the debug registers.
6190 */
6191 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6192 return 1;
6193 }
6194
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006195 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6196 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006197 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006198
6199 if (kvm_get_dr(vcpu, dr, &val))
6200 return 1;
6201 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006202 } else
Nadav Amit57773922014-06-18 17:19:23 +03006203 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006204 return 1;
6205
Avi Kivity6aa8b732006-12-10 02:21:36 -08006206 skip_emulated_instruction(vcpu);
6207 return 1;
6208}
6209
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006210static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6211{
6212 return vcpu->arch.dr6;
6213}
6214
6215static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6216{
6217}
6218
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006219static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6220{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006221 get_debugreg(vcpu->arch.db[0], 0);
6222 get_debugreg(vcpu->arch.db[1], 1);
6223 get_debugreg(vcpu->arch.db[2], 2);
6224 get_debugreg(vcpu->arch.db[3], 3);
6225 get_debugreg(vcpu->arch.dr6, 6);
6226 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6227
6228 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006229 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006230}
6231
Gleb Natapov020df072010-04-13 10:05:23 +03006232static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6233{
6234 vmcs_writel(GUEST_DR7, val);
6235}
6236
Avi Kivity851ba692009-08-24 11:10:17 +03006237static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006238{
Avi Kivity06465c52007-02-28 20:46:53 +02006239 kvm_emulate_cpuid(vcpu);
6240 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006241}
6242
Avi Kivity851ba692009-08-24 11:10:17 +03006243static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006244{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006245 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006246 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006247
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006248 msr_info.index = ecx;
6249 msr_info.host_initiated = false;
6250 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006251 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006252 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006253 return 1;
6254 }
6255
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006256 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006257
Avi Kivity6aa8b732006-12-10 02:21:36 -08006258 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006259 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6260 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006261 skip_emulated_instruction(vcpu);
6262 return 1;
6263}
6264
Avi Kivity851ba692009-08-24 11:10:17 +03006265static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006266{
Will Auld8fe8ab42012-11-29 12:42:12 -08006267 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006268 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6269 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6270 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006271
Will Auld8fe8ab42012-11-29 12:42:12 -08006272 msr.data = data;
6273 msr.index = ecx;
6274 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006275 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006276 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006277 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006278 return 1;
6279 }
6280
Avi Kivity59200272010-01-25 19:47:02 +02006281 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006282 skip_emulated_instruction(vcpu);
6283 return 1;
6284}
6285
Avi Kivity851ba692009-08-24 11:10:17 +03006286static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006287{
Avi Kivity3842d132010-07-27 12:30:24 +03006288 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006289 return 1;
6290}
6291
Avi Kivity851ba692009-08-24 11:10:17 +03006292static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006293{
Eddie Dong85f455f2007-07-06 12:20:49 +03006294 u32 cpu_based_vm_exec_control;
6295
6296 /* clear pending irq */
6297 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6298 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6299 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006300
Avi Kivity3842d132010-07-27 12:30:24 +03006301 kvm_make_request(KVM_REQ_EVENT, vcpu);
6302
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006303 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006304 return 1;
6305}
6306
Avi Kivity851ba692009-08-24 11:10:17 +03006307static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006308{
Avi Kivityd3bef152007-06-05 15:53:05 +03006309 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006310}
6311
Avi Kivity851ba692009-08-24 11:10:17 +03006312static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006313{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006314 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006315}
6316
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006317static int handle_invd(struct kvm_vcpu *vcpu)
6318{
Andre Przywara51d8b662010-12-21 11:12:02 +01006319 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006320}
6321
Avi Kivity851ba692009-08-24 11:10:17 +03006322static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006323{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006324 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006325
6326 kvm_mmu_invlpg(vcpu, exit_qualification);
6327 skip_emulated_instruction(vcpu);
6328 return 1;
6329}
6330
Avi Kivityfee84b02011-11-10 14:57:25 +02006331static int handle_rdpmc(struct kvm_vcpu *vcpu)
6332{
6333 int err;
6334
6335 err = kvm_rdpmc(vcpu);
6336 kvm_complete_insn_gp(vcpu, err);
6337
6338 return 1;
6339}
6340
Avi Kivity851ba692009-08-24 11:10:17 +03006341static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006342{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006343 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006344 return 1;
6345}
6346
Dexuan Cui2acf9232010-06-10 11:27:12 +08006347static int handle_xsetbv(struct kvm_vcpu *vcpu)
6348{
6349 u64 new_bv = kvm_read_edx_eax(vcpu);
6350 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6351
6352 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6353 skip_emulated_instruction(vcpu);
6354 return 1;
6355}
6356
Wanpeng Lif53cd632014-12-02 19:14:58 +08006357static int handle_xsaves(struct kvm_vcpu *vcpu)
6358{
6359 skip_emulated_instruction(vcpu);
6360 WARN(1, "this should never happen\n");
6361 return 1;
6362}
6363
6364static int handle_xrstors(struct kvm_vcpu *vcpu)
6365{
6366 skip_emulated_instruction(vcpu);
6367 WARN(1, "this should never happen\n");
6368 return 1;
6369}
6370
Avi Kivity851ba692009-08-24 11:10:17 +03006371static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006372{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006373 if (likely(fasteoi)) {
6374 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6375 int access_type, offset;
6376
6377 access_type = exit_qualification & APIC_ACCESS_TYPE;
6378 offset = exit_qualification & APIC_ACCESS_OFFSET;
6379 /*
6380 * Sane guest uses MOV to write EOI, with written value
6381 * not cared. So make a short-circuit here by avoiding
6382 * heavy instruction emulation.
6383 */
6384 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6385 (offset == APIC_EOI)) {
6386 kvm_lapic_set_eoi(vcpu);
6387 skip_emulated_instruction(vcpu);
6388 return 1;
6389 }
6390 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006391 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006392}
6393
Yang Zhangc7c9c562013-01-25 10:18:51 +08006394static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6395{
6396 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6397 int vector = exit_qualification & 0xff;
6398
6399 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6400 kvm_apic_set_eoi_accelerated(vcpu, vector);
6401 return 1;
6402}
6403
Yang Zhang83d4c282013-01-25 10:18:49 +08006404static int handle_apic_write(struct kvm_vcpu *vcpu)
6405{
6406 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6407 u32 offset = exit_qualification & 0xfff;
6408
6409 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6410 kvm_apic_write_nodecode(vcpu, offset);
6411 return 1;
6412}
6413
Avi Kivity851ba692009-08-24 11:10:17 +03006414static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006415{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006416 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006417 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006418 bool has_error_code = false;
6419 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006420 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006421 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006422
6423 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006424 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006425 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006426
6427 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6428
6429 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006430 if (reason == TASK_SWITCH_GATE && idt_v) {
6431 switch (type) {
6432 case INTR_TYPE_NMI_INTR:
6433 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006434 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006435 break;
6436 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006437 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006438 kvm_clear_interrupt_queue(vcpu);
6439 break;
6440 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006441 if (vmx->idt_vectoring_info &
6442 VECTORING_INFO_DELIVER_CODE_MASK) {
6443 has_error_code = true;
6444 error_code =
6445 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6446 }
6447 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006448 case INTR_TYPE_SOFT_EXCEPTION:
6449 kvm_clear_exception_queue(vcpu);
6450 break;
6451 default:
6452 break;
6453 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006454 }
Izik Eidus37817f22008-03-24 23:14:53 +02006455 tss_selector = exit_qualification;
6456
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006457 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6458 type != INTR_TYPE_EXT_INTR &&
6459 type != INTR_TYPE_NMI_INTR))
6460 skip_emulated_instruction(vcpu);
6461
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006462 if (kvm_task_switch(vcpu, tss_selector,
6463 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6464 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006465 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6466 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6467 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006468 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006469 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006470
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006471 /*
6472 * TODO: What about debug traps on tss switch?
6473 * Are we supposed to inject them and update dr6?
6474 */
6475
6476 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006477}
6478
Avi Kivity851ba692009-08-24 11:10:17 +03006479static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006480{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006481 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006482 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006483 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006484 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006485
Sheng Yangf9c617f2009-03-25 10:08:52 +08006486 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006487
Sheng Yang14394422008-04-28 12:24:45 +08006488 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006489 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006490 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6491 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6492 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006493 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006494 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6495 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006496 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6497 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006498 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006499 }
6500
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006501 /*
6502 * EPT violation happened while executing iret from NMI,
6503 * "blocked by NMI" bit has to be set before next VM entry.
6504 * There are errata that may cause this bit to not be set:
6505 * AAK134, BY25.
6506 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006507 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6508 cpu_has_virtual_nmis() &&
6509 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006510 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6511
Sheng Yang14394422008-04-28 12:24:45 +08006512 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006513 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006514
Bandan Dasd95c5562016-07-12 18:18:51 -04006515 /* it is a read fault? */
6516 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6517 /* it is a write fault? */
6518 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006519 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006520 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006521 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006522 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006523
Yang Zhang25d92082013-08-06 12:00:32 +03006524 vcpu->arch.exit_qualification = exit_qualification;
6525
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006526 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006527}
6528
Avi Kivity851ba692009-08-24 11:10:17 +03006529static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006530{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006531 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006532 gpa_t gpa;
6533
6534 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006535 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006536 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006537 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006538 return 1;
6539 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006540
Paolo Bonzini450869d2015-11-04 13:41:21 +01006541 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006542 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006543 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6544 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006545
6546 if (unlikely(ret == RET_MMIO_PF_INVALID))
6547 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6548
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006549 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006550 return 1;
6551
6552 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006553 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006554
Avi Kivity851ba692009-08-24 11:10:17 +03006555 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6556 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006557
6558 return 0;
6559}
6560
Avi Kivity851ba692009-08-24 11:10:17 +03006561static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006562{
6563 u32 cpu_based_vm_exec_control;
6564
6565 /* clear pending NMI */
6566 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6567 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6568 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6569 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006570 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006571
6572 return 1;
6573}
6574
Mohammed Gamal80ced182009-09-01 12:48:18 +02006575static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006576{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006577 struct vcpu_vmx *vmx = to_vmx(vcpu);
6578 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006579 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006580 u32 cpu_exec_ctrl;
6581 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006582 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006583
6584 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6585 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006586
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006587 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006588 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006589 return handle_interrupt_window(&vmx->vcpu);
6590
Avi Kivityde87dcd2012-06-12 20:21:38 +03006591 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6592 return 1;
6593
Liran Alon114de9b2017-11-05 16:56:34 +02006594 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006595
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006596 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006597 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006598 ret = 0;
6599 goto out;
6600 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006601
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006602 if (err != EMULATE_DONE)
6603 goto emulation_error;
6604
6605 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6606 vcpu->arch.exception.pending)
6607 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006608
Gleb Natapov8d76c492013-05-08 18:38:44 +03006609 if (vcpu->arch.halt_request) {
6610 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006611 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006612 goto out;
6613 }
6614
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006615 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006616 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006617 if (need_resched())
6618 schedule();
6619 }
6620
Mohammed Gamal80ced182009-09-01 12:48:18 +02006621out:
6622 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006623
6624emulation_error:
6625 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6626 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6627 vcpu->run->internal.ndata = 0;
6628 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006629}
6630
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006631static int __grow_ple_window(int val)
6632{
6633 if (ple_window_grow < 1)
6634 return ple_window;
6635
6636 val = min(val, ple_window_actual_max);
6637
6638 if (ple_window_grow < ple_window)
6639 val *= ple_window_grow;
6640 else
6641 val += ple_window_grow;
6642
6643 return val;
6644}
6645
6646static int __shrink_ple_window(int val, int modifier, int minimum)
6647{
6648 if (modifier < 1)
6649 return ple_window;
6650
6651 if (modifier < ple_window)
6652 val /= modifier;
6653 else
6654 val -= modifier;
6655
6656 return max(val, minimum);
6657}
6658
6659static void grow_ple_window(struct kvm_vcpu *vcpu)
6660{
6661 struct vcpu_vmx *vmx = to_vmx(vcpu);
6662 int old = vmx->ple_window;
6663
6664 vmx->ple_window = __grow_ple_window(old);
6665
6666 if (vmx->ple_window != old)
6667 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006668
6669 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006670}
6671
6672static void shrink_ple_window(struct kvm_vcpu *vcpu)
6673{
6674 struct vcpu_vmx *vmx = to_vmx(vcpu);
6675 int old = vmx->ple_window;
6676
6677 vmx->ple_window = __shrink_ple_window(old,
6678 ple_window_shrink, ple_window);
6679
6680 if (vmx->ple_window != old)
6681 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006682
6683 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006684}
6685
6686/*
6687 * ple_window_actual_max is computed to be one grow_ple_window() below
6688 * ple_window_max. (See __grow_ple_window for the reason.)
6689 * This prevents overflows, because ple_window_max is int.
6690 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6691 * this process.
6692 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6693 */
6694static void update_ple_window_actual_max(void)
6695{
6696 ple_window_actual_max =
6697 __shrink_ple_window(max(ple_window_max, ple_window),
6698 ple_window_grow, INT_MIN);
6699}
6700
Feng Wubf9f6ac2015-09-18 22:29:55 +08006701/*
6702 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6703 */
6704static void wakeup_handler(void)
6705{
6706 struct kvm_vcpu *vcpu;
6707 int cpu = smp_processor_id();
6708
6709 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6710 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6711 blocked_vcpu_list) {
6712 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6713
6714 if (pi_test_on(pi_desc) == 1)
6715 kvm_vcpu_kick(vcpu);
6716 }
6717 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6718}
6719
Tiejun Chenf2c76482014-10-28 10:14:47 +08006720static __init int hardware_setup(void)
6721{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006722 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006723
6724 rdmsrl_safe(MSR_EFER, &host_efer);
6725
6726 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6727 kvm_define_shared_msr(i, vmx_msr_index[i]);
6728
6729 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6730 if (!vmx_io_bitmap_a)
6731 return r;
6732
6733 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6734 if (!vmx_io_bitmap_b)
6735 goto out;
6736
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006737 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6738 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006739 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006740
6741 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6742 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006743 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006744
6745 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6746 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6747
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006748 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006749
6750 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6751
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006752 if (setup_vmcs_config(&vmcs_config) < 0) {
6753 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006754 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006755 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006756
6757 if (boot_cpu_has(X86_FEATURE_NX))
6758 kvm_enable_efer_bits(EFER_NX);
6759
Wanpeng Li2df19692017-03-23 05:30:08 -07006760 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6761 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006762 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006763
Tiejun Chenf2c76482014-10-28 10:14:47 +08006764 if (!cpu_has_vmx_shadow_vmcs())
6765 enable_shadow_vmcs = 0;
6766 if (enable_shadow_vmcs)
6767 init_vmcs_shadow_fields();
6768
6769 if (!cpu_has_vmx_ept() ||
6770 !cpu_has_vmx_ept_4levels()) {
6771 enable_ept = 0;
6772 enable_unrestricted_guest = 0;
6773 enable_ept_ad_bits = 0;
6774 }
6775
6776 if (!cpu_has_vmx_ept_ad_bits())
6777 enable_ept_ad_bits = 0;
6778
6779 if (!cpu_has_vmx_unrestricted_guest())
6780 enable_unrestricted_guest = 0;
6781
Paolo Bonziniad15a292015-01-30 16:18:49 +01006782 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006783 flexpriority_enabled = 0;
6784
Paolo Bonziniad15a292015-01-30 16:18:49 +01006785 /*
6786 * set_apic_access_page_addr() is used to reload apic access
6787 * page upon invalidation. No need to do anything if not
6788 * using the APIC_ACCESS_ADDR VMCS field.
6789 */
6790 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006791 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006792
6793 if (!cpu_has_vmx_tpr_shadow())
6794 kvm_x86_ops->update_cr8_intercept = NULL;
6795
6796 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6797 kvm_disable_largepages();
6798
6799 if (!cpu_has_vmx_ple())
6800 ple_gap = 0;
6801
6802 if (!cpu_has_vmx_apicv())
6803 enable_apicv = 0;
6804
Haozhong Zhang64903d62015-10-20 15:39:09 +08006805 if (cpu_has_vmx_tsc_scaling()) {
6806 kvm_has_tsc_control = true;
6807 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6808 kvm_tsc_scaling_ratio_frac_bits = 48;
6809 }
6810
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006811 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6812
Tiejun Chenbaa03522014-12-23 16:21:11 +08006813 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006814 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006815 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6816 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006817 0ull, VMX_EPT_EXECUTABLE_MASK,
6818 cpu_has_vmx_ept_execute_only() ?
6819 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006820 ept_set_mmio_spte_mask();
6821 kvm_enable_tdp();
6822 } else
6823 kvm_disable_tdp();
6824
6825 update_ple_window_actual_max();
6826
Kai Huang843e4332015-01-28 10:54:28 +08006827 /*
6828 * Only enable PML when hardware supports PML feature, and both EPT
6829 * and EPT A/D bit features are enabled -- PML depends on them to work.
6830 */
6831 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6832 enable_pml = 0;
6833
6834 if (!enable_pml) {
6835 kvm_x86_ops->slot_enable_log_dirty = NULL;
6836 kvm_x86_ops->slot_disable_log_dirty = NULL;
6837 kvm_x86_ops->flush_log_dirty = NULL;
6838 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6839 }
6840
Yunhong Jiang64672c92016-06-13 14:19:59 -07006841 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6842 u64 vmx_msr;
6843
6844 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6845 cpu_preemption_timer_multi =
6846 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6847 } else {
6848 kvm_x86_ops->set_hv_timer = NULL;
6849 kvm_x86_ops->cancel_hv_timer = NULL;
6850 }
6851
Feng Wubf9f6ac2015-09-18 22:29:55 +08006852 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6853
Ashok Rajc45dcc72016-06-22 14:59:56 +08006854 kvm_mce_cap_supported |= MCG_LMCE_P;
6855
Tiejun Chenf2c76482014-10-28 10:14:47 +08006856 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006857
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006858out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006859 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006860out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006861 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006862out1:
6863 free_page((unsigned long)vmx_io_bitmap_b);
6864out:
6865 free_page((unsigned long)vmx_io_bitmap_a);
6866
6867 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006868}
6869
6870static __exit void hardware_unsetup(void)
6871{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006872 free_page((unsigned long)vmx_io_bitmap_b);
6873 free_page((unsigned long)vmx_io_bitmap_a);
6874 free_page((unsigned long)vmx_vmwrite_bitmap);
6875 free_page((unsigned long)vmx_vmread_bitmap);
6876
Tiejun Chenf2c76482014-10-28 10:14:47 +08006877 free_kvm_area();
6878}
6879
Avi Kivity6aa8b732006-12-10 02:21:36 -08006880/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006881 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6882 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6883 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006884static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006885{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006886 if (ple_gap)
6887 grow_ple_window(vcpu);
6888
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006889 skip_emulated_instruction(vcpu);
6890 kvm_vcpu_on_spin(vcpu);
6891
6892 return 1;
6893}
6894
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006895static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006896{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006897 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006898 return 1;
6899}
6900
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006901static int handle_mwait(struct kvm_vcpu *vcpu)
6902{
6903 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6904 return handle_nop(vcpu);
6905}
6906
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006907static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6908{
6909 return 1;
6910}
6911
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006912static int handle_monitor(struct kvm_vcpu *vcpu)
6913{
6914 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6915 return handle_nop(vcpu);
6916}
6917
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006918/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006919 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6920 * set the success or error code of an emulated VMX instruction, as specified
6921 * by Vol 2B, VMX Instruction Reference, "Conventions".
6922 */
6923static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6924{
6925 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6926 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6927 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6928}
6929
6930static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6931{
6932 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6933 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6934 X86_EFLAGS_SF | X86_EFLAGS_OF))
6935 | X86_EFLAGS_CF);
6936}
6937
Abel Gordon145c28d2013-04-18 14:36:55 +03006938static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006939 u32 vm_instruction_error)
6940{
6941 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6942 /*
6943 * failValid writes the error number to the current VMCS, which
6944 * can't be done there isn't a current VMCS.
6945 */
6946 nested_vmx_failInvalid(vcpu);
6947 return;
6948 }
6949 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6950 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6951 X86_EFLAGS_SF | X86_EFLAGS_OF))
6952 | X86_EFLAGS_ZF);
6953 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6954 /*
6955 * We don't need to force a shadow sync because
6956 * VM_INSTRUCTION_ERROR is not shadowed
6957 */
6958}
Abel Gordon145c28d2013-04-18 14:36:55 +03006959
Wincy Vanff651cb2014-12-11 08:52:58 +03006960static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6961{
6962 /* TODO: not to reset guest simply here. */
6963 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006964 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006965}
6966
Jan Kiszkaf4124502014-03-07 20:03:13 +01006967static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6968{
6969 struct vcpu_vmx *vmx =
6970 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6971
6972 vmx->nested.preemption_timer_expired = true;
6973 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6974 kvm_vcpu_kick(&vmx->vcpu);
6975
6976 return HRTIMER_NORESTART;
6977}
6978
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006979/*
Bandan Das19677e32014-05-06 02:19:15 -04006980 * Decode the memory-address operand of a vmx instruction, as recorded on an
6981 * exit caused by such an instruction (run by a guest hypervisor).
6982 * On success, returns 0. When the operand is invalid, returns 1 and throws
6983 * #UD or #GP.
6984 */
6985static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6986 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006987 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006988{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006989 gva_t off;
6990 bool exn;
6991 struct kvm_segment s;
6992
Bandan Das19677e32014-05-06 02:19:15 -04006993 /*
6994 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6995 * Execution", on an exit, vmx_instruction_info holds most of the
6996 * addressing components of the operand. Only the displacement part
6997 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6998 * For how an actual address is calculated from all these components,
6999 * refer to Vol. 1, "Operand Addressing".
7000 */
7001 int scaling = vmx_instruction_info & 3;
7002 int addr_size = (vmx_instruction_info >> 7) & 7;
7003 bool is_reg = vmx_instruction_info & (1u << 10);
7004 int seg_reg = (vmx_instruction_info >> 15) & 7;
7005 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7006 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7007 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7008 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7009
7010 if (is_reg) {
7011 kvm_queue_exception(vcpu, UD_VECTOR);
7012 return 1;
7013 }
7014
7015 /* Addr = segment_base + offset */
7016 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007017 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007018 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007019 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007020 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007021 off += kvm_register_read(vcpu, index_reg)<<scaling;
7022 vmx_get_segment(vcpu, &s, seg_reg);
7023 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007024
7025 if (addr_size == 1) /* 32 bit */
7026 *ret &= 0xffffffff;
7027
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007028 /* Checks for #GP/#SS exceptions. */
7029 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007030 if (is_long_mode(vcpu)) {
7031 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7032 * non-canonical form. This is the only check on the memory
7033 * destination for long mode!
7034 */
7035 exn = is_noncanonical_address(*ret);
7036 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007037 /* Protected mode: apply checks for segment validity in the
7038 * following order:
7039 * - segment type check (#GP(0) may be thrown)
7040 * - usability check (#GP(0)/#SS(0))
7041 * - limit check (#GP(0)/#SS(0))
7042 */
7043 if (wr)
7044 /* #GP(0) if the destination operand is located in a
7045 * read-only data segment or any code segment.
7046 */
7047 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7048 else
7049 /* #GP(0) if the source operand is located in an
7050 * execute-only code segment
7051 */
7052 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007053 if (exn) {
7054 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7055 return 1;
7056 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007057 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7058 */
7059 exn = (s.unusable != 0);
7060 /* Protected mode: #GP(0)/#SS(0) if the memory
7061 * operand is outside the segment limit.
7062 */
7063 exn = exn || (off + sizeof(u64) > s.limit);
7064 }
7065 if (exn) {
7066 kvm_queue_exception_e(vcpu,
7067 seg_reg == VCPU_SREG_SS ?
7068 SS_VECTOR : GP_VECTOR,
7069 0);
7070 return 1;
7071 }
7072
Bandan Das19677e32014-05-06 02:19:15 -04007073 return 0;
7074}
7075
7076/*
Bandan Das3573e222014-05-06 02:19:16 -04007077 * This function performs the various checks including
7078 * - if it's 4KB aligned
7079 * - No bits beyond the physical address width are set
7080 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04007081 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04007082 */
Bandan Das4291b582014-05-06 02:19:18 -04007083static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
7084 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007085{
7086 gva_t gva;
7087 gpa_t vmptr;
7088 struct x86_exception e;
7089 struct page *page;
7090 struct vcpu_vmx *vmx = to_vmx(vcpu);
7091 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7092
7093 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007094 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007095 return 1;
7096
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007097 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007098 kvm_inject_page_fault(vcpu, &e);
7099 return 1;
7100 }
7101
7102 switch (exit_reason) {
7103 case EXIT_REASON_VMON:
7104 /*
7105 * SDM 3: 24.11.5
7106 * The first 4 bytes of VMXON region contain the supported
7107 * VMCS revision identifier
7108 *
7109 * Note - IA32_VMX_BASIC[48] will never be 1
7110 * for the nested case;
7111 * which replaces physical address width with 32
7112 *
7113 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007114 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007115 nested_vmx_failInvalid(vcpu);
7116 skip_emulated_instruction(vcpu);
7117 return 1;
7118 }
7119
7120 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007121 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007122 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007123 skip_emulated_instruction(vcpu);
7124 return 1;
7125 }
7126 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007127 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007128 nested_release_page_clean(page);
7129 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007130 skip_emulated_instruction(vcpu);
7131 return 1;
7132 }
7133 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007134 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007135 vmx->nested.vmxon_ptr = vmptr;
7136 break;
Bandan Das4291b582014-05-06 02:19:18 -04007137 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007138 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007139 nested_vmx_failValid(vcpu,
7140 VMXERR_VMCLEAR_INVALID_ADDRESS);
7141 skip_emulated_instruction(vcpu);
7142 return 1;
7143 }
Bandan Das3573e222014-05-06 02:19:16 -04007144
Bandan Das4291b582014-05-06 02:19:18 -04007145 if (vmptr == vmx->nested.vmxon_ptr) {
7146 nested_vmx_failValid(vcpu,
7147 VMXERR_VMCLEAR_VMXON_POINTER);
7148 skip_emulated_instruction(vcpu);
7149 return 1;
7150 }
7151 break;
7152 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007153 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007154 nested_vmx_failValid(vcpu,
7155 VMXERR_VMPTRLD_INVALID_ADDRESS);
7156 skip_emulated_instruction(vcpu);
7157 return 1;
7158 }
7159
7160 if (vmptr == vmx->nested.vmxon_ptr) {
7161 nested_vmx_failValid(vcpu,
7162 VMXERR_VMCLEAR_VMXON_POINTER);
7163 skip_emulated_instruction(vcpu);
7164 return 1;
7165 }
7166 break;
Bandan Das3573e222014-05-06 02:19:16 -04007167 default:
7168 return 1; /* shouldn't happen */
7169 }
7170
Bandan Das4291b582014-05-06 02:19:18 -04007171 if (vmpointer)
7172 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007173 return 0;
7174}
7175
7176/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007177 * Emulate the VMXON instruction.
7178 * Currently, we just remember that VMX is active, and do not save or even
7179 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7180 * do not currently need to store anything in that guest-allocated memory
7181 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7182 * argument is different from the VMXON pointer (which the spec says they do).
7183 */
7184static int handle_vmon(struct kvm_vcpu *vcpu)
7185{
7186 struct kvm_segment cs;
7187 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007188 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007189 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7190 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007191 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007192
7193 /* The Intel VMX Instruction Reference lists a bunch of bits that
7194 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7195 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7196 * Otherwise, we should fail with #UD. We test these now:
7197 */
7198 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7199 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7200 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7201 kvm_queue_exception(vcpu, UD_VECTOR);
7202 return 1;
7203 }
7204
7205 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7206 if (is_long_mode(vcpu) && !cs.l) {
7207 kvm_queue_exception(vcpu, UD_VECTOR);
7208 return 1;
7209 }
7210
7211 if (vmx_get_cpl(vcpu)) {
7212 kvm_inject_gp(vcpu, 0);
7213 return 1;
7214 }
Bandan Das3573e222014-05-06 02:19:16 -04007215
Bandan Das4291b582014-05-06 02:19:18 -04007216 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007217 return 1;
7218
Abel Gordon145c28d2013-04-18 14:36:55 +03007219 if (vmx->nested.vmxon) {
7220 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7221 skip_emulated_instruction(vcpu);
7222 return 1;
7223 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007224
Haozhong Zhang3b840802016-06-22 14:59:54 +08007225 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007226 != VMXON_NEEDED_FEATURES) {
7227 kvm_inject_gp(vcpu, 0);
7228 return 1;
7229 }
7230
Paolo Bonziniff546f92018-01-11 12:16:15 +01007231 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7232 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007233 goto out_vmcs02;
Jim Mattson46e24df2017-11-27 17:22:25 -06007234
David Matlack4f2777b2016-07-13 17:16:37 -07007235 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7236 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007237 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007238
Abel Gordon8de48832013-04-18 14:37:25 +03007239 if (enable_shadow_vmcs) {
7240 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007241 if (!shadow_vmcs)
7242 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007243 /* mark vmcs as shadow */
7244 shadow_vmcs->revision_id |= (1u << 31);
7245 /* init shadow vmcs */
7246 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007247 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007248 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007249
Jan Kiszkaf4124502014-03-07 20:03:13 +01007250 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007251 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007252 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7253
Roman Kagan020a90f2018-07-19 21:59:07 +03007254 vmx->nested.vpid02 = allocate_vpid();
7255
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007256 vmx->nested.vmxon = true;
7257
7258 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007259 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007260 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007261
7262out_shadow_vmcs:
7263 kfree(vmx->nested.cached_vmcs12);
7264
7265out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007266 free_loaded_vmcs(&vmx->nested.vmcs02);
7267
7268out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007269 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007270}
7271
7272/*
7273 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7274 * for running VMX instructions (except VMXON, whose prerequisites are
7275 * slightly different). It also specifies what exception to inject otherwise.
7276 */
7277static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7278{
7279 struct kvm_segment cs;
7280 struct vcpu_vmx *vmx = to_vmx(vcpu);
7281
7282 if (!vmx->nested.vmxon) {
7283 kvm_queue_exception(vcpu, UD_VECTOR);
7284 return 0;
7285 }
7286
7287 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7288 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7289 (is_long_mode(vcpu) && !cs.l)) {
7290 kvm_queue_exception(vcpu, UD_VECTOR);
7291 return 0;
7292 }
7293
7294 if (vmx_get_cpl(vcpu)) {
7295 kvm_inject_gp(vcpu, 0);
7296 return 0;
7297 }
7298
7299 return 1;
7300}
7301
Abel Gordone7953d72013-04-18 14:37:55 +03007302static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7303{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007304 if (vmx->nested.current_vmptr == -1ull)
7305 return;
7306
7307 /* current_vmptr and current_vmcs12 are always set/reset together */
7308 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7309 return;
7310
Abel Gordon012f83c2013-04-18 14:39:25 +03007311 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007312 /* copy to memory all shadowed fields in case
7313 they were modified */
7314 copy_shadow_to_vmcs12(vmx);
7315 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007316 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7317 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007318 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007319 }
Wincy Van705699a2015-02-03 23:58:17 +08007320 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007321
7322 /* Flush VMCS12 to guest memory */
7323 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7324 VMCS12_SIZE);
7325
Abel Gordone7953d72013-04-18 14:37:55 +03007326 kunmap(vmx->nested.current_vmcs12_page);
7327 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007328 vmx->nested.current_vmptr = -1ull;
7329 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007330}
7331
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007332/*
7333 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7334 * just stops using VMX.
7335 */
7336static void free_nested(struct vcpu_vmx *vmx)
7337{
7338 if (!vmx->nested.vmxon)
7339 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007340
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007341 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007342 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007343 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007344 if (enable_shadow_vmcs) {
7345 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7346 free_vmcs(vmx->vmcs01.shadow_vmcs);
7347 vmx->vmcs01.shadow_vmcs = NULL;
7348 }
David Matlack4f2777b2016-07-13 17:16:37 -07007349 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007350 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007351 if (vmx->nested.apic_access_page) {
7352 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007353 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007354 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007355 if (vmx->nested.virtual_apic_page) {
7356 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007357 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007358 }
Wincy Van705699a2015-02-03 23:58:17 +08007359 if (vmx->nested.pi_desc_page) {
7360 kunmap(vmx->nested.pi_desc_page);
7361 nested_release_page(vmx->nested.pi_desc_page);
7362 vmx->nested.pi_desc_page = NULL;
7363 vmx->nested.pi_desc = NULL;
7364 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007365
Jim Mattson46e24df2017-11-27 17:22:25 -06007366 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007367}
7368
7369/* Emulate the VMXOFF instruction */
7370static int handle_vmoff(struct kvm_vcpu *vcpu)
7371{
7372 if (!nested_vmx_check_permission(vcpu))
7373 return 1;
7374 free_nested(to_vmx(vcpu));
7375 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007376 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007377 return 1;
7378}
7379
Nadav Har'El27d6c862011-05-25 23:06:59 +03007380/* Emulate the VMCLEAR instruction */
7381static int handle_vmclear(struct kvm_vcpu *vcpu)
7382{
7383 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007384 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007385 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007386
7387 if (!nested_vmx_check_permission(vcpu))
7388 return 1;
7389
Bandan Das4291b582014-05-06 02:19:18 -04007390 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007391 return 1;
7392
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007393 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007394 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007395
Jim Mattson29deec42017-03-02 12:41:48 -08007396 kvm_vcpu_write_guest(vcpu,
7397 vmptr + offsetof(struct vmcs12, launch_state),
7398 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007399
Nadav Har'El27d6c862011-05-25 23:06:59 +03007400 skip_emulated_instruction(vcpu);
7401 nested_vmx_succeed(vcpu);
7402 return 1;
7403}
7404
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007405static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7406
7407/* Emulate the VMLAUNCH instruction */
7408static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7409{
7410 return nested_vmx_run(vcpu, true);
7411}
7412
7413/* Emulate the VMRESUME instruction */
7414static int handle_vmresume(struct kvm_vcpu *vcpu)
7415{
7416
7417 return nested_vmx_run(vcpu, false);
7418}
7419
Nadav Har'El49f705c2011-05-25 23:08:30 +03007420enum vmcs_field_type {
7421 VMCS_FIELD_TYPE_U16 = 0,
7422 VMCS_FIELD_TYPE_U64 = 1,
7423 VMCS_FIELD_TYPE_U32 = 2,
7424 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7425};
7426
7427static inline int vmcs_field_type(unsigned long field)
7428{
7429 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7430 return VMCS_FIELD_TYPE_U32;
7431 return (field >> 13) & 0x3 ;
7432}
7433
7434static inline int vmcs_field_readonly(unsigned long field)
7435{
7436 return (((field >> 10) & 0x3) == 1);
7437}
7438
7439/*
7440 * Read a vmcs12 field. Since these can have varying lengths and we return
7441 * one type, we chose the biggest type (u64) and zero-extend the return value
7442 * to that size. Note that the caller, handle_vmread, might need to use only
7443 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7444 * 64-bit fields are to be returned).
7445 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007446static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7447 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007448{
7449 short offset = vmcs_field_to_offset(field);
7450 char *p;
7451
7452 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007453 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007454
7455 p = ((char *)(get_vmcs12(vcpu))) + offset;
7456
7457 switch (vmcs_field_type(field)) {
7458 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7459 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007460 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007461 case VMCS_FIELD_TYPE_U16:
7462 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007463 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007464 case VMCS_FIELD_TYPE_U32:
7465 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007466 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007467 case VMCS_FIELD_TYPE_U64:
7468 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007469 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007470 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007471 WARN_ON(1);
7472 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007473 }
7474}
7475
Abel Gordon20b97fe2013-04-18 14:36:25 +03007476
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007477static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7478 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007479 short offset = vmcs_field_to_offset(field);
7480 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7481 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007482 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007483
7484 switch (vmcs_field_type(field)) {
7485 case VMCS_FIELD_TYPE_U16:
7486 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007487 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007488 case VMCS_FIELD_TYPE_U32:
7489 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007490 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007491 case VMCS_FIELD_TYPE_U64:
7492 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007493 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007494 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7495 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007496 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007497 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007498 WARN_ON(1);
7499 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007500 }
7501
7502}
7503
Abel Gordon16f5b902013-04-18 14:38:25 +03007504static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7505{
7506 int i;
7507 unsigned long field;
7508 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007509 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007510 const unsigned long *fields = shadow_read_write_fields;
7511 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007512
Jan Kiszka282da872014-10-08 18:05:39 +02007513 preempt_disable();
7514
Abel Gordon16f5b902013-04-18 14:38:25 +03007515 vmcs_load(shadow_vmcs);
7516
7517 for (i = 0; i < num_fields; i++) {
7518 field = fields[i];
7519 switch (vmcs_field_type(field)) {
7520 case VMCS_FIELD_TYPE_U16:
7521 field_value = vmcs_read16(field);
7522 break;
7523 case VMCS_FIELD_TYPE_U32:
7524 field_value = vmcs_read32(field);
7525 break;
7526 case VMCS_FIELD_TYPE_U64:
7527 field_value = vmcs_read64(field);
7528 break;
7529 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7530 field_value = vmcs_readl(field);
7531 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007532 default:
7533 WARN_ON(1);
7534 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007535 }
7536 vmcs12_write_any(&vmx->vcpu, field, field_value);
7537 }
7538
7539 vmcs_clear(shadow_vmcs);
7540 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007541
7542 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007543}
7544
Abel Gordonc3114422013-04-18 14:38:55 +03007545static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7546{
Mathias Krausec2bae892013-06-26 20:36:21 +02007547 const unsigned long *fields[] = {
7548 shadow_read_write_fields,
7549 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007550 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007551 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007552 max_shadow_read_write_fields,
7553 max_shadow_read_only_fields
7554 };
7555 int i, q;
7556 unsigned long field;
7557 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007558 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007559
7560 vmcs_load(shadow_vmcs);
7561
Mathias Krausec2bae892013-06-26 20:36:21 +02007562 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007563 for (i = 0; i < max_fields[q]; i++) {
7564 field = fields[q][i];
7565 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7566
7567 switch (vmcs_field_type(field)) {
7568 case VMCS_FIELD_TYPE_U16:
7569 vmcs_write16(field, (u16)field_value);
7570 break;
7571 case VMCS_FIELD_TYPE_U32:
7572 vmcs_write32(field, (u32)field_value);
7573 break;
7574 case VMCS_FIELD_TYPE_U64:
7575 vmcs_write64(field, (u64)field_value);
7576 break;
7577 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7578 vmcs_writel(field, (long)field_value);
7579 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007580 default:
7581 WARN_ON(1);
7582 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007583 }
7584 }
7585 }
7586
7587 vmcs_clear(shadow_vmcs);
7588 vmcs_load(vmx->loaded_vmcs->vmcs);
7589}
7590
Nadav Har'El49f705c2011-05-25 23:08:30 +03007591/*
7592 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7593 * used before) all generate the same failure when it is missing.
7594 */
7595static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7596{
7597 struct vcpu_vmx *vmx = to_vmx(vcpu);
7598 if (vmx->nested.current_vmptr == -1ull) {
7599 nested_vmx_failInvalid(vcpu);
7600 skip_emulated_instruction(vcpu);
7601 return 0;
7602 }
7603 return 1;
7604}
7605
7606static int handle_vmread(struct kvm_vcpu *vcpu)
7607{
7608 unsigned long field;
7609 u64 field_value;
7610 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7611 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7612 gva_t gva = 0;
7613
7614 if (!nested_vmx_check_permission(vcpu) ||
7615 !nested_vmx_check_vmcs12(vcpu))
7616 return 1;
7617
7618 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007619 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007620 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007621 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007622 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7623 skip_emulated_instruction(vcpu);
7624 return 1;
7625 }
7626 /*
7627 * Now copy part of this value to register or memory, as requested.
7628 * Note that the number of bits actually copied is 32 or 64 depending
7629 * on the guest's mode (32 or 64 bit), not on the given field's length.
7630 */
7631 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007632 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007633 field_value);
7634 } else {
7635 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007636 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007637 return 1;
7638 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007639 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7640 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007641 }
7642
7643 nested_vmx_succeed(vcpu);
7644 skip_emulated_instruction(vcpu);
7645 return 1;
7646}
7647
7648
7649static int handle_vmwrite(struct kvm_vcpu *vcpu)
7650{
7651 unsigned long field;
7652 gva_t gva;
7653 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7654 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007655 /* The value to write might be 32 or 64 bits, depending on L1's long
7656 * mode, and eventually we need to write that into a field of several
7657 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007658 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007659 * bits into the vmcs12 field.
7660 */
7661 u64 field_value = 0;
7662 struct x86_exception e;
7663
7664 if (!nested_vmx_check_permission(vcpu) ||
7665 !nested_vmx_check_vmcs12(vcpu))
7666 return 1;
7667
7668 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007669 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007670 (((vmx_instruction_info) >> 3) & 0xf));
7671 else {
7672 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007673 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007674 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007675 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7676 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007677 kvm_inject_page_fault(vcpu, &e);
7678 return 1;
7679 }
7680 }
7681
7682
Nadav Amit27e6fb52014-06-18 17:19:26 +03007683 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007684 if (vmcs_field_readonly(field)) {
7685 nested_vmx_failValid(vcpu,
7686 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7687 skip_emulated_instruction(vcpu);
7688 return 1;
7689 }
7690
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007691 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007692 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7693 skip_emulated_instruction(vcpu);
7694 return 1;
7695 }
7696
7697 nested_vmx_succeed(vcpu);
7698 skip_emulated_instruction(vcpu);
7699 return 1;
7700}
7701
Nadav Har'El63846662011-05-25 23:07:29 +03007702/* Emulate the VMPTRLD instruction */
7703static int handle_vmptrld(struct kvm_vcpu *vcpu)
7704{
7705 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007706 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007707
7708 if (!nested_vmx_check_permission(vcpu))
7709 return 1;
7710
Bandan Das4291b582014-05-06 02:19:18 -04007711 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007712 return 1;
7713
Nadav Har'El63846662011-05-25 23:07:29 +03007714 if (vmx->nested.current_vmptr != vmptr) {
7715 struct vmcs12 *new_vmcs12;
7716 struct page *page;
7717 page = nested_get_page(vcpu, vmptr);
7718 if (page == NULL) {
7719 nested_vmx_failInvalid(vcpu);
7720 skip_emulated_instruction(vcpu);
7721 return 1;
7722 }
7723 new_vmcs12 = kmap(page);
7724 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7725 kunmap(page);
7726 nested_release_page_clean(page);
7727 nested_vmx_failValid(vcpu,
7728 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7729 skip_emulated_instruction(vcpu);
7730 return 1;
7731 }
Nadav Har'El63846662011-05-25 23:07:29 +03007732
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007733 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007734 vmx->nested.current_vmptr = vmptr;
7735 vmx->nested.current_vmcs12 = new_vmcs12;
7736 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007737 /*
7738 * Load VMCS12 from guest memory since it is not already
7739 * cached.
7740 */
7741 memcpy(vmx->nested.cached_vmcs12,
7742 vmx->nested.current_vmcs12, VMCS12_SIZE);
7743
Abel Gordon012f83c2013-04-18 14:39:25 +03007744 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007745 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7746 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007747 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007748 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007749 vmx->nested.sync_shadow_vmcs = true;
7750 }
Nadav Har'El63846662011-05-25 23:07:29 +03007751 }
7752
7753 nested_vmx_succeed(vcpu);
7754 skip_emulated_instruction(vcpu);
7755 return 1;
7756}
7757
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007758/* Emulate the VMPTRST instruction */
7759static int handle_vmptrst(struct kvm_vcpu *vcpu)
7760{
7761 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7762 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7763 gva_t vmcs_gva;
7764 struct x86_exception e;
7765
7766 if (!nested_vmx_check_permission(vcpu))
7767 return 1;
7768
7769 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007770 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007771 return 1;
7772 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007773 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7774 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7775 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007776 kvm_inject_page_fault(vcpu, &e);
7777 return 1;
7778 }
7779 nested_vmx_succeed(vcpu);
7780 skip_emulated_instruction(vcpu);
7781 return 1;
7782}
7783
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007784/* Emulate the INVEPT instruction */
7785static int handle_invept(struct kvm_vcpu *vcpu)
7786{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007787 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007788 u32 vmx_instruction_info, types;
7789 unsigned long type;
7790 gva_t gva;
7791 struct x86_exception e;
7792 struct {
7793 u64 eptp, gpa;
7794 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007795
Wincy Vanb9c237b2015-02-03 23:56:30 +08007796 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7797 SECONDARY_EXEC_ENABLE_EPT) ||
7798 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007799 kvm_queue_exception(vcpu, UD_VECTOR);
7800 return 1;
7801 }
7802
7803 if (!nested_vmx_check_permission(vcpu))
7804 return 1;
7805
7806 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7807 kvm_queue_exception(vcpu, UD_VECTOR);
7808 return 1;
7809 }
7810
7811 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007812 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007813
Wincy Vanb9c237b2015-02-03 23:56:30 +08007814 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007815
Jim Mattson85c856b2016-10-26 08:38:38 -07007816 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007817 nested_vmx_failValid(vcpu,
7818 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007819 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007820 return 1;
7821 }
7822
7823 /* According to the Intel VMX instruction reference, the memory
7824 * operand is read even if it isn't needed (e.g., for type==global)
7825 */
7826 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007827 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007828 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007829 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007830 kvm_inject_page_fault(vcpu, &e);
7831 return 1;
7832 }
7833
7834 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007835 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007836 /*
7837 * TODO: track mappings and invalidate
7838 * single context requests appropriately
7839 */
7840 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007841 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007842 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007843 nested_vmx_succeed(vcpu);
7844 break;
7845 default:
7846 BUG_ON(1);
7847 break;
7848 }
7849
7850 skip_emulated_instruction(vcpu);
7851 return 1;
7852}
7853
Petr Matouseka642fc32014-09-23 20:22:30 +02007854static int handle_invvpid(struct kvm_vcpu *vcpu)
7855{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007856 struct vcpu_vmx *vmx = to_vmx(vcpu);
7857 u32 vmx_instruction_info;
7858 unsigned long type, types;
7859 gva_t gva;
7860 struct x86_exception e;
7861 int vpid;
7862
7863 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7864 SECONDARY_EXEC_ENABLE_VPID) ||
7865 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7866 kvm_queue_exception(vcpu, UD_VECTOR);
7867 return 1;
7868 }
7869
7870 if (!nested_vmx_check_permission(vcpu))
7871 return 1;
7872
7873 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7874 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7875
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007876 types = (vmx->nested.nested_vmx_vpid_caps &
7877 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007878
Jim Mattson85c856b2016-10-26 08:38:38 -07007879 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007880 nested_vmx_failValid(vcpu,
7881 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007882 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007883 return 1;
7884 }
7885
7886 /* according to the intel vmx instruction reference, the memory
7887 * operand is read even if it isn't needed (e.g., for type==global)
7888 */
7889 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7890 vmx_instruction_info, false, &gva))
7891 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007892 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007893 kvm_inject_page_fault(vcpu, &e);
7894 return 1;
7895 }
7896
7897 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007898 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007899 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007900 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7901 if (!vpid) {
7902 nested_vmx_failValid(vcpu,
7903 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7904 skip_emulated_instruction(vcpu);
7905 return 1;
7906 }
7907 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007908 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007909 break;
7910 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007911 WARN_ON_ONCE(1);
7912 skip_emulated_instruction(vcpu);
7913 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007914 }
7915
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007916 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7917 nested_vmx_succeed(vcpu);
7918
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007919 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007920 return 1;
7921}
7922
Kai Huang843e4332015-01-28 10:54:28 +08007923static int handle_pml_full(struct kvm_vcpu *vcpu)
7924{
7925 unsigned long exit_qualification;
7926
7927 trace_kvm_pml_full(vcpu->vcpu_id);
7928
7929 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7930
7931 /*
7932 * PML buffer FULL happened while executing iret from NMI,
7933 * "blocked by NMI" bit has to be set before next VM entry.
7934 */
7935 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7936 cpu_has_virtual_nmis() &&
7937 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7938 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7939 GUEST_INTR_STATE_NMI);
7940
7941 /*
7942 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7943 * here.., and there's no userspace involvement needed for PML.
7944 */
7945 return 1;
7946}
7947
Yunhong Jiang64672c92016-06-13 14:19:59 -07007948static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7949{
7950 kvm_lapic_expired_hv_timer(vcpu);
7951 return 1;
7952}
7953
Nadav Har'El0140cae2011-05-25 23:06:28 +03007954/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007955 * The exit handlers return 1 if the exit was handled fully and guest execution
7956 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7957 * to be done to userspace and return 0.
7958 */
Mathias Krause772e0312012-08-30 01:30:19 +02007959static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007960 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7961 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007962 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007963 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007964 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007965 [EXIT_REASON_CR_ACCESS] = handle_cr,
7966 [EXIT_REASON_DR_ACCESS] = handle_dr,
7967 [EXIT_REASON_CPUID] = handle_cpuid,
7968 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7969 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7970 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7971 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007972 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007973 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007974 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007975 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007976 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007977 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007978 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007979 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007980 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007981 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007982 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007983 [EXIT_REASON_VMOFF] = handle_vmoff,
7984 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007985 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7986 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007987 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007988 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007989 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007990 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007991 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007992 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007993 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7994 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007995 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007996 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007997 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007998 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007999 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008000 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008001 [EXIT_REASON_XSAVES] = handle_xsaves,
8002 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008003 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008004 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008005};
8006
8007static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008008 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008009
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008010static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8011 struct vmcs12 *vmcs12)
8012{
8013 unsigned long exit_qualification;
8014 gpa_t bitmap, last_bitmap;
8015 unsigned int port;
8016 int size;
8017 u8 b;
8018
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008019 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008020 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008021
8022 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8023
8024 port = exit_qualification >> 16;
8025 size = (exit_qualification & 7) + 1;
8026
8027 last_bitmap = (gpa_t)-1;
8028 b = -1;
8029
8030 while (size > 0) {
8031 if (port < 0x8000)
8032 bitmap = vmcs12->io_bitmap_a;
8033 else if (port < 0x10000)
8034 bitmap = vmcs12->io_bitmap_b;
8035 else
Joe Perches1d804d02015-03-30 16:46:09 -07008036 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008037 bitmap += (port & 0x7fff) / 8;
8038
8039 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008040 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008041 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008042 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008043 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008044
8045 port++;
8046 size--;
8047 last_bitmap = bitmap;
8048 }
8049
Joe Perches1d804d02015-03-30 16:46:09 -07008050 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008051}
8052
Nadav Har'El644d7112011-05-25 23:12:35 +03008053/*
8054 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8055 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8056 * disinterest in the current event (read or write a specific MSR) by using an
8057 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8058 */
8059static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8060 struct vmcs12 *vmcs12, u32 exit_reason)
8061{
8062 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8063 gpa_t bitmap;
8064
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008065 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008066 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008067
8068 /*
8069 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8070 * for the four combinations of read/write and low/high MSR numbers.
8071 * First we need to figure out which of the four to use:
8072 */
8073 bitmap = vmcs12->msr_bitmap;
8074 if (exit_reason == EXIT_REASON_MSR_WRITE)
8075 bitmap += 2048;
8076 if (msr_index >= 0xc0000000) {
8077 msr_index -= 0xc0000000;
8078 bitmap += 1024;
8079 }
8080
8081 /* Then read the msr_index'th bit from this bitmap: */
8082 if (msr_index < 1024*8) {
8083 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008084 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008085 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008086 return 1 & (b >> (msr_index & 7));
8087 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008088 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008089}
8090
8091/*
8092 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8093 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8094 * intercept (via guest_host_mask etc.) the current event.
8095 */
8096static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8097 struct vmcs12 *vmcs12)
8098{
8099 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8100 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008101 int reg;
8102 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008103
8104 switch ((exit_qualification >> 4) & 3) {
8105 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008106 reg = (exit_qualification >> 8) & 15;
8107 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008108 switch (cr) {
8109 case 0:
8110 if (vmcs12->cr0_guest_host_mask &
8111 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008112 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008113 break;
8114 case 3:
8115 if ((vmcs12->cr3_target_count >= 1 &&
8116 vmcs12->cr3_target_value0 == val) ||
8117 (vmcs12->cr3_target_count >= 2 &&
8118 vmcs12->cr3_target_value1 == val) ||
8119 (vmcs12->cr3_target_count >= 3 &&
8120 vmcs12->cr3_target_value2 == val) ||
8121 (vmcs12->cr3_target_count >= 4 &&
8122 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008123 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008124 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008125 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008126 break;
8127 case 4:
8128 if (vmcs12->cr4_guest_host_mask &
8129 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008130 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008131 break;
8132 case 8:
8133 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008134 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008135 break;
8136 }
8137 break;
8138 case 2: /* clts */
8139 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8140 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008141 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008142 break;
8143 case 1: /* mov from cr */
8144 switch (cr) {
8145 case 3:
8146 if (vmcs12->cpu_based_vm_exec_control &
8147 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008148 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008149 break;
8150 case 8:
8151 if (vmcs12->cpu_based_vm_exec_control &
8152 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008153 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008154 break;
8155 }
8156 break;
8157 case 3: /* lmsw */
8158 /*
8159 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8160 * cr0. Other attempted changes are ignored, with no exit.
8161 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008162 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008163 if (vmcs12->cr0_guest_host_mask & 0xe &
8164 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008165 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008166 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8167 !(vmcs12->cr0_read_shadow & 0x1) &&
8168 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008169 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008170 break;
8171 }
Joe Perches1d804d02015-03-30 16:46:09 -07008172 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008173}
8174
8175/*
8176 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8177 * should handle it ourselves in L0 (and then continue L2). Only call this
8178 * when in is_guest_mode (L2).
8179 */
8180static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8181{
Nadav Har'El644d7112011-05-25 23:12:35 +03008182 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8183 struct vcpu_vmx *vmx = to_vmx(vcpu);
8184 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008185 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008186
Jan Kiszka542060e2014-01-04 18:47:21 +01008187 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8188 vmcs_readl(EXIT_QUALIFICATION),
8189 vmx->idt_vectoring_info,
8190 intr_info,
8191 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8192 KVM_ISA_VMX);
8193
David Matlackb7649e12017-08-01 14:00:40 -07008194 /*
8195 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008196 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8197 * Page). The CPU may write to these pages via their host
8198 * physical address while L2 is running, bypassing any
8199 * address-translation-based dirty tracking (e.g. EPT write
8200 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008201 *
8202 * Mark them dirty on every exit from L2 to prevent them from
8203 * getting out of sync with dirty tracking.
8204 */
8205 nested_mark_vmcs12_pages_dirty(vcpu);
8206
Nadav Har'El644d7112011-05-25 23:12:35 +03008207 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008208 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008209
8210 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008211 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8212 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008213 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008214 }
8215
8216 switch (exit_reason) {
8217 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008218 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008219 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008220 else if (is_page_fault(intr_info))
8221 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008222 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008223 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008224 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008225 else if (is_debug(intr_info) &&
8226 vcpu->guest_debug &
8227 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8228 return false;
8229 else if (is_breakpoint(intr_info) &&
8230 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8231 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008232 return vmcs12->exception_bitmap &
8233 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8234 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008235 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008236 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008237 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008238 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008239 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008240 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008241 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008242 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008243 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008244 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008245 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008246 case EXIT_REASON_HLT:
8247 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8248 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008249 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008250 case EXIT_REASON_INVLPG:
8251 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8252 case EXIT_REASON_RDPMC:
8253 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008254 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008255 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8256 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8257 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8258 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8259 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8260 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008261 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008262 /*
8263 * VMX instructions trap unconditionally. This allows L1 to
8264 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8265 */
Joe Perches1d804d02015-03-30 16:46:09 -07008266 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008267 case EXIT_REASON_CR_ACCESS:
8268 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8269 case EXIT_REASON_DR_ACCESS:
8270 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8271 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008272 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008273 case EXIT_REASON_MSR_READ:
8274 case EXIT_REASON_MSR_WRITE:
8275 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8276 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008277 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008278 case EXIT_REASON_MWAIT_INSTRUCTION:
8279 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008280 case EXIT_REASON_MONITOR_TRAP_FLAG:
8281 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008282 case EXIT_REASON_MONITOR_INSTRUCTION:
8283 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8284 case EXIT_REASON_PAUSE_INSTRUCTION:
8285 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8286 nested_cpu_has2(vmcs12,
8287 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8288 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008289 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008290 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008291 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008292 case EXIT_REASON_APIC_ACCESS:
8293 return nested_cpu_has2(vmcs12,
8294 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008295 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008296 case EXIT_REASON_EOI_INDUCED:
8297 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008298 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008299 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008300 /*
8301 * L0 always deals with the EPT violation. If nested EPT is
8302 * used, and the nested mmu code discovers that the address is
8303 * missing in the guest EPT table (EPT12), the EPT violation
8304 * will be injected with nested_ept_inject_page_fault()
8305 */
Joe Perches1d804d02015-03-30 16:46:09 -07008306 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008307 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008308 /*
8309 * L2 never uses directly L1's EPT, but rather L0's own EPT
8310 * table (shadow on EPT) or a merged EPT table that L0 built
8311 * (EPT on EPT). So any problems with the structure of the
8312 * table is L0's fault.
8313 */
Joe Perches1d804d02015-03-30 16:46:09 -07008314 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008315 case EXIT_REASON_WBINVD:
8316 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8317 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008318 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008319 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8320 /*
8321 * This should never happen, since it is not possible to
8322 * set XSS to a non-zero value---neither in L1 nor in L2.
8323 * If if it were, XSS would have to be checked against
8324 * the XSS exit bitmap in vmcs12.
8325 */
8326 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008327 case EXIT_REASON_PREEMPTION_TIMER:
8328 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008329 case EXIT_REASON_PML_FULL:
8330 /* We don't expose PML support to L1. */
8331 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008332 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008333 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008334 }
8335}
8336
Avi Kivity586f9602010-11-18 13:09:54 +02008337static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8338{
8339 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8340 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8341}
8342
Kai Huanga3eaa862015-11-04 13:46:05 +08008343static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008344{
Kai Huanga3eaa862015-11-04 13:46:05 +08008345 if (vmx->pml_pg) {
8346 __free_page(vmx->pml_pg);
8347 vmx->pml_pg = NULL;
8348 }
Kai Huang843e4332015-01-28 10:54:28 +08008349}
8350
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008351static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008352{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008353 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008354 u64 *pml_buf;
8355 u16 pml_idx;
8356
8357 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8358
8359 /* Do nothing if PML buffer is empty */
8360 if (pml_idx == (PML_ENTITY_NUM - 1))
8361 return;
8362
8363 /* PML index always points to next available PML buffer entity */
8364 if (pml_idx >= PML_ENTITY_NUM)
8365 pml_idx = 0;
8366 else
8367 pml_idx++;
8368
8369 pml_buf = page_address(vmx->pml_pg);
8370 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8371 u64 gpa;
8372
8373 gpa = pml_buf[pml_idx];
8374 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008375 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008376 }
8377
8378 /* reset PML index */
8379 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8380}
8381
8382/*
8383 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8384 * Called before reporting dirty_bitmap to userspace.
8385 */
8386static void kvm_flush_pml_buffers(struct kvm *kvm)
8387{
8388 int i;
8389 struct kvm_vcpu *vcpu;
8390 /*
8391 * We only need to kick vcpu out of guest mode here, as PML buffer
8392 * is flushed at beginning of all VMEXITs, and it's obvious that only
8393 * vcpus running in guest are possible to have unflushed GPAs in PML
8394 * buffer.
8395 */
8396 kvm_for_each_vcpu(i, vcpu, kvm)
8397 kvm_vcpu_kick(vcpu);
8398}
8399
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008400static void vmx_dump_sel(char *name, uint32_t sel)
8401{
8402 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008403 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008404 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8405 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8406 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8407}
8408
8409static void vmx_dump_dtsel(char *name, uint32_t limit)
8410{
8411 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8412 name, vmcs_read32(limit),
8413 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8414}
8415
8416static void dump_vmcs(void)
8417{
8418 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8419 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8420 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8421 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8422 u32 secondary_exec_control = 0;
8423 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008424 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008425 int i, n;
8426
8427 if (cpu_has_secondary_exec_ctrls())
8428 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8429
8430 pr_err("*** Guest State ***\n");
8431 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8432 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8433 vmcs_readl(CR0_GUEST_HOST_MASK));
8434 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8435 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8436 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8437 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8438 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8439 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008440 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8441 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8442 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8443 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008444 }
8445 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8446 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8447 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8448 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8449 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8450 vmcs_readl(GUEST_SYSENTER_ESP),
8451 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8452 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8453 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8454 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8455 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8456 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8457 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8458 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8459 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8460 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8461 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8462 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8463 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008464 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8465 efer, vmcs_read64(GUEST_IA32_PAT));
8466 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8467 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008468 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8469 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008470 pr_err("PerfGlobCtl = 0x%016llx\n",
8471 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008472 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008473 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008474 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8475 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8476 vmcs_read32(GUEST_ACTIVITY_STATE));
8477 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8478 pr_err("InterruptStatus = %04x\n",
8479 vmcs_read16(GUEST_INTR_STATUS));
8480
8481 pr_err("*** Host State ***\n");
8482 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8483 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8484 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8485 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8486 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8487 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8488 vmcs_read16(HOST_TR_SELECTOR));
8489 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8490 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8491 vmcs_readl(HOST_TR_BASE));
8492 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8493 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8494 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8495 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8496 vmcs_readl(HOST_CR4));
8497 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8498 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8499 vmcs_read32(HOST_IA32_SYSENTER_CS),
8500 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8501 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008502 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8503 vmcs_read64(HOST_IA32_EFER),
8504 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008505 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008506 pr_err("PerfGlobCtl = 0x%016llx\n",
8507 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008508
8509 pr_err("*** Control State ***\n");
8510 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8511 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8512 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8513 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8514 vmcs_read32(EXCEPTION_BITMAP),
8515 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8516 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8517 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8518 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8519 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8520 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8521 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8522 vmcs_read32(VM_EXIT_INTR_INFO),
8523 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8524 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8525 pr_err(" reason=%08x qualification=%016lx\n",
8526 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8527 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8528 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8529 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008530 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008531 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008532 pr_err("TSC Multiplier = 0x%016llx\n",
8533 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008534 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8535 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8536 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8537 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8538 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008539 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008540 n = vmcs_read32(CR3_TARGET_COUNT);
8541 for (i = 0; i + 1 < n; i += 4)
8542 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8543 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8544 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8545 if (i < n)
8546 pr_err("CR3 target%u=%016lx\n",
8547 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8548 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8549 pr_err("PLE Gap=%08x Window=%08x\n",
8550 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8551 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8552 pr_err("Virtual processor ID = 0x%04x\n",
8553 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8554}
8555
Avi Kivity6aa8b732006-12-10 02:21:36 -08008556/*
8557 * The guest has exited. See if we can fix it or if we need userspace
8558 * assistance.
8559 */
Avi Kivity851ba692009-08-24 11:10:17 +03008560static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008561{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008562 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008563 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008564 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008565
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008566 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8567
Kai Huang843e4332015-01-28 10:54:28 +08008568 /*
8569 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8570 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8571 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8572 * mode as if vcpus is in root mode, the PML buffer must has been
8573 * flushed already.
8574 */
8575 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008576 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008577
Mohammed Gamal80ced182009-09-01 12:48:18 +02008578 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008579 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008580 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008581
Nadav Har'El644d7112011-05-25 23:12:35 +03008582 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008583 nested_vmx_vmexit(vcpu, exit_reason,
8584 vmcs_read32(VM_EXIT_INTR_INFO),
8585 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008586 return 1;
8587 }
8588
Mohammed Gamal51207022010-05-31 22:40:54 +03008589 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008590 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008591 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8592 vcpu->run->fail_entry.hardware_entry_failure_reason
8593 = exit_reason;
8594 return 0;
8595 }
8596
Avi Kivity29bd8a72007-09-10 17:27:03 +03008597 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008598 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8599 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008600 = vmcs_read32(VM_INSTRUCTION_ERROR);
8601 return 0;
8602 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008603
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008604 /*
8605 * Note:
8606 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8607 * delivery event since it indicates guest is accessing MMIO.
8608 * The vm-exit can be triggered again after return to guest that
8609 * will cause infinite loop.
8610 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008611 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008612 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008613 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008614 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008615 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8616 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8617 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8618 vcpu->run->internal.ndata = 2;
8619 vcpu->run->internal.data[0] = vectoring_info;
8620 vcpu->run->internal.data[1] = exit_reason;
8621 return 0;
8622 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008623
Nadav Har'El644d7112011-05-25 23:12:35 +03008624 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8625 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008626 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008627 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008628 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008629 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008630 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008631 /*
8632 * This CPU don't support us in finding the end of an
8633 * NMI-blocked window if the guest runs with IRQs
8634 * disabled. So we pull the trigger after 1 s of
8635 * futile waiting, but inform the user about this.
8636 */
8637 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8638 "state on VCPU %d after 1 s timeout\n",
8639 __func__, vcpu->vcpu_id);
8640 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008641 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008642 }
8643
Avi Kivity6aa8b732006-12-10 02:21:36 -08008644 if (exit_reason < kvm_vmx_max_exit_handlers
8645 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008646 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008647 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008648 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8649 kvm_queue_exception(vcpu, UD_VECTOR);
8650 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008651 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008652}
8653
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008654/*
8655 * Software based L1D cache flush which is used when microcode providing
8656 * the cache control MSR is not loaded.
8657 *
8658 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8659 * flush it is required to read in 64 KiB because the replacement algorithm
8660 * is not exactly LRU. This could be sized at runtime via topology
8661 * information but as all relevant affected CPUs have 32KiB L1D cache size
8662 * there is no point in doing so.
8663 */
8664#define L1D_CACHE_ORDER 4
8665static void *vmx_l1d_flush_pages;
8666
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008667static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008668{
8669 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008670
8671 /*
Thomas Gleixner31282cf2018-07-13 16:23:17 +02008672 * This code is only executed when the the flush mode is 'cond' or
8673 * 'always'
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008674 */
Nicolai Stange936f5662018-07-21 22:25:00 +02008675 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stangee371c922018-07-27 13:22:16 +02008676 bool flush_l1d;
Nicolai Stange90bc3062018-07-21 22:35:28 +02008677
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008678 /*
Nicolai Stangee371c922018-07-27 13:22:16 +02008679 * Clear the per-vcpu flush bit, it gets set again
8680 * either from vcpu_run() or from one of the unsafe
8681 * VMEXIT handlers.
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008682 */
Nicolai Stangee371c922018-07-27 13:22:16 +02008683 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixnerdff09822018-07-13 16:23:20 +02008684 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stangee371c922018-07-27 13:22:16 +02008685
8686 /*
8687 * Clear the per-cpu flush bit, it gets set again from
8688 * the interrupt handlers.
8689 */
8690 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
8691 kvm_clear_cpu_l1tf_flush_l1d();
8692
Nicolai Stange90bc3062018-07-21 22:35:28 +02008693 if (!flush_l1d)
8694 return;
Nicolai Stange698ac1b2018-07-21 22:16:56 +02008695 }
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008696
8697 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008698
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008699 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8700 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8701 return;
8702 }
8703
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008704 asm volatile(
8705 /* First ensure the pages are in the TLB */
8706 "xorl %%eax, %%eax\n"
8707 ".Lpopulate_tlb:\n\t"
Nicolai Stange587d4992018-07-18 19:07:38 +02008708 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008709 "addl $4096, %%eax\n\t"
8710 "cmpl %%eax, %[size]\n\t"
8711 "jne .Lpopulate_tlb\n\t"
8712 "xorl %%eax, %%eax\n\t"
8713 "cpuid\n\t"
8714 /* Now fill the cache */
8715 "xorl %%eax, %%eax\n"
8716 ".Lfill_cache:\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008717 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008718 "addl $64, %%eax\n\t"
8719 "cmpl %%eax, %[size]\n\t"
8720 "jne .Lfill_cache\n\t"
8721 "lfence\n"
Nicolai Stange587d4992018-07-18 19:07:38 +02008722 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008723 [size] "r" (size)
8724 : "eax", "ebx", "ecx", "edx");
8725}
8726
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008727static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008728{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008729 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8730
8731 if (is_guest_mode(vcpu) &&
8732 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8733 return;
8734
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008735 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008736 vmcs_write32(TPR_THRESHOLD, 0);
8737 return;
8738 }
8739
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008740 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008741}
8742
Yang Zhang8d146952013-01-25 10:18:50 +08008743static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8744{
8745 u32 sec_exec_control;
8746
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008747 /* Postpone execution until vmcs01 is the current VMCS. */
8748 if (is_guest_mode(vcpu)) {
8749 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8750 return;
8751 }
8752
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008753 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008754 return;
8755
Paolo Bonzini35754c92015-07-29 12:05:37 +02008756 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008757 return;
8758
8759 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8760
8761 if (set) {
8762 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8763 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8764 } else {
8765 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8766 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008767 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008768 }
8769 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8770
Paolo Bonzini6236b782018-01-16 16:51:18 +01008771 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008772}
8773
Tang Chen38b99172014-09-24 15:57:54 +08008774static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8775{
8776 struct vcpu_vmx *vmx = to_vmx(vcpu);
8777
8778 /*
8779 * Currently we do not handle the nested case where L2 has an
8780 * APIC access page of its own; that page is still pinned.
8781 * Hence, we skip the case where the VCPU is in guest mode _and_
8782 * L1 prepared an APIC access page for L2.
8783 *
8784 * For the case where L1 and L2 share the same APIC access page
8785 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8786 * in the vmcs12), this function will only update either the vmcs01
8787 * or the vmcs02. If the former, the vmcs02 will be updated by
8788 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8789 * the next L2->L1 exit.
8790 */
8791 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008792 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008793 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008794 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008795 vmx_flush_tlb_ept_only(vcpu);
8796 }
Tang Chen38b99172014-09-24 15:57:54 +08008797}
8798
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008799static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008800{
8801 u16 status;
8802 u8 old;
8803
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008804 if (max_isr == -1)
8805 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008806
8807 status = vmcs_read16(GUEST_INTR_STATUS);
8808 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008809 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008810 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008811 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008812 vmcs_write16(GUEST_INTR_STATUS, status);
8813 }
8814}
8815
8816static void vmx_set_rvi(int vector)
8817{
8818 u16 status;
8819 u8 old;
8820
Wei Wang4114c272014-11-05 10:53:43 +08008821 if (vector == -1)
8822 vector = 0;
8823
Yang Zhangc7c9c562013-01-25 10:18:51 +08008824 status = vmcs_read16(GUEST_INTR_STATUS);
8825 old = (u8)status & 0xff;
8826 if ((u8)vector != old) {
8827 status &= ~0xff;
8828 status |= (u8)vector;
8829 vmcs_write16(GUEST_INTR_STATUS, status);
8830 }
8831}
8832
8833static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8834{
Wanpeng Li963fee12014-07-17 19:03:00 +08008835 if (!is_guest_mode(vcpu)) {
8836 vmx_set_rvi(max_irr);
8837 return;
8838 }
8839
Wei Wang4114c272014-11-05 10:53:43 +08008840 if (max_irr == -1)
8841 return;
8842
Wanpeng Li963fee12014-07-17 19:03:00 +08008843 /*
Wei Wang4114c272014-11-05 10:53:43 +08008844 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8845 * handles it.
8846 */
8847 if (nested_exit_on_intr(vcpu))
8848 return;
8849
8850 /*
8851 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008852 * is run without virtual interrupt delivery.
8853 */
8854 if (!kvm_event_needs_reinjection(vcpu) &&
8855 vmx_interrupt_allowed(vcpu)) {
8856 kvm_queue_interrupt(vcpu, max_irr, false);
8857 vmx_inject_irq(vcpu);
8858 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008859}
8860
Andrey Smetanin63086302015-11-10 15:36:32 +03008861static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008862{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008863 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008864 return;
8865
Yang Zhangc7c9c562013-01-25 10:18:51 +08008866 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8867 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8868 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8869 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8870}
8871
Avi Kivity51aa01d2010-07-20 14:31:20 +03008872static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008873{
Avi Kivity00eba012011-03-07 17:24:54 +02008874 u32 exit_intr_info;
8875
8876 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8877 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8878 return;
8879
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008880 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008881 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008882
8883 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008884 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008885 kvm_machine_check();
8886
Gleb Natapov20f65982009-05-11 13:35:55 +03008887 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008888 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008889 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008890 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008891 kvm_after_handle_nmi(&vmx->vcpu);
8892 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008893}
Gleb Natapov20f65982009-05-11 13:35:55 +03008894
Yang Zhanga547c6d2013-04-11 19:25:10 +08008895static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8896{
8897 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008898 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008899
8900 /*
8901 * If external interrupt exists, IF bit is set in rflags/eflags on the
8902 * interrupt stack frame, and interrupt will be enabled on a return
8903 * from interrupt handler.
8904 */
8905 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8906 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8907 unsigned int vector;
8908 unsigned long entry;
8909 gate_desc *desc;
8910 struct vcpu_vmx *vmx = to_vmx(vcpu);
8911#ifdef CONFIG_X86_64
8912 unsigned long tmp;
8913#endif
8914
8915 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8916 desc = (gate_desc *)vmx->host_idt_base + vector;
8917 entry = gate_offset(*desc);
8918 asm volatile(
8919#ifdef CONFIG_X86_64
8920 "mov %%" _ASM_SP ", %[sp]\n\t"
8921 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8922 "push $%c[ss]\n\t"
8923 "push %[sp]\n\t"
8924#endif
8925 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008926 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008927 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008928 :
8929#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008930 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008931#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008932 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008933 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008934 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008935 [ss]"i"(__KERNEL_DS),
8936 [cs]"i"(__KERNEL_CS)
8937 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008938 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008939}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008940STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008941
Tom Lendackyb9655922018-05-10 22:06:39 +02008942static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008943{
Tom Lendackyb9655922018-05-10 22:06:39 +02008944 switch (index) {
8945 case MSR_IA32_SMBASE:
8946 /*
8947 * We cannot do SMM unless we can run the guest in big
8948 * real mode.
8949 */
8950 return enable_unrestricted_guest || emulate_invalid_guest_state;
8951 case MSR_AMD64_VIRT_SPEC_CTRL:
8952 /* This is AMD only. */
8953 return false;
8954 default:
8955 return true;
8956 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008957}
8958
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008959static bool vmx_mpx_supported(void)
8960{
8961 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8962 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8963}
8964
Wanpeng Li55412b22014-12-02 19:21:30 +08008965static bool vmx_xsaves_supported(void)
8966{
8967 return vmcs_config.cpu_based_2nd_exec_ctrl &
8968 SECONDARY_EXEC_XSAVES;
8969}
8970
Avi Kivity51aa01d2010-07-20 14:31:20 +03008971static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8972{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008973 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008974 bool unblock_nmi;
8975 u8 vector;
8976 bool idtv_info_valid;
8977
8978 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008979
Avi Kivitycf393f72008-07-01 16:20:21 +03008980 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008981 if (vmx->nmi_known_unmasked)
8982 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008983 /*
8984 * Can't use vmx->exit_intr_info since we're not sure what
8985 * the exit reason is.
8986 */
8987 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008988 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8989 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8990 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008991 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008992 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8993 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008994 * SDM 3: 23.2.2 (September 2008)
8995 * Bit 12 is undefined in any of the following cases:
8996 * If the VM exit sets the valid bit in the IDT-vectoring
8997 * information field.
8998 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008999 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009000 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9001 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03009002 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9003 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02009004 else
9005 vmx->nmi_known_unmasked =
9006 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9007 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009008 } else if (unlikely(vmx->soft_vnmi_blocked))
9009 vmx->vnmi_blocked_time +=
9010 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009011}
9012
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009013static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009014 u32 idt_vectoring_info,
9015 int instr_len_field,
9016 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009017{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009018 u8 vector;
9019 int type;
9020 bool idtv_info_valid;
9021
9022 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009023
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009024 vcpu->arch.nmi_injected = false;
9025 kvm_clear_exception_queue(vcpu);
9026 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009027
9028 if (!idtv_info_valid)
9029 return;
9030
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009031 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009032
Avi Kivity668f6122008-07-02 09:28:55 +03009033 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9034 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009035
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009036 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009037 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009038 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009039 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009040 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009041 * Clear bit "block by NMI" before VM entry if a NMI
9042 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009043 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009044 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009045 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009046 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009047 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009048 /* fall through */
9049 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009050 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009051 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009052 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009053 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009054 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009055 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009056 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009057 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009058 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009059 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009060 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009061 break;
9062 default:
9063 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009064 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009065}
9066
Avi Kivity83422e12010-07-20 14:43:23 +03009067static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9068{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009069 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009070 VM_EXIT_INSTRUCTION_LEN,
9071 IDT_VECTORING_ERROR_CODE);
9072}
9073
Avi Kivityb463a6f2010-07-20 15:06:17 +03009074static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9075{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009076 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009077 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9078 VM_ENTRY_INSTRUCTION_LEN,
9079 VM_ENTRY_EXCEPTION_ERROR_CODE);
9080
9081 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9082}
9083
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009084static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9085{
9086 int i, nr_msrs;
9087 struct perf_guest_switch_msr *msrs;
9088
9089 msrs = perf_guest_get_msrs(&nr_msrs);
9090
9091 if (!msrs)
9092 return;
9093
9094 for (i = 0; i < nr_msrs; i++)
9095 if (msrs[i].host == msrs[i].guest)
9096 clear_atomic_switch_msr(vmx, msrs[i].msr);
9097 else
9098 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04009099 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009100}
9101
Yunhong Jiang64672c92016-06-13 14:19:59 -07009102void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
9103{
9104 struct vcpu_vmx *vmx = to_vmx(vcpu);
9105 u64 tscl;
9106 u32 delta_tsc;
9107
9108 if (vmx->hv_deadline_tsc == -1)
9109 return;
9110
9111 tscl = rdtsc();
9112 if (vmx->hv_deadline_tsc > tscl)
9113 /* sure to be 32 bit only because checked on set_hv_timer */
9114 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9115 cpu_preemption_timer_multi);
9116 else
9117 delta_tsc = 0;
9118
9119 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9120}
9121
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009122static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009123{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009124 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009125 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009126
9127 /* Record the guest's net vcpu time for enforced NMI injections. */
9128 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9129 vmx->entry_time = ktime_get();
9130
9131 /* Don't enter VMX if guest state is invalid, let the exit handler
9132 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009133 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009134 return;
9135
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009136 if (vmx->ple_window_dirty) {
9137 vmx->ple_window_dirty = false;
9138 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9139 }
9140
Abel Gordon012f83c2013-04-18 14:39:25 +03009141 if (vmx->nested.sync_shadow_vmcs) {
9142 copy_vmcs12_to_shadow(vmx);
9143 vmx->nested.sync_shadow_vmcs = false;
9144 }
9145
Avi Kivity104f2262010-11-18 13:12:52 +02009146 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9147 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9148 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9149 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9150
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009151 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009152 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9153 vmcs_writel(HOST_CR4, cr4);
9154 vmx->host_state.vmcs_host_cr4 = cr4;
9155 }
9156
Avi Kivity104f2262010-11-18 13:12:52 +02009157 /* When single-stepping over STI and MOV SS, we must clear the
9158 * corresponding interruptibility bits in the guest state. Otherwise
9159 * vmentry fails as it then expects bit 14 (BS) in pending debug
9160 * exceptions being set, but that's not correct for the guest debugging
9161 * case. */
9162 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9163 vmx_set_interrupt_shadow(vcpu, 0);
9164
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009165 if (vmx->guest_pkru_valid)
9166 __write_pkru(vmx->guest_pkru);
9167
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009168 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009169 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009170
Yunhong Jiang64672c92016-06-13 14:19:59 -07009171 vmx_arm_hv_timer(vcpu);
9172
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009173 /*
9174 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9175 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9176 * is no need to worry about the conditional branch over the wrmsr
9177 * being speculatively taken.
9178 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009179 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009180
Nadav Har'Eld462b812011-05-24 15:26:10 +03009181 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009182
Nicolai Stange90bc3062018-07-21 22:35:28 +02009183 if (static_branch_unlikely(&vmx_l1d_should_flush))
9184 vmx_l1d_flush(vcpu);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009185
Avi Kivity104f2262010-11-18 13:12:52 +02009186 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009187 /* Store host registers */
Avi Kivityb188c812012-09-16 15:10:58 +03009188 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9189 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9190 "push %%" _ASM_CX " \n\t"
9191 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009192 "je 1f \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009193 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009194 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009195 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009196 /* Reload cr2 if changed */
Avi Kivityb188c812012-09-16 15:10:58 +03009197 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9198 "mov %%cr2, %%" _ASM_DX " \n\t"
9199 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009200 "je 2f \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009201 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009202 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009203 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009204 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009205 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c812012-09-16 15:10:58 +03009206 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9207 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9208 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9209 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9210 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9211 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009212#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009213 "mov %c[r8](%0), %%r8 \n\t"
9214 "mov %c[r9](%0), %%r9 \n\t"
9215 "mov %c[r10](%0), %%r10 \n\t"
9216 "mov %c[r11](%0), %%r11 \n\t"
9217 "mov %c[r12](%0), %%r12 \n\t"
9218 "mov %c[r13](%0), %%r13 \n\t"
9219 "mov %c[r14](%0), %%r14 \n\t"
9220 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009221#endif
Avi Kivityb188c812012-09-16 15:10:58 +03009222 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009223
Avi Kivity6aa8b732006-12-10 02:21:36 -08009224 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009225 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009226 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009227 "jmp 2f \n\t"
9228 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9229 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009230 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c812012-09-16 15:10:58 +03009231 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009232 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009233 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009234 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9235 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9236 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9237 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9238 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9239 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9240 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009241#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009242 "mov %%r8, %c[r8](%0) \n\t"
9243 "mov %%r9, %c[r9](%0) \n\t"
9244 "mov %%r10, %c[r10](%0) \n\t"
9245 "mov %%r11, %c[r11](%0) \n\t"
9246 "mov %%r12, %c[r12](%0) \n\t"
9247 "mov %%r13, %c[r13](%0) \n\t"
9248 "mov %%r14, %c[r14](%0) \n\t"
9249 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009250 "xor %%r8d, %%r8d \n\t"
9251 "xor %%r9d, %%r9d \n\t"
9252 "xor %%r10d, %%r10d \n\t"
9253 "xor %%r11d, %%r11d \n\t"
9254 "xor %%r12d, %%r12d \n\t"
9255 "xor %%r13d, %%r13d \n\t"
9256 "xor %%r14d, %%r14d \n\t"
9257 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009258#endif
Avi Kivityb188c812012-09-16 15:10:58 +03009259 "mov %%cr2, %%" _ASM_AX " \n\t"
9260 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009261
Jim Mattson491c0ca2018-01-03 14:31:38 -08009262 "xor %%eax, %%eax \n\t"
9263 "xor %%ebx, %%ebx \n\t"
9264 "xor %%esi, %%esi \n\t"
9265 "xor %%edi, %%edi \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009266 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009267 ".pushsection .rodata \n\t"
9268 ".global vmx_return \n\t"
9269 "vmx_return: " _ASM_PTR " 2b \n\t"
9270 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009271 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009272 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009273 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009274 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009275 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9276 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9277 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9278 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9279 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9280 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9281 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009282#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009283 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9284 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9285 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9286 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9287 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9288 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9289 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9290 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009291#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009292 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9293 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009294 : "cc", "memory"
9295#ifdef CONFIG_X86_64
Avi Kivityb188c812012-09-16 15:10:58 +03009296 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009297 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c812012-09-16 15:10:58 +03009298#else
9299 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009300#endif
9301 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009302
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009303 /*
9304 * We do not use IBRS in the kernel. If this vCPU has used the
9305 * SPEC_CTRL MSR it may have left it on; save the value and
9306 * turn it off. This is much more efficient than blindly adding
9307 * it to the atomic save/restore list. Especially as the former
9308 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9309 *
9310 * For non-nested case:
9311 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9312 * save it.
9313 *
9314 * For nested case:
9315 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9316 * save it.
9317 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009318 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009319 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009320
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009321 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009322
David Woodhousec1ddd992018-01-12 11:11:27 +00009323 /* Eliminate branch target predictions from guest mode */
9324 vmexit_fill_RSB();
9325
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009326 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9327 if (debugctlmsr)
9328 update_debugctlmsr(debugctlmsr);
9329
Avi Kivityaa67f602012-08-01 16:48:03 +03009330#ifndef CONFIG_X86_64
9331 /*
9332 * The sysexit path does not restore ds/es, so we must set them to
9333 * a reasonable value ourselves.
9334 *
9335 * We can't defer this to vmx_load_host_state() since that function
9336 * may be executed in interrupt context, which saves and restore segments
9337 * around it, nullifying its effect.
9338 */
9339 loadsegment(ds, __USER_DS);
9340 loadsegment(es, __USER_DS);
9341#endif
9342
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009343 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009344 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009345 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009346 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009347 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009348 vcpu->arch.regs_dirty = 0;
9349
Avi Kivity1155f762007-11-22 11:30:47 +02009350 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9351
Nadav Har'Eld462b812011-05-24 15:26:10 +03009352 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009353
Avi Kivity51aa01d2010-07-20 14:31:20 +03009354 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009355
Gleb Natapove0b890d2013-09-25 12:51:33 +03009356 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009357 * eager fpu is enabled if PKEY is supported and CR4 is switched
9358 * back on host, so it is safe to read guest PKRU from current
9359 * XSAVE.
9360 */
9361 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9362 vmx->guest_pkru = __read_pkru();
9363 if (vmx->guest_pkru != vmx->host_pkru) {
9364 vmx->guest_pkru_valid = true;
9365 __write_pkru(vmx->host_pkru);
9366 } else
9367 vmx->guest_pkru_valid = false;
9368 }
9369
9370 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009371 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9372 * we did not inject a still-pending event to L1 now because of
9373 * nested_run_pending, we need to re-enable this bit.
9374 */
9375 if (vmx->nested.nested_run_pending)
9376 kvm_make_request(KVM_REQ_EVENT, vcpu);
9377
9378 vmx->nested.nested_run_pending = 0;
9379
Avi Kivity51aa01d2010-07-20 14:31:20 +03009380 vmx_complete_atomic_exit(vmx);
9381 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009382 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009383}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009384STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009385
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009386static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9387{
9388 struct vcpu_vmx *vmx = to_vmx(vcpu);
9389 int cpu;
9390
9391 if (vmx->loaded_vmcs == &vmx->vmcs01)
9392 return;
9393
9394 cpu = get_cpu();
9395 vmx->loaded_vmcs = &vmx->vmcs01;
9396 vmx_vcpu_put(vcpu);
9397 vmx_vcpu_load(vcpu, cpu);
9398 vcpu->cpu = cpu;
9399 put_cpu();
9400}
9401
Jim Mattson2f1fe812016-07-08 15:36:06 -07009402/*
9403 * Ensure that the current vmcs of the logical processor is the
9404 * vmcs01 of the vcpu before calling free_nested().
9405 */
9406static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9407{
9408 struct vcpu_vmx *vmx = to_vmx(vcpu);
9409 int r;
9410
9411 r = vcpu_load(vcpu);
9412 BUG_ON(r);
9413 vmx_load_vmcs01(vcpu);
9414 free_nested(vmx);
9415 vcpu_put(vcpu);
9416}
9417
Avi Kivity6aa8b732006-12-10 02:21:36 -08009418static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9419{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009420 struct vcpu_vmx *vmx = to_vmx(vcpu);
9421
Kai Huang843e4332015-01-28 10:54:28 +08009422 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009423 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009424 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009425 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009426 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009427 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009428 kfree(vmx->guest_msrs);
9429 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009430 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009431}
9432
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009433static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009434{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009435 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009436 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009437 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009438 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009439
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009440 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009441 return ERR_PTR(-ENOMEM);
9442
Wanpeng Li991e7a02015-09-16 17:30:05 +08009443 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009444
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009445 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9446 if (err)
9447 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009448
Peter Feiner4e595162016-07-07 14:49:58 -07009449 err = -ENOMEM;
9450
9451 /*
9452 * If PML is turned on, failure on enabling PML just results in failure
9453 * of creating the vcpu, therefore we can simplify PML logic (by
9454 * avoiding dealing with cases, such as enabling PML partially on vcpus
9455 * for the guest, etc.
9456 */
9457 if (enable_pml) {
9458 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9459 if (!vmx->pml_pg)
9460 goto uninit_vcpu;
9461 }
9462
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009463 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009464 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9465 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009466
Peter Feiner4e595162016-07-07 14:49:58 -07009467 if (!vmx->guest_msrs)
9468 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009469
Nadav Har'Eld462b812011-05-24 15:26:10 +03009470 if (!vmm_exclusive)
9471 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009472 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009473 if (!vmm_exclusive)
9474 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009475 if (err < 0)
9476 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009477
Paolo Bonzini6236b782018-01-16 16:51:18 +01009478 msr_bitmap = vmx->vmcs01.msr_bitmap;
9479 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9480 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9481 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9482 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9483 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9484 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9485 vmx->msr_bitmap_mode = 0;
9486
Paolo Bonziniff546f92018-01-11 12:16:15 +01009487 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009488 cpu = get_cpu();
9489 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009490 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009491 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009492 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009493 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009494 if (err)
9495 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009496 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009497 err = alloc_apic_access_page(kvm);
9498 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009499 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009500 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009501
Sheng Yangb927a3c2009-07-21 10:42:48 +08009502 if (enable_ept) {
9503 if (!kvm->arch.ept_identity_map_addr)
9504 kvm->arch.ept_identity_map_addr =
9505 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009506 err = init_rmode_identity_map(kvm);
9507 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009508 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009509 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009510
Roman Kagan020a90f2018-07-19 21:59:07 +03009511 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009512 nested_vmx_setup_ctls_msrs(vmx);
9513
Wincy Van705699a2015-02-03 23:58:17 +08009514 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009515 vmx->nested.current_vmptr = -1ull;
9516 vmx->nested.current_vmcs12 = NULL;
9517
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009518 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9519
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009520 /*
9521 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9522 * or POSTED_INTR_WAKEUP_VECTOR.
9523 */
9524 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9525 vmx->pi_desc.sn = 1;
9526
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009527 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009528
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009529free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009530 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009531free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009532 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009533free_pml:
9534 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009535uninit_vcpu:
9536 kvm_vcpu_uninit(&vmx->vcpu);
9537free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009538 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009539 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009540 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009541}
9542
Jiri Kosina2decbf52018-07-13 16:23:25 +02009543#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"
9544#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 -04009545
9546static int vmx_vm_init(struct kvm *kvm)
9547{
Jiri Kosina2decbf52018-07-13 16:23:25 +02009548 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
9549 switch (l1tf_mitigation) {
9550 case L1TF_MITIGATION_OFF:
9551 case L1TF_MITIGATION_FLUSH_NOWARN:
9552 /* 'I explicitly don't care' is set */
9553 break;
9554 case L1TF_MITIGATION_FLUSH:
9555 case L1TF_MITIGATION_FLUSH_NOSMT:
9556 case L1TF_MITIGATION_FULL:
9557 /*
9558 * Warn upon starting the first VM in a potentially
9559 * insecure environment.
9560 */
9561 if (cpu_smt_control == CPU_SMT_ENABLED)
9562 pr_warn_once(L1TF_MSG_SMT);
9563 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
9564 pr_warn_once(L1TF_MSG_L1D);
9565 break;
9566 case L1TF_MITIGATION_FULL_FORCE:
9567 /* Flush is enforced */
9568 break;
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009569 }
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009570 }
9571 return 0;
9572}
9573
Yang, Sheng002c7f72007-07-31 14:23:01 +03009574static void __init vmx_check_processor_compat(void *rtn)
9575{
9576 struct vmcs_config vmcs_conf;
9577
9578 *(int *)rtn = 0;
9579 if (setup_vmcs_config(&vmcs_conf) < 0)
9580 *(int *)rtn = -EIO;
9581 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9582 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9583 smp_processor_id());
9584 *(int *)rtn = -EIO;
9585 }
9586}
9587
Sheng Yang67253af2008-04-25 10:20:22 +08009588static int get_ept_level(void)
9589{
9590 return VMX_EPT_DEFAULT_GAW + 1;
9591}
9592
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009593static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009594{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009595 u8 cache;
9596 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009597
Sheng Yang522c68c2009-04-27 20:35:43 +08009598 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009599 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009600 * 2. EPT with VT-d:
9601 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009602 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009603 * b. VT-d with snooping control feature: snooping control feature of
9604 * VT-d engine can guarantee the cache correctness. Just set it
9605 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009606 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009607 * consistent with host MTRR
9608 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009609 if (is_mmio) {
9610 cache = MTRR_TYPE_UNCACHABLE;
9611 goto exit;
9612 }
9613
9614 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009615 ipat = VMX_EPT_IPAT_BIT;
9616 cache = MTRR_TYPE_WRBACK;
9617 goto exit;
9618 }
9619
9620 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9621 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009622 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009623 cache = MTRR_TYPE_WRBACK;
9624 else
9625 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009626 goto exit;
9627 }
9628
Xiao Guangrongff536042015-06-15 16:55:22 +08009629 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009630
9631exit:
9632 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009633}
9634
Sheng Yang17cc3932010-01-05 19:02:27 +08009635static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009636{
Sheng Yang878403b2010-01-05 19:02:29 +08009637 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9638 return PT_DIRECTORY_LEVEL;
9639 else
9640 /* For shadow and EPT supported 1GB page */
9641 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009642}
9643
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009644static void vmcs_set_secondary_exec_control(u32 new_ctl)
9645{
9646 /*
9647 * These bits in the secondary execution controls field
9648 * are dynamic, the others are mostly based on the hypervisor
9649 * architecture and the guest's CPUID. Do not touch the
9650 * dynamic bits.
9651 */
9652 u32 mask =
9653 SECONDARY_EXEC_SHADOW_VMCS |
9654 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9655 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9656
9657 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9658
9659 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9660 (new_ctl & ~mask) | (cur_ctl & mask));
9661}
9662
Sheng Yang0e851882009-12-18 16:48:46 +08009663static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9664{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009665 struct kvm_cpuid_entry2 *best;
9666 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009667 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009668
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009669 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009670 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9671 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009672 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009673
Paolo Bonzini8b972652015-09-15 17:34:42 +02009674 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009675 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009676 vmx->nested.nested_vmx_secondary_ctls_high |=
9677 SECONDARY_EXEC_RDTSCP;
9678 else
9679 vmx->nested.nested_vmx_secondary_ctls_high &=
9680 ~SECONDARY_EXEC_RDTSCP;
9681 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009682 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009683
Mao, Junjiead756a12012-07-02 01:18:48 +00009684 /* Exposing INVPCID only when PCID is exposed */
9685 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9686 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009687 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9688 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009689 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009690
Mao, Junjiead756a12012-07-02 01:18:48 +00009691 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009692 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009693 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009694
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009695 if (cpu_has_secondary_exec_ctrls())
9696 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009697
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009698 if (nested_vmx_allowed(vcpu))
9699 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9700 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9701 else
9702 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9703 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009704}
9705
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009706static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9707{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009708 if (func == 1 && nested)
9709 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009710}
9711
Yang Zhang25d92082013-08-06 12:00:32 +03009712static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9713 struct x86_exception *fault)
9714{
Jan Kiszka533558b2014-01-04 18:47:20 +01009715 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9716 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009717
9718 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009719 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009720 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009721 exit_reason = EXIT_REASON_EPT_VIOLATION;
9722 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009723 vmcs12->guest_physical_address = fault->address;
9724}
9725
Nadav Har'El155a97a2013-08-05 11:07:16 +03009726/* Callbacks for nested_ept_init_mmu_context: */
9727
9728static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9729{
9730 /* return the page table to be shadowed - in our case, EPT12 */
9731 return get_vmcs12(vcpu)->ept_pointer;
9732}
9733
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009734static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009735{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009736 WARN_ON(mmu_is_nested(vcpu));
9737 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009738 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9739 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009740 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9741 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9742 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9743
9744 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009745}
9746
9747static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9748{
9749 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9750}
9751
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009752static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9753 u16 error_code)
9754{
9755 bool inequality, bit;
9756
9757 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9758 inequality =
9759 (error_code & vmcs12->page_fault_error_code_mask) !=
9760 vmcs12->page_fault_error_code_match;
9761 return inequality ^ bit;
9762}
9763
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009764static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9765 struct x86_exception *fault)
9766{
9767 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9768
9769 WARN_ON(!is_guest_mode(vcpu));
9770
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009771 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009772 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9773 vmcs_read32(VM_EXIT_INTR_INFO),
9774 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009775 else
9776 kvm_inject_page_fault(vcpu, fault);
9777}
9778
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009779static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9780 struct vmcs12 *vmcs12)
9781{
9782 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009783 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009784
9785 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009786 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9787 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009788 return false;
9789
9790 /*
9791 * Translate L1 physical address to host physical
9792 * address for vmcs02. Keep the page pinned, so this
9793 * physical address remains valid. We keep a reference
9794 * to it so we can release it later.
9795 */
9796 if (vmx->nested.apic_access_page) /* shouldn't happen */
9797 nested_release_page(vmx->nested.apic_access_page);
9798 vmx->nested.apic_access_page =
9799 nested_get_page(vcpu, vmcs12->apic_access_addr);
9800 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009801
9802 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009803 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9804 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009805 return false;
9806
9807 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9808 nested_release_page(vmx->nested.virtual_apic_page);
9809 vmx->nested.virtual_apic_page =
9810 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9811
9812 /*
9813 * Failing the vm entry is _not_ what the processor does
9814 * but it's basically the only possibility we have.
9815 * We could still enter the guest if CR8 load exits are
9816 * enabled, CR8 store exits are enabled, and virtualize APIC
9817 * access is disabled; in this case the processor would never
9818 * use the TPR shadow and we could simply clear the bit from
9819 * the execution control. But such a configuration is useless,
9820 * so let's keep the code simple.
9821 */
9822 if (!vmx->nested.virtual_apic_page)
9823 return false;
9824 }
9825
Wincy Van705699a2015-02-03 23:58:17 +08009826 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009827 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9828 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009829 return false;
9830
9831 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9832 kunmap(vmx->nested.pi_desc_page);
9833 nested_release_page(vmx->nested.pi_desc_page);
9834 }
9835 vmx->nested.pi_desc_page =
9836 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9837 if (!vmx->nested.pi_desc_page)
9838 return false;
9839
9840 vmx->nested.pi_desc =
9841 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9842 if (!vmx->nested.pi_desc) {
9843 nested_release_page_clean(vmx->nested.pi_desc_page);
9844 return false;
9845 }
9846 vmx->nested.pi_desc =
9847 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9848 (unsigned long)(vmcs12->posted_intr_desc_addr &
9849 (PAGE_SIZE - 1)));
9850 }
9851
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009852 return true;
9853}
9854
Jan Kiszkaf4124502014-03-07 20:03:13 +01009855static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9856{
9857 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9858 struct vcpu_vmx *vmx = to_vmx(vcpu);
9859
9860 if (vcpu->arch.virtual_tsc_khz == 0)
9861 return;
9862
9863 /* Make sure short timeouts reliably trigger an immediate vmexit.
9864 * hrtimer_start does not guarantee this. */
9865 if (preemption_timeout <= 1) {
9866 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9867 return;
9868 }
9869
9870 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9871 preemption_timeout *= 1000000;
9872 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9873 hrtimer_start(&vmx->nested.preemption_timer,
9874 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9875}
9876
Wincy Van3af18d92015-02-03 23:49:31 +08009877static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9878 struct vmcs12 *vmcs12)
9879{
9880 int maxphyaddr;
9881 u64 addr;
9882
9883 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9884 return 0;
9885
9886 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9887 WARN_ON(1);
9888 return -EINVAL;
9889 }
9890 maxphyaddr = cpuid_maxphyaddr(vcpu);
9891
9892 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9893 ((addr + PAGE_SIZE) >> maxphyaddr))
9894 return -EINVAL;
9895
9896 return 0;
9897}
9898
9899/*
9900 * Merge L0's and L1's MSR bitmap, return false to indicate that
9901 * we do not use the hardware.
9902 */
9903static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9904 struct vmcs12 *vmcs12)
9905{
Wincy Van82f0dd42015-02-03 23:57:18 +08009906 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009907 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009908 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009909 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009910 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009911 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009912 *
9913 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9914 * ensures that we do not accidentally generate an L02 MSR bitmap
9915 * from the L12 MSR bitmap that is too permissive.
9916 * 2. That L1 or L2s have actually used the MSR. This avoids
9917 * unnecessarily merging of the bitmap if the MSR is unused. This
9918 * works properly because we only update the L01 MSR bitmap lazily.
9919 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9920 * updated to reflect this when L1 (or its L2s) actually write to
9921 * the MSR.
9922 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009923 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9924 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009925
Ashok Raj70131292018-02-01 22:59:43 +01009926 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009927 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009928 return false;
9929
9930 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009931 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009932 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009933 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009934
Radim Krčmářd048c092016-08-08 20:16:22 +02009935 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9936
Wincy Vanf2b93282015-02-03 23:56:03 +08009937 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009938 if (nested_cpu_has_apic_reg_virt(vmcs12))
9939 for (msr = 0x800; msr <= 0x8ff; msr++)
9940 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009941 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009942 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009943
9944 nested_vmx_disable_intercept_for_msr(
9945 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009946 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9947 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009948
Wincy Van608406e2015-02-03 23:57:51 +08009949 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009950 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009951 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009952 APIC_BASE_MSR + (APIC_EOI >> 4),
9953 MSR_TYPE_W);
9954 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009955 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009956 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9957 MSR_TYPE_W);
9958 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009959 }
Ashok Raj70131292018-02-01 22:59:43 +01009960
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009961 if (spec_ctrl)
9962 nested_vmx_disable_intercept_for_msr(
9963 msr_bitmap_l1, msr_bitmap_l0,
9964 MSR_IA32_SPEC_CTRL,
9965 MSR_TYPE_R | MSR_TYPE_W);
9966
Ashok Raj70131292018-02-01 22:59:43 +01009967 if (pred_cmd)
9968 nested_vmx_disable_intercept_for_msr(
9969 msr_bitmap_l1, msr_bitmap_l0,
9970 MSR_IA32_PRED_CMD,
9971 MSR_TYPE_W);
9972
Wincy Vanf2b93282015-02-03 23:56:03 +08009973 kunmap(page);
9974 nested_release_page_clean(page);
9975
9976 return true;
9977}
9978
9979static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9980 struct vmcs12 *vmcs12)
9981{
Wincy Van82f0dd42015-02-03 23:57:18 +08009982 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009983 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009984 !nested_cpu_has_vid(vmcs12) &&
9985 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009986 return 0;
9987
9988 /*
9989 * If virtualize x2apic mode is enabled,
9990 * virtualize apic access must be disabled.
9991 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009992 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9993 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009994 return -EINVAL;
9995
Wincy Van608406e2015-02-03 23:57:51 +08009996 /*
9997 * If virtual interrupt delivery is enabled,
9998 * we must exit on external interrupts.
9999 */
10000 if (nested_cpu_has_vid(vmcs12) &&
10001 !nested_exit_on_intr(vcpu))
10002 return -EINVAL;
10003
Wincy Van705699a2015-02-03 23:58:17 +080010004 /*
10005 * bits 15:8 should be zero in posted_intr_nv,
10006 * the descriptor address has been already checked
10007 * in nested_get_vmcs12_pages.
10008 */
10009 if (nested_cpu_has_posted_intr(vmcs12) &&
10010 (!nested_cpu_has_vid(vmcs12) ||
10011 !nested_exit_intr_ack_set(vcpu) ||
10012 vmcs12->posted_intr_nv & 0xff00))
10013 return -EINVAL;
10014
Wincy Vanf2b93282015-02-03 23:56:03 +080010015 /* tpr shadow is needed by all apicv features. */
10016 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10017 return -EINVAL;
10018
10019 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010020}
10021
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010022static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10023 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010024 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010025{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010026 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010027 u64 count, addr;
10028
10029 if (vmcs12_read_any(vcpu, count_field, &count) ||
10030 vmcs12_read_any(vcpu, addr_field, &addr)) {
10031 WARN_ON(1);
10032 return -EINVAL;
10033 }
10034 if (count == 0)
10035 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010036 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010037 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10038 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010039 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010040 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10041 addr_field, maxphyaddr, count, addr);
10042 return -EINVAL;
10043 }
10044 return 0;
10045}
10046
10047static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10048 struct vmcs12 *vmcs12)
10049{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010050 if (vmcs12->vm_exit_msr_load_count == 0 &&
10051 vmcs12->vm_exit_msr_store_count == 0 &&
10052 vmcs12->vm_entry_msr_load_count == 0)
10053 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010054 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010055 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010056 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010057 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010058 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010059 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010060 return -EINVAL;
10061 return 0;
10062}
10063
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010064static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10065 struct vmx_msr_entry *e)
10066{
10067 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010068 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010069 return -EINVAL;
10070 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10071 e->index == MSR_IA32_UCODE_REV)
10072 return -EINVAL;
10073 if (e->reserved != 0)
10074 return -EINVAL;
10075 return 0;
10076}
10077
10078static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10079 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010080{
10081 if (e->index == MSR_FS_BASE ||
10082 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010083 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10084 nested_vmx_msr_check_common(vcpu, e))
10085 return -EINVAL;
10086 return 0;
10087}
10088
10089static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10090 struct vmx_msr_entry *e)
10091{
10092 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10093 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010094 return -EINVAL;
10095 return 0;
10096}
10097
10098/*
10099 * Load guest's/host's msr at nested entry/exit.
10100 * return 0 for success, entry index for failure.
10101 */
10102static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10103{
10104 u32 i;
10105 struct vmx_msr_entry e;
10106 struct msr_data msr;
10107
10108 msr.host_initiated = false;
10109 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010110 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10111 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010112 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010113 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10114 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010115 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010116 }
10117 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010118 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010119 "%s check failed (%u, 0x%x, 0x%x)\n",
10120 __func__, i, e.index, e.reserved);
10121 goto fail;
10122 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010123 msr.index = e.index;
10124 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010125 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010126 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010127 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10128 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010129 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010130 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010131 }
10132 return 0;
10133fail:
10134 return i + 1;
10135}
10136
10137static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10138{
10139 u32 i;
10140 struct vmx_msr_entry e;
10141
10142 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010143 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010144 if (kvm_vcpu_read_guest(vcpu,
10145 gpa + i * sizeof(e),
10146 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010147 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010148 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10149 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010150 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010151 }
10152 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010153 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010154 "%s check failed (%u, 0x%x, 0x%x)\n",
10155 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010156 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010157 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010158 msr_info.host_initiated = false;
10159 msr_info.index = e.index;
10160 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010161 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010162 "%s cannot read MSR (%u, 0x%x)\n",
10163 __func__, i, e.index);
10164 return -EINVAL;
10165 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010166 if (kvm_vcpu_write_guest(vcpu,
10167 gpa + i * sizeof(e) +
10168 offsetof(struct vmx_msr_entry, value),
10169 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010170 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010171 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010172 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010173 return -EINVAL;
10174 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010175 }
10176 return 0;
10177}
10178
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010179/*
10180 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10181 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010182 * 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 +030010183 * guest in a way that will both be appropriate to L1's requests, and our
10184 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10185 * function also has additional necessary side-effects, like setting various
10186 * vcpu->arch fields.
10187 */
10188static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10189{
10190 struct vcpu_vmx *vmx = to_vmx(vcpu);
10191 u32 exec_control;
10192
10193 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10194 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10195 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10196 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10197 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10198 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10199 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10200 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10201 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10202 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10203 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10204 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10205 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10206 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10207 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10208 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10209 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10210 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10211 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10212 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10213 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10214 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10215 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10216 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10217 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10218 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10219 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10220 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10221 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10222 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10223 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10224 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10225 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10226 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10227 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10228 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10229
Jan Kiszka2996fca2014-06-16 13:59:43 +020010230 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10231 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10232 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10233 } else {
10234 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10235 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10236 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010237 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10238 vmcs12->vm_entry_intr_info_field);
10239 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10240 vmcs12->vm_entry_exception_error_code);
10241 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10242 vmcs12->vm_entry_instruction_len);
10243 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10244 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010245 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010246 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010247 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10248 vmcs12->guest_pending_dbg_exceptions);
10249 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10250 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10251
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010252 if (nested_cpu_has_xsaves(vmcs12))
10253 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010254 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10255
Jan Kiszkaf4124502014-03-07 20:03:13 +010010256 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010257
Paolo Bonzini93140062016-07-06 13:23:51 +020010258 /* Preemption timer setting is only taken from vmcs01. */
10259 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10260 exec_control |= vmcs_config.pin_based_exec_ctrl;
10261 if (vmx->hv_deadline_tsc == -1)
10262 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10263
10264 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010265 if (nested_cpu_has_posted_intr(vmcs12)) {
10266 /*
10267 * Note that we use L0's vector here and in
10268 * vmx_deliver_nested_posted_interrupt.
10269 */
10270 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10271 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010272 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010273 vmcs_write64(POSTED_INTR_DESC_ADDR,
10274 page_to_phys(vmx->nested.pi_desc_page) +
10275 (unsigned long)(vmcs12->posted_intr_desc_addr &
10276 (PAGE_SIZE - 1)));
10277 } else
10278 exec_control &= ~PIN_BASED_POSTED_INTR;
10279
Jan Kiszkaf4124502014-03-07 20:03:13 +010010280 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010281
Jan Kiszkaf4124502014-03-07 20:03:13 +010010282 vmx->nested.preemption_timer_expired = false;
10283 if (nested_cpu_has_preemption_timer(vmcs12))
10284 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010285
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010286 /*
10287 * Whether page-faults are trapped is determined by a combination of
10288 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10289 * If enable_ept, L0 doesn't care about page faults and we should
10290 * set all of these to L1's desires. However, if !enable_ept, L0 does
10291 * care about (at least some) page faults, and because it is not easy
10292 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10293 * to exit on each and every L2 page fault. This is done by setting
10294 * MASK=MATCH=0 and (see below) EB.PF=1.
10295 * Note that below we don't need special code to set EB.PF beyond the
10296 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10297 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10298 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10299 *
10300 * A problem with this approach (when !enable_ept) is that L1 may be
10301 * injected with more page faults than it asked for. This could have
10302 * caused problems, but in practice existing hypervisors don't care.
10303 * To fix this, we will need to emulate the PFEC checking (on the L1
10304 * page tables), using walk_addr(), when injecting PFs to L1.
10305 */
10306 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10307 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10308 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10309 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10310
10311 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010312 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010313
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010314 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010315 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010316 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010317 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010318 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010319 if (nested_cpu_has(vmcs12,
10320 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10321 exec_control |= vmcs12->secondary_vm_exec_control;
10322
10323 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10324 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010325 * If translation failed, no matter: This feature asks
10326 * to exit when accessing the given address, and if it
10327 * can never be accessed, this feature won't do
10328 * anything anyway.
10329 */
10330 if (!vmx->nested.apic_access_page)
10331 exec_control &=
10332 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10333 else
10334 vmcs_write64(APIC_ACCESS_ADDR,
10335 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010336 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010337 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010338 exec_control |=
10339 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010340 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010341 }
10342
Wincy Van608406e2015-02-03 23:57:51 +080010343 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10344 vmcs_write64(EOI_EXIT_BITMAP0,
10345 vmcs12->eoi_exit_bitmap0);
10346 vmcs_write64(EOI_EXIT_BITMAP1,
10347 vmcs12->eoi_exit_bitmap1);
10348 vmcs_write64(EOI_EXIT_BITMAP2,
10349 vmcs12->eoi_exit_bitmap2);
10350 vmcs_write64(EOI_EXIT_BITMAP3,
10351 vmcs12->eoi_exit_bitmap3);
10352 vmcs_write16(GUEST_INTR_STATUS,
10353 vmcs12->guest_intr_status);
10354 }
10355
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010356 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10357 }
10358
10359
10360 /*
10361 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10362 * Some constant fields are set here by vmx_set_constant_host_state().
10363 * Other fields are different per CPU, and will be set later when
10364 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10365 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010366 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010367
10368 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010369 * Set the MSR load/store lists to match L0's settings.
10370 */
10371 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010372 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10373 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10374 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10375 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010376
10377 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010378 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10379 * entry, but only if the current (host) sp changed from the value
10380 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10381 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10382 * here we just force the write to happen on entry.
10383 */
10384 vmx->host_rsp = 0;
10385
10386 exec_control = vmx_exec_control(vmx); /* L0's desires */
10387 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10388 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10389 exec_control &= ~CPU_BASED_TPR_SHADOW;
10390 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010391
10392 if (exec_control & CPU_BASED_TPR_SHADOW) {
10393 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10394 page_to_phys(vmx->nested.virtual_apic_page));
10395 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010396 } else {
10397#ifdef CONFIG_X86_64
10398 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10399 CPU_BASED_CR8_STORE_EXITING;
10400#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010401 }
10402
Wincy Van3af18d92015-02-03 23:49:31 +080010403 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010404 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10405 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10406 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10407 else
Wincy Van3af18d92015-02-03 23:49:31 +080010408 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10409
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010410 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010411 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010412 * Rather, exit every time.
10413 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010414 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10415 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10416
10417 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10418
10419 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10420 * bitwise-or of what L1 wants to trap for L2, and what we want to
10421 * trap. Note that CR0.TS also needs updating - we do this later.
10422 */
10423 update_exception_bitmap(vcpu);
10424 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10425 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10426
Nadav Har'El8049d652013-08-05 11:07:06 +030010427 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10428 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10429 * bits are further modified by vmx_set_efer() below.
10430 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010431 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010432
10433 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10434 * emulated by vmx_set_efer(), below.
10435 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010436 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010437 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10438 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010439 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10440
Jan Kiszka44811c02013-08-04 17:17:27 +020010441 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010442 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010443 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10444 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010445 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10446
10447
10448 set_cr4_guest_host_mask(vmx);
10449
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010450 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10451 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10452
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010453 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10454 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010455 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010456 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010457 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010458 if (kvm_has_tsc_control)
10459 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010460
Paolo Bonzini6236b782018-01-16 16:51:18 +010010461 if (cpu_has_vmx_msr_bitmap())
10462 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10463
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010464 if (enable_vpid) {
10465 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010466 * There is no direct mapping between vpid02 and vpid12, the
10467 * vpid02 is per-vCPU for L0 and reused while the value of
10468 * vpid12 is changed w/ one invvpid during nested vmentry.
10469 * The vpid12 is allocated by L1 for L2, so it will not
10470 * influence global bitmap(for vpid01 and vpid02 allocation)
10471 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010472 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010473 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10474 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10475 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10476 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10477 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10478 }
10479 } else {
10480 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10481 vmx_flush_tlb(vcpu);
10482 }
10483
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010484 }
10485
Ladi Prosek560a9792017-04-04 14:18:53 +020010486 if (enable_pml) {
10487 /*
10488 * Conceptually we want to copy the PML address and index from
10489 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10490 * since we always flush the log on each vmexit, this happens
10491 * to be equivalent to simply resetting the fields in vmcs02.
10492 */
10493 ASSERT(vmx->pml_pg);
10494 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10495 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10496 }
10497
Nadav Har'El155a97a2013-08-05 11:07:16 +030010498 if (nested_cpu_has_ept(vmcs12)) {
10499 kvm_mmu_unload(vcpu);
10500 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010501 } else if (nested_cpu_has2(vmcs12,
10502 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10503 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010504 }
10505
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010506 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10507 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010508 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010509 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10510 else
10511 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10512 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10513 vmx_set_efer(vcpu, vcpu->arch.efer);
10514
10515 /*
10516 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10517 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10518 * The CR0_READ_SHADOW is what L2 should have expected to read given
10519 * the specifications by L1; It's not enough to take
10520 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10521 * have more bits than L1 expected.
10522 */
10523 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10524 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10525
10526 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10527 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10528
10529 /* shadow page tables on either EPT or shadow page tables */
10530 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10531 kvm_mmu_reset_context(vcpu);
10532
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010533 if (!enable_ept)
10534 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10535
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010536 /*
10537 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10538 */
10539 if (enable_ept) {
10540 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10541 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10542 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10543 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10544 }
10545
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010546 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10547 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10548}
10549
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010550/*
10551 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10552 * for running an L2 nested guest.
10553 */
10554static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10555{
10556 struct vmcs12 *vmcs12;
10557 struct vcpu_vmx *vmx = to_vmx(vcpu);
10558 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010559 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010560 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010561
10562 if (!nested_vmx_check_permission(vcpu) ||
10563 !nested_vmx_check_vmcs12(vcpu))
10564 return 1;
10565
10566 skip_emulated_instruction(vcpu);
10567 vmcs12 = get_vmcs12(vcpu);
10568
Abel Gordon012f83c2013-04-18 14:39:25 +030010569 if (enable_shadow_vmcs)
10570 copy_shadow_to_vmcs12(vmx);
10571
Nadav Har'El7c177932011-05-25 23:12:04 +030010572 /*
10573 * The nested entry process starts with enforcing various prerequisites
10574 * on vmcs12 as required by the Intel SDM, and act appropriately when
10575 * they fail: As the SDM explains, some conditions should cause the
10576 * instruction to fail, while others will cause the instruction to seem
10577 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10578 * To speed up the normal (success) code path, we should avoid checking
10579 * for misconfigurations which will anyway be caught by the processor
10580 * when using the merged vmcs02.
10581 */
10582 if (vmcs12->launch_state == launch) {
10583 nested_vmx_failValid(vcpu,
10584 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10585 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10586 return 1;
10587 }
10588
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010589 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10590 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010591 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10592 return 1;
10593 }
10594
Wincy Van3af18d92015-02-03 23:49:31 +080010595 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010596 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10597 return 1;
10598 }
10599
Wincy Van3af18d92015-02-03 23:49:31 +080010600 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010601 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10602 return 1;
10603 }
10604
Wincy Vanf2b93282015-02-03 23:56:03 +080010605 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10606 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10607 return 1;
10608 }
10609
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010610 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10611 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10612 return 1;
10613 }
10614
Nadav Har'El7c177932011-05-25 23:12:04 +030010615 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010616 vmx->nested.nested_vmx_true_procbased_ctls_low,
10617 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010618 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010619 vmx->nested.nested_vmx_secondary_ctls_low,
10620 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010621 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010622 vmx->nested.nested_vmx_pinbased_ctls_low,
10623 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010624 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010625 vmx->nested.nested_vmx_true_exit_ctls_low,
10626 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010627 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010628 vmx->nested.nested_vmx_true_entry_ctls_low,
10629 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010630 {
10631 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10632 return 1;
10633 }
10634
10635 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10636 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10637 nested_vmx_failValid(vcpu,
10638 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10639 return 1;
10640 }
10641
Wincy Vanb9c237b2015-02-03 23:56:30 +080010642 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010643 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10644 nested_vmx_entry_failure(vcpu, vmcs12,
10645 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10646 return 1;
10647 }
10648 if (vmcs12->vmcs_link_pointer != -1ull) {
10649 nested_vmx_entry_failure(vcpu, vmcs12,
10650 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10651 return 1;
10652 }
10653
10654 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010655 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010656 * are performed on the field for the IA32_EFER MSR:
10657 * - Bits reserved in the IA32_EFER MSR must be 0.
10658 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10659 * the IA-32e mode guest VM-exit control. It must also be identical
10660 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10661 * CR0.PG) is 1.
10662 */
10663 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10664 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10665 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10666 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10667 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10668 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10669 nested_vmx_entry_failure(vcpu, vmcs12,
10670 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10671 return 1;
10672 }
10673 }
10674
10675 /*
10676 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10677 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10678 * the values of the LMA and LME bits in the field must each be that of
10679 * the host address-space size VM-exit control.
10680 */
10681 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10682 ia32e = (vmcs12->vm_exit_controls &
10683 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10684 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10685 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10686 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10687 nested_vmx_entry_failure(vcpu, vmcs12,
10688 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10689 return 1;
10690 }
10691 }
10692
10693 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010694 * We're finally done with prerequisite checking, and can start with
10695 * the nested entry.
10696 */
10697
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010698 enter_guest_mode(vcpu);
10699
Jan Kiszka2996fca2014-06-16 13:59:43 +020010700 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10701 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10702
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010703 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010704 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010705 vmx_vcpu_put(vcpu);
10706 vmx_vcpu_load(vcpu, cpu);
10707 vcpu->cpu = cpu;
10708 put_cpu();
10709
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010710 vmx_segment_cache_clear(vmx);
10711
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010712 prepare_vmcs02(vcpu, vmcs12);
10713
Wincy Vanff651cb2014-12-11 08:52:58 +030010714 msr_entry_idx = nested_vmx_load_msr(vcpu,
10715 vmcs12->vm_entry_msr_load_addr,
10716 vmcs12->vm_entry_msr_load_count);
10717 if (msr_entry_idx) {
10718 leave_guest_mode(vcpu);
10719 vmx_load_vmcs01(vcpu);
10720 nested_vmx_entry_failure(vcpu, vmcs12,
10721 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10722 return 1;
10723 }
10724
10725 vmcs12->launch_state = 1;
10726
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010727 /* Hide L1D cache contents from the nested guest. */
10728 vmx->vcpu.arch.l1tf_flush_l1d = true;
10729
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010730 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010731 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010732
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010733 vmx->nested.nested_run_pending = 1;
10734
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010735 /*
10736 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10737 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10738 * returned as far as L1 is concerned. It will only return (and set
10739 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10740 */
10741 return 1;
10742}
10743
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010744/*
10745 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10746 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10747 * This function returns the new value we should put in vmcs12.guest_cr0.
10748 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10749 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10750 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10751 * didn't trap the bit, because if L1 did, so would L0).
10752 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10753 * been modified by L2, and L1 knows it. So just leave the old value of
10754 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10755 * isn't relevant, because if L0 traps this bit it can set it to anything.
10756 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10757 * changed these bits, and therefore they need to be updated, but L0
10758 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10759 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10760 */
10761static inline unsigned long
10762vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10763{
10764 return
10765 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10766 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10767 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10768 vcpu->arch.cr0_guest_owned_bits));
10769}
10770
10771static inline unsigned long
10772vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10773{
10774 return
10775 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10776 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10777 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10778 vcpu->arch.cr4_guest_owned_bits));
10779}
10780
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010781static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10782 struct vmcs12 *vmcs12)
10783{
10784 u32 idt_vectoring;
10785 unsigned int nr;
10786
Gleb Natapov851eb6672013-09-25 12:51:34 +030010787 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010788 nr = vcpu->arch.exception.nr;
10789 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10790
10791 if (kvm_exception_is_soft(nr)) {
10792 vmcs12->vm_exit_instruction_len =
10793 vcpu->arch.event_exit_inst_len;
10794 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10795 } else
10796 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10797
10798 if (vcpu->arch.exception.has_error_code) {
10799 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10800 vmcs12->idt_vectoring_error_code =
10801 vcpu->arch.exception.error_code;
10802 }
10803
10804 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010805 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010806 vmcs12->idt_vectoring_info_field =
10807 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10808 } else if (vcpu->arch.interrupt.pending) {
10809 nr = vcpu->arch.interrupt.nr;
10810 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10811
10812 if (vcpu->arch.interrupt.soft) {
10813 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10814 vmcs12->vm_entry_instruction_len =
10815 vcpu->arch.event_exit_inst_len;
10816 } else
10817 idt_vectoring |= INTR_TYPE_EXT_INTR;
10818
10819 vmcs12->idt_vectoring_info_field = idt_vectoring;
10820 }
10821}
10822
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010823static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10824{
10825 struct vcpu_vmx *vmx = to_vmx(vcpu);
10826
Jan Kiszkaf4124502014-03-07 20:03:13 +010010827 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10828 vmx->nested.preemption_timer_expired) {
10829 if (vmx->nested.nested_run_pending)
10830 return -EBUSY;
10831 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10832 return 0;
10833 }
10834
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010835 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010836 if (vmx->nested.nested_run_pending ||
10837 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010838 return -EBUSY;
10839 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10840 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10841 INTR_INFO_VALID_MASK, 0);
10842 /*
10843 * The NMI-triggered VM exit counts as injection:
10844 * clear this one and block further NMIs.
10845 */
10846 vcpu->arch.nmi_pending = 0;
10847 vmx_set_nmi_mask(vcpu, true);
10848 return 0;
10849 }
10850
10851 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10852 nested_exit_on_intr(vcpu)) {
10853 if (vmx->nested.nested_run_pending)
10854 return -EBUSY;
10855 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010856 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010857 }
10858
David Hildenbrand1edccf22017-01-25 11:58:58 +010010859 vmx_complete_nested_posted_interrupt(vcpu);
10860 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010861}
10862
Jan Kiszkaf4124502014-03-07 20:03:13 +010010863static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10864{
10865 ktime_t remaining =
10866 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10867 u64 value;
10868
10869 if (ktime_to_ns(remaining) <= 0)
10870 return 0;
10871
10872 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10873 do_div(value, 1000000);
10874 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10875}
10876
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010877/*
10878 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10879 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10880 * and this function updates it to reflect the changes to the guest state while
10881 * L2 was running (and perhaps made some exits which were handled directly by L0
10882 * without going back to L1), and to reflect the exit reason.
10883 * Note that we do not have to copy here all VMCS fields, just those that
10884 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10885 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10886 * which already writes to vmcs12 directly.
10887 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010888static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10889 u32 exit_reason, u32 exit_intr_info,
10890 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010891{
10892 /* update guest state fields: */
10893 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10894 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10895
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010896 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10897 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10898 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10899
10900 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10901 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10902 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10903 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10904 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10905 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10906 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10907 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10908 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10909 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10910 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10911 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10912 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10913 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10914 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10915 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10916 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10917 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10918 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10919 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10920 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10921 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10922 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10923 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10924 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10925 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10926 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10927 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10928 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10929 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10930 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10931 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10932 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10933 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10934 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10935 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10936
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010937 vmcs12->guest_interruptibility_info =
10938 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10939 vmcs12->guest_pending_dbg_exceptions =
10940 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010941 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10942 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10943 else
10944 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010945
Jan Kiszkaf4124502014-03-07 20:03:13 +010010946 if (nested_cpu_has_preemption_timer(vmcs12)) {
10947 if (vmcs12->vm_exit_controls &
10948 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10949 vmcs12->vmx_preemption_timer_value =
10950 vmx_get_preemption_timer_value(vcpu);
10951 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10952 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010953
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010954 /*
10955 * In some cases (usually, nested EPT), L2 is allowed to change its
10956 * own CR3 without exiting. If it has changed it, we must keep it.
10957 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10958 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10959 *
10960 * Additionally, restore L2's PDPTR to vmcs12.
10961 */
10962 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010963 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010964 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10965 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10966 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10967 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10968 }
10969
Jim Mattson4933e9f2017-06-01 12:44:46 -070010970 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010971
Wincy Van608406e2015-02-03 23:57:51 +080010972 if (nested_cpu_has_vid(vmcs12))
10973 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10974
Jan Kiszkac18911a2013-03-13 16:06:41 +010010975 vmcs12->vm_entry_controls =
10976 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010977 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010978
Jan Kiszka2996fca2014-06-16 13:59:43 +020010979 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10980 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10981 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10982 }
10983
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010984 /* TODO: These cannot have changed unless we have MSR bitmaps and
10985 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010986 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010987 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010988 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10989 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010990 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10991 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10992 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010993 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010994 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010995 if (nested_cpu_has_xsaves(vmcs12))
10996 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010997
10998 /* update exit information fields: */
10999
Jan Kiszka533558b2014-01-04 18:47:20 +010011000 vmcs12->vm_exit_reason = exit_reason;
11001 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011002
Jan Kiszka533558b2014-01-04 18:47:20 +010011003 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020011004 if ((vmcs12->vm_exit_intr_info &
11005 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
11006 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
11007 vmcs12->vm_exit_intr_error_code =
11008 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011009 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011010 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11011 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11012
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011013 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
11014 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11015 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011016 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011017
11018 /*
11019 * Transfer the event that L0 or L1 may wanted to inject into
11020 * L2 to IDT_VECTORING_INFO_FIELD.
11021 */
11022 vmcs12_save_pending_event(vcpu, vmcs12);
11023 }
11024
11025 /*
11026 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11027 * preserved above and would only end up incorrectly in L1.
11028 */
11029 vcpu->arch.nmi_injected = false;
11030 kvm_clear_exception_queue(vcpu);
11031 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011032}
11033
11034/*
11035 * A part of what we need to when the nested L2 guest exits and we want to
11036 * run its L1 parent, is to reset L1's guest state to the host state specified
11037 * in vmcs12.
11038 * This function is to be called not only on normal nested exit, but also on
11039 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11040 * Failures During or After Loading Guest State").
11041 * This function should be called when the active VMCS is L1's (vmcs01).
11042 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011043static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11044 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011045{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011046 struct kvm_segment seg;
11047
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011048 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11049 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011050 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011051 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11052 else
11053 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11054 vmx_set_efer(vcpu, vcpu->arch.efer);
11055
11056 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11057 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011058 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011059 /*
11060 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
11061 * actually changed, because it depends on the current state of
11062 * fpu_active (which may have changed).
11063 * Note that vmx_set_cr0 refers to efer set above.
11064 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020011065 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011066 /*
11067 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
11068 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
11069 * but we also need to update cr0_guest_host_mask and exception_bitmap.
11070 */
11071 update_exception_bitmap(vcpu);
11072 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
11073 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11074
11075 /*
11076 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
11077 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
11078 */
11079 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080011080 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011081
Jan Kiszka29bf08f2013-12-28 16:31:52 +010011082 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011083
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011084 kvm_set_cr3(vcpu, vmcs12->host_cr3);
11085 kvm_mmu_reset_context(vcpu);
11086
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011087 if (!enable_ept)
11088 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11089
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011090 if (enable_vpid) {
11091 /*
11092 * Trivially support vpid by letting L2s share their parent
11093 * L1's vpid. TODO: move to a more elaborate solution, giving
11094 * each L2 its own vpid and exposing the vpid feature to L1.
11095 */
11096 vmx_flush_tlb(vcpu);
11097 }
11098
11099
11100 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11101 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11102 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11103 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11104 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020011105 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11106 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011107
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011108 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11109 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11110 vmcs_write64(GUEST_BNDCFGS, 0);
11111
Jan Kiszka44811c02013-08-04 17:17:27 +020011112 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011113 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011114 vcpu->arch.pat = vmcs12->host_ia32_pat;
11115 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011116 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11117 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11118 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011119
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011120 /* Set L1 segment info according to Intel SDM
11121 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11122 seg = (struct kvm_segment) {
11123 .base = 0,
11124 .limit = 0xFFFFFFFF,
11125 .selector = vmcs12->host_cs_selector,
11126 .type = 11,
11127 .present = 1,
11128 .s = 1,
11129 .g = 1
11130 };
11131 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11132 seg.l = 1;
11133 else
11134 seg.db = 1;
11135 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11136 seg = (struct kvm_segment) {
11137 .base = 0,
11138 .limit = 0xFFFFFFFF,
11139 .type = 3,
11140 .present = 1,
11141 .s = 1,
11142 .db = 1,
11143 .g = 1
11144 };
11145 seg.selector = vmcs12->host_ds_selector;
11146 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11147 seg.selector = vmcs12->host_es_selector;
11148 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11149 seg.selector = vmcs12->host_ss_selector;
11150 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11151 seg.selector = vmcs12->host_fs_selector;
11152 seg.base = vmcs12->host_fs_base;
11153 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11154 seg.selector = vmcs12->host_gs_selector;
11155 seg.base = vmcs12->host_gs_base;
11156 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11157 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011158 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011159 .limit = 0x67,
11160 .selector = vmcs12->host_tr_selector,
11161 .type = 11,
11162 .present = 1
11163 };
11164 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11165
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011166 kvm_set_dr(vcpu, 7, 0x400);
11167 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011168
Wincy Van3af18d92015-02-03 23:49:31 +080011169 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011170 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011171
Wincy Vanff651cb2014-12-11 08:52:58 +030011172 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11173 vmcs12->vm_exit_msr_load_count))
11174 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011175}
11176
11177/*
11178 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11179 * and modify vmcs12 to make it see what it would expect to see there if
11180 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11181 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011182static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11183 u32 exit_intr_info,
11184 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011185{
11186 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011187 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11188
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011189 /* trying to cancel vmlaunch/vmresume is a bug */
11190 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11191
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011192 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011193 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11194 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011195
Wincy Vanff651cb2014-12-11 08:52:58 +030011196 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11197 vmcs12->vm_exit_msr_store_count))
11198 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11199
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011200 vmx_load_vmcs01(vcpu);
11201
Bandan Das77b0f5d2014-04-19 18:17:45 -040011202 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11203 && nested_exit_intr_ack_set(vcpu)) {
11204 int irq = kvm_cpu_get_interrupt(vcpu);
11205 WARN_ON(irq < 0);
11206 vmcs12->vm_exit_intr_info = irq |
11207 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11208 }
11209
Jan Kiszka542060e2014-01-04 18:47:21 +010011210 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11211 vmcs12->exit_qualification,
11212 vmcs12->idt_vectoring_info_field,
11213 vmcs12->vm_exit_intr_info,
11214 vmcs12->vm_exit_intr_error_code,
11215 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011216
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011217 vm_entry_controls_reset_shadow(vmx);
11218 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011219 vmx_segment_cache_clear(vmx);
11220
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011221 load_vmcs12_host_state(vcpu, vmcs12);
11222
Paolo Bonzini93140062016-07-06 13:23:51 +020011223 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011224 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11225 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011226 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011227 if (vmx->hv_deadline_tsc == -1)
11228 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11229 PIN_BASED_VMX_PREEMPTION_TIMER);
11230 else
11231 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11232 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011233 if (kvm_has_tsc_control)
11234 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011235
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011236 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11237 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11238 vmx_set_virtual_x2apic_mode(vcpu,
11239 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011240 } else if (!nested_cpu_has_ept(vmcs12) &&
11241 nested_cpu_has2(vmcs12,
11242 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11243 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011244 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011245
11246 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11247 vmx->host_rsp = 0;
11248
11249 /* Unpin physical memory we referred to in vmcs02 */
11250 if (vmx->nested.apic_access_page) {
11251 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011252 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011253 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011254 if (vmx->nested.virtual_apic_page) {
11255 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011256 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011257 }
Wincy Van705699a2015-02-03 23:58:17 +080011258 if (vmx->nested.pi_desc_page) {
11259 kunmap(vmx->nested.pi_desc_page);
11260 nested_release_page(vmx->nested.pi_desc_page);
11261 vmx->nested.pi_desc_page = NULL;
11262 vmx->nested.pi_desc = NULL;
11263 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011264
11265 /*
Tang Chen38b99172014-09-24 15:57:54 +080011266 * We are now running in L2, mmu_notifier will force to reload the
11267 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11268 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011269 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011270
11271 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011272 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11273 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11274 * success or failure flag accordingly.
11275 */
11276 if (unlikely(vmx->fail)) {
11277 vmx->fail = 0;
11278 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11279 } else
11280 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011281 if (enable_shadow_vmcs)
11282 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011283
11284 /* in case we halted in L2 */
11285 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011286}
11287
Nadav Har'El7c177932011-05-25 23:12:04 +030011288/*
Jan Kiszka42124922014-01-04 18:47:19 +010011289 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11290 */
11291static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11292{
Wanpeng Lic886f282017-03-06 04:03:28 -080011293 if (is_guest_mode(vcpu)) {
11294 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011295 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011296 }
Jan Kiszka42124922014-01-04 18:47:19 +010011297 free_nested(to_vmx(vcpu));
11298}
11299
11300/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011301 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11302 * 23.7 "VM-entry failures during or after loading guest state" (this also
11303 * lists the acceptable exit-reason and exit-qualification parameters).
11304 * It should only be called before L2 actually succeeded to run, and when
11305 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11306 */
11307static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11308 struct vmcs12 *vmcs12,
11309 u32 reason, unsigned long qualification)
11310{
11311 load_vmcs12_host_state(vcpu, vmcs12);
11312 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11313 vmcs12->exit_qualification = qualification;
11314 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011315 if (enable_shadow_vmcs)
11316 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011317}
11318
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011319static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11320 struct x86_instruction_info *info,
11321 enum x86_intercept_stage stage)
11322{
11323 return X86EMUL_CONTINUE;
11324}
11325
Yunhong Jiang64672c92016-06-13 14:19:59 -070011326#ifdef CONFIG_X86_64
11327/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11328static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11329 u64 divisor, u64 *result)
11330{
11331 u64 low = a << shift, high = a >> (64 - shift);
11332
11333 /* To avoid the overflow on divq */
11334 if (high >= divisor)
11335 return 1;
11336
11337 /* Low hold the result, high hold rem which is discarded */
11338 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11339 "rm" (divisor), "0" (low), "1" (high));
11340 *result = low;
11341
11342 return 0;
11343}
11344
11345static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11346{
11347 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011348 u64 tscl = rdtsc();
11349 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11350 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011351
11352 /* Convert to host delta tsc if tsc scaling is enabled */
11353 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11354 u64_shl_div_u64(delta_tsc,
11355 kvm_tsc_scaling_ratio_frac_bits,
11356 vcpu->arch.tsc_scaling_ratio,
11357 &delta_tsc))
11358 return -ERANGE;
11359
11360 /*
11361 * If the delta tsc can't fit in the 32 bit after the multi shift,
11362 * we can't use the preemption timer.
11363 * It's possible that it fits on later vmentries, but checking
11364 * on every vmentry is costly so we just use an hrtimer.
11365 */
11366 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11367 return -ERANGE;
11368
11369 vmx->hv_deadline_tsc = tscl + delta_tsc;
11370 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11371 PIN_BASED_VMX_PREEMPTION_TIMER);
11372 return 0;
11373}
11374
11375static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11376{
11377 struct vcpu_vmx *vmx = to_vmx(vcpu);
11378 vmx->hv_deadline_tsc = -1;
11379 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11380 PIN_BASED_VMX_PREEMPTION_TIMER);
11381}
11382#endif
11383
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011384static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011385{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011386 if (ple_gap)
11387 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011388}
11389
Kai Huang843e4332015-01-28 10:54:28 +080011390static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11391 struct kvm_memory_slot *slot)
11392{
11393 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11394 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11395}
11396
11397static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11398 struct kvm_memory_slot *slot)
11399{
11400 kvm_mmu_slot_set_dirty(kvm, slot);
11401}
11402
11403static void vmx_flush_log_dirty(struct kvm *kvm)
11404{
11405 kvm_flush_pml_buffers(kvm);
11406}
11407
11408static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11409 struct kvm_memory_slot *memslot,
11410 gfn_t offset, unsigned long mask)
11411{
11412 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11413}
11414
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011415static void __pi_post_block(struct kvm_vcpu *vcpu)
11416{
11417 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11418 struct pi_desc old, new;
11419 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011420
11421 do {
11422 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011423 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11424 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011425
11426 dest = cpu_physical_id(vcpu->cpu);
11427
11428 if (x2apic_enabled())
11429 new.ndst = dest;
11430 else
11431 new.ndst = (dest << 8) & 0xFF00;
11432
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011433 /* set 'NV' to 'notification vector' */
11434 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011435 } while (cmpxchg64(&pi_desc->control, old.control,
11436 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011437
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011438 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11439 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011440 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011441 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011442 vcpu->pre_pcpu = -1;
11443 }
11444}
11445
Feng Wuefc64402015-09-18 22:29:51 +080011446/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011447 * This routine does the following things for vCPU which is going
11448 * to be blocked if VT-d PI is enabled.
11449 * - Store the vCPU to the wakeup list, so when interrupts happen
11450 * we can find the right vCPU to wake up.
11451 * - Change the Posted-interrupt descriptor as below:
11452 * 'NDST' <-- vcpu->pre_pcpu
11453 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11454 * - If 'ON' is set during this process, which means at least one
11455 * interrupt is posted for this vCPU, we cannot block it, in
11456 * this case, return 1, otherwise, return 0.
11457 *
11458 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011459static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011460{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011461 unsigned int dest;
11462 struct pi_desc old, new;
11463 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11464
11465 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011466 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11467 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011468 return 0;
11469
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011470 WARN_ON(irqs_disabled());
11471 local_irq_disable();
11472 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11473 vcpu->pre_pcpu = vcpu->cpu;
11474 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11475 list_add_tail(&vcpu->blocked_vcpu_list,
11476 &per_cpu(blocked_vcpu_on_cpu,
11477 vcpu->pre_pcpu));
11478 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11479 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011480
11481 do {
11482 old.control = new.control = pi_desc->control;
11483
Feng Wubf9f6ac2015-09-18 22:29:55 +080011484 WARN((pi_desc->sn == 1),
11485 "Warning: SN field of posted-interrupts "
11486 "is set before blocking\n");
11487
11488 /*
11489 * Since vCPU can be preempted during this process,
11490 * vcpu->cpu could be different with pre_pcpu, we
11491 * need to set pre_pcpu as the destination of wakeup
11492 * notification event, then we can find the right vCPU
11493 * to wakeup in wakeup handler if interrupts happen
11494 * when the vCPU is in blocked state.
11495 */
11496 dest = cpu_physical_id(vcpu->pre_pcpu);
11497
11498 if (x2apic_enabled())
11499 new.ndst = dest;
11500 else
11501 new.ndst = (dest << 8) & 0xFF00;
11502
11503 /* set 'NV' to 'wakeup vector' */
11504 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011505 } while (cmpxchg64(&pi_desc->control, old.control,
11506 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011507
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011508 /* We should not block the vCPU if an interrupt is posted for it. */
11509 if (pi_test_on(pi_desc) == 1)
11510 __pi_post_block(vcpu);
11511
11512 local_irq_enable();
11513 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011514}
11515
Yunhong Jiangbc225122016-06-13 14:19:58 -070011516static int vmx_pre_block(struct kvm_vcpu *vcpu)
11517{
11518 if (pi_pre_block(vcpu))
11519 return 1;
11520
Yunhong Jiang64672c92016-06-13 14:19:59 -070011521 if (kvm_lapic_hv_timer_in_use(vcpu))
11522 kvm_lapic_switch_to_sw_timer(vcpu);
11523
Yunhong Jiangbc225122016-06-13 14:19:58 -070011524 return 0;
11525}
11526
11527static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011528{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011529 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011530 return;
11531
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011532 WARN_ON(irqs_disabled());
11533 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011534 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011535 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011536}
11537
Yunhong Jiangbc225122016-06-13 14:19:58 -070011538static void vmx_post_block(struct kvm_vcpu *vcpu)
11539{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011540 if (kvm_x86_ops->set_hv_timer)
11541 kvm_lapic_switch_to_hv_timer(vcpu);
11542
Yunhong Jiangbc225122016-06-13 14:19:58 -070011543 pi_post_block(vcpu);
11544}
11545
Feng Wubf9f6ac2015-09-18 22:29:55 +080011546/*
Feng Wuefc64402015-09-18 22:29:51 +080011547 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11548 *
11549 * @kvm: kvm
11550 * @host_irq: host irq of the interrupt
11551 * @guest_irq: gsi of the interrupt
11552 * @set: set or unset PI
11553 * returns 0 on success, < 0 on failure
11554 */
11555static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11556 uint32_t guest_irq, bool set)
11557{
11558 struct kvm_kernel_irq_routing_entry *e;
11559 struct kvm_irq_routing_table *irq_rt;
11560 struct kvm_lapic_irq irq;
11561 struct kvm_vcpu *vcpu;
11562 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011563 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011564
11565 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011566 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11567 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011568 return 0;
11569
11570 idx = srcu_read_lock(&kvm->irq_srcu);
11571 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011572 if (guest_irq >= irq_rt->nr_rt_entries ||
11573 hlist_empty(&irq_rt->map[guest_irq])) {
11574 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11575 guest_irq, irq_rt->nr_rt_entries);
11576 goto out;
11577 }
Feng Wuefc64402015-09-18 22:29:51 +080011578
11579 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11580 if (e->type != KVM_IRQ_ROUTING_MSI)
11581 continue;
11582 /*
11583 * VT-d PI cannot support posting multicast/broadcast
11584 * interrupts to a vCPU, we still use interrupt remapping
11585 * for these kind of interrupts.
11586 *
11587 * For lowest-priority interrupts, we only support
11588 * those with single CPU as the destination, e.g. user
11589 * configures the interrupts via /proc/irq or uses
11590 * irqbalance to make the interrupts single-CPU.
11591 *
11592 * We will support full lowest-priority interrupt later.
11593 */
11594
Radim Krčmář371313132016-07-12 22:09:27 +020011595 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011596 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11597 /*
11598 * Make sure the IRTE is in remapped mode if
11599 * we don't handle it in posted mode.
11600 */
11601 ret = irq_set_vcpu_affinity(host_irq, NULL);
11602 if (ret < 0) {
11603 printk(KERN_INFO
11604 "failed to back to remapped mode, irq: %u\n",
11605 host_irq);
11606 goto out;
11607 }
11608
Feng Wuefc64402015-09-18 22:29:51 +080011609 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011610 }
Feng Wuefc64402015-09-18 22:29:51 +080011611
11612 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11613 vcpu_info.vector = irq.vector;
11614
Feng Wub6ce9782016-01-25 16:53:35 +080011615 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011616 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11617
11618 if (set)
11619 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011620 else
Feng Wuefc64402015-09-18 22:29:51 +080011621 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011622
11623 if (ret < 0) {
11624 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11625 __func__);
11626 goto out;
11627 }
11628 }
11629
11630 ret = 0;
11631out:
11632 srcu_read_unlock(&kvm->irq_srcu, idx);
11633 return ret;
11634}
11635
Ashok Rajc45dcc72016-06-22 14:59:56 +080011636static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11637{
11638 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11639 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11640 FEATURE_CONTROL_LMCE;
11641 else
11642 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11643 ~FEATURE_CONTROL_LMCE;
11644}
11645
Kees Cook404f6aa2016-08-08 16:29:06 -070011646static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011647 .cpu_has_kvm_support = cpu_has_kvm_support,
11648 .disabled_by_bios = vmx_disabled_by_bios,
11649 .hardware_setup = hardware_setup,
11650 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011651 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011652 .hardware_enable = hardware_enable,
11653 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011654 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011655 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011656
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011657 .vm_init = vmx_vm_init,
11658
Avi Kivity6aa8b732006-12-10 02:21:36 -080011659 .vcpu_create = vmx_create_vcpu,
11660 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011661 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011662
Avi Kivity04d2cc72007-09-10 18:10:54 +030011663 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011664 .vcpu_load = vmx_vcpu_load,
11665 .vcpu_put = vmx_vcpu_put,
11666
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011667 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky62d88fc2018-02-21 13:39:51 -060011668 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011669 .get_msr = vmx_get_msr,
11670 .set_msr = vmx_set_msr,
11671 .get_segment_base = vmx_get_segment_base,
11672 .get_segment = vmx_get_segment,
11673 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011674 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011675 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011676 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011677 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011678 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011679 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011680 .set_cr3 = vmx_set_cr3,
11681 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011682 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011683 .get_idt = vmx_get_idt,
11684 .set_idt = vmx_set_idt,
11685 .get_gdt = vmx_get_gdt,
11686 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011687 .get_dr6 = vmx_get_dr6,
11688 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011689 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011690 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011691 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011692 .get_rflags = vmx_get_rflags,
11693 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011694
11695 .get_pkru = vmx_get_pkru,
11696
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011697 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011698 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011699
11700 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011701
Avi Kivity6aa8b732006-12-10 02:21:36 -080011702 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011703 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011704 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011705 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11706 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011707 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011708 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011709 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011710 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011711 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011712 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011713 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011714 .get_nmi_mask = vmx_get_nmi_mask,
11715 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011716 .enable_nmi_window = enable_nmi_window,
11717 .enable_irq_window = enable_irq_window,
11718 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011719 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011720 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011721 .get_enable_apicv = vmx_get_enable_apicv,
11722 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011723 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11724 .hwapic_irr_update = vmx_hwapic_irr_update,
11725 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011726 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11727 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011728
Izik Eiduscbc94022007-10-25 00:29:55 +020011729 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011730 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011731 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011732
Avi Kivity586f9602010-11-18 13:09:54 +020011733 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011734
Sheng Yang17cc3932010-01-05 19:02:27 +080011735 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011736
11737 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011738
11739 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011740 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011741
11742 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011743
11744 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011745
11746 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011747
11748 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011749
11750 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011751 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011752 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011753 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011754
11755 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011756
11757 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011758
11759 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11760 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11761 .flush_log_dirty = vmx_flush_log_dirty,
11762 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011763
Feng Wubf9f6ac2015-09-18 22:29:55 +080011764 .pre_block = vmx_pre_block,
11765 .post_block = vmx_post_block,
11766
Wei Huang25462f72015-06-19 15:45:05 +020011767 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011768
11769 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011770
11771#ifdef CONFIG_X86_64
11772 .set_hv_timer = vmx_set_hv_timer,
11773 .cancel_hv_timer = vmx_cancel_hv_timer,
11774#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011775
11776 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011777};
11778
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011779static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011780{
11781 if (vmx_l1d_flush_pages) {
11782 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11783 vmx_l1d_flush_pages = NULL;
11784 }
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011785 /* Restore state so sysfs ignores VMX */
11786 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011787}
11788
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011789
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011790static void vmx_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011791{
Dave Young2965faa2015-09-09 15:38:55 -070011792#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011793 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011794 synchronize_rcu();
11795#endif
11796
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011797 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011798
Thomas Gleixner80e55b52018-07-13 16:23:16 +020011799 vmx_cleanup_l1d_flush();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011800}
Avi Kivity6aa8b732006-12-10 02:21:36 -080011801module_exit(vmx_exit)
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011802
11803static int __init vmx_init(void)
11804{
11805 int r;
11806
11807 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11808 __alignof__(struct vcpu_vmx), THIS_MODULE);
11809 if (r)
11810 return r;
11811
11812 /*
Thomas Gleixner641a2112018-07-13 16:23:19 +020011813 * Must be called after kvm_init() so enable_ept is properly set
11814 * up. Hand the parameter mitigation value in which was stored in
11815 * the pre module init parser. If no parameter was given, it will
11816 * contain 'auto' which will be turned into the default 'cond'
11817 * mitigation mode.
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011818 */
Thomas Gleixner641a2112018-07-13 16:23:19 +020011819 if (boot_cpu_has(X86_BUG_L1TF)) {
11820 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
11821 if (r) {
11822 vmx_exit();
11823 return r;
11824 }
Thomas Gleixner4186ae82018-07-13 16:23:18 +020011825 }
11826
11827#ifdef CONFIG_KEXEC_CORE
11828 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11829 crash_vmclear_local_loaded_vmcss);
11830#endif
11831
11832 return 0;
11833}
11834module_init(vmx_init)